X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FApp%2FSCS%2FPageSet.pm;h=731f4937c6568cc184cbc36c8c0d1b5ef5cf6211;hb=fd5c2ec2b8daefffdb4dc95b01f1118dde99a7fb;hp=26fbe6aad3e152f5a582c80ff71834592d37587d;hpb=f6e34d3cc963df2863f16eead38ff1a4f70e3ab2;p=scpubgit%2FApp-SCS.git diff --git a/lib/App/SCS/PageSet.pm b/lib/App/SCS/PageSet.pm index 26fbe6a..731f493 100644 --- a/lib/App/SCS/PageSet.pm +++ b/lib/App/SCS/PageSet.pm @@ -7,10 +7,13 @@ use Syntax::Keyword::Gather; use App::SCS::Page; use IO::All; use Try::Tiny; -use List::Util qw(reduce); +use List::Util qw(reduce max); use Module::Runtime qw(use_module); use JSON::MaybeXS; use Moo; +use Hash::Merge qw(merge); +use Data::Pond qw(pond_read_datum pond_write_datum); +use JSONY; with 'App::SCS::Role::PageChildren'; @@ -58,12 +61,47 @@ sub get { ); } +sub _get_config_files { + my ($self, $path) = @_; + + my @files = (); + my @dirs = io->dir($path)->splitdir; + + + shift @dirs; + pop @dirs; + my $buildpath = io(''); + + foreach my $dir (@dirs) { + $buildpath = $buildpath->catdir("/$dir"); + #/home/.../share/pages/blog.conf etc + + my $file = $self->_top_dir->catfile("$buildpath.conf"); + + if (!$file->exists || !$file->file || $file->empty) { + next; + } + + push @files, $file; + } + + return \@files; +} + sub _inflate { my ($self, $type, $path, $io) = @_; (my $cache_name = $io->name) =~ s/\/([^\/]+)$/\/.htcache.$1.json/; my $cache = io($cache_name); + my $config_files = $self->_get_config_files($path); + my $maxstat = 0; + + if (scalar @$config_files) { + my $maxfile = reduce { $a->mtime > $b->mtime ? $a : $b } @$config_files; + $maxstat = $maxfile->mtime; + } + if (-f $cache_name) { - if ($cache->mtime >= $io->mtime) { + if ($cache->mtime >= max($io->mtime, $maxstat)) { return try { $self->_new_page($path, $self->_json->decode($cache->all)); } catch { @@ -74,8 +112,19 @@ sub _inflate { my $raw = $io->all; try { my $extracted = $self->${\"_extract_from_${type}"}($raw); - try { $cache->print($self->_json->encode($extracted)); }; - $self->_new_page($path, $extracted); + my $jsony = JSONY->new; + my $config = reduce { merge($a, $jsony->load($b->all)) } {}, @$config_files; + + $extracted->{plugins} = pond_read_datum('[' . $extracted->{plugins} . ']'); + my $setup = merge($extracted, $config); + + try { + my $tmp_cache = io($cache_name . ".tmp"); + $tmp_cache->print($self->_json->encode($setup)); + $tmp_cache->rename($cache_name); + }; + + $self->_new_page($path, $setup); } catch { die "Error inflating ${path} as ${type}: $_\n"; }