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';
);
}
+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 {
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";
}