56b393b51f99e2bf2b8fc668d4c0164cb80cea02
[scpubgit/App-SCS.git] / lib / App / SCS.pm
1 package App::SCS;
2
3 use Module::Runtime qw(use_module);
4 use IO::All;
5 use Moo;
6
7 with 'App::SCS::Role::WithConfig';
8
9 has plugins => (is => 'ro', default => sub { [] });
10
11 has root_dir => (is => 'lazy');
12
13 sub _build_root_dir {
14   my ($self) = @_;
15   io->dir(io->dir($self->config->{root_dir})->absolute);
16 }
17
18 has share_dir => (is => 'lazy');
19
20 sub _build_share_dir {
21   my ($self) = @_;
22   io->dir($self->config->{share_dir}||$self->root_dir->catdir('share'));
23 }
24
25 has page_plugin_config => (is => 'lazy');
26
27 sub _build_page_plugin_config {
28   my ($self) = @_;
29   return {
30     plugin_map => {
31       do {
32         my %map = map $_->page_plugins, reverse @{$self->plugins};
33         ref($_) or $_ = { class => $_, config => sub {} } for values %map;
34         %map;
35       }
36     },
37     defaults => [
38       map $_->default_page_plugins, @{$self->plugins}
39     ],
40   };
41 }
42
43 has pages => (is => 'lazy');
44
45 sub _build_pages {
46   my ($self) = @_;
47   return use_module('App::SCS::PageSet')->new(
48     base_dir => $self->share_dir->catdir('pages'),
49     plugin_config => $self->page_plugin_config,
50   );
51 }
52
53 has web => (is => 'lazy');
54
55 sub _build_web {
56   my ($self) = @_;
57   return use_module('App::SCS::Web')->new(
58     app => $self
59   );
60 }
61
62 sub BUILD {
63   my ($self) = @_;
64   $self->load_plugin(Core => {});
65   foreach my $spec (@{$self->config->{plugins}||[]}) {
66     $self->load_plugin(@$spec);
67   }
68 }
69
70 sub load_plugin {
71   my ($self, $name, $config) = @_;
72   push(
73     @{$self->plugins},
74     use_module("App::SCS::Plugin::${name}")->new(
75       app => $self,
76       config => $config
77     )
78   );
79   return;
80 }
81
82 1;