From: Errietta Kostala Date: Mon, 18 Aug 2014 10:13:43 +0000 (+0100) Subject: Per-path config files X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fd5c2ec2b8daefffdb4dc95b01f1118dde99a7fb;p=scpubgit%2FApp-SCS.git Per-path config files --- diff --git a/Makefile.PL b/Makefile.PL index 1c366b0..59c1e05 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -18,5 +18,7 @@ WriteMakefile( 'Try::Tiny' => 0, 'Plack::Middleware::SSI' => '0.12', 'JSON::MaybeXS' => 0, + 'Hash::Merge' => 0, + 'JSONY' => 0 } ); diff --git a/lib/App/SCS/Page.pm b/lib/App/SCS/Page.pm index 355f129..7c4350c 100644 --- a/lib/App/SCS/Page.pm +++ b/lib/App/SCS/Page.pm @@ -24,9 +24,8 @@ has plugin_config => (is => 'lazy'); sub _build_plugin_config { my ($self) = @_; - $self->plugins - ? pond_read_datum('[ '.$self->plugins.' ]') - : [] + + return $self->plugins; } sub has_plugin_config { exists $_[0]->plugin_config->{$_[1]} } 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"; }