$page->body, custom plugin loading
[scpubgit/App-SCS.git] / lib / App / SCS.pm
CommitLineData
632f0e07 1package App::SCS;
2
3use Module::Runtime qw(use_module);
4use IO::All;
5use Moo;
6
7with 'App::SCS::Role::WithConfig';
8
9has plugins => (is => 'ro', default => sub { [] });
10
0034f151 11has root_dir => (is => 'lazy');
12
13sub _build_root_dir {
14 my ($self) = @_;
15 io->dir(io->dir($self->config->{root_dir})->absolute);
16}
17
18has share_dir => (is => 'lazy');
19
20sub _build_share_dir {
21 my ($self) = @_;
22 io->dir($self->config->{share_dir}||$self->root_dir->catdir('share'));
23}
24
25has page_plugin_config => (is => 'lazy');
26
27sub _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
632f0e07 43has pages => (is => 'lazy');
44
45sub _build_pages {
46 my ($self) = @_;
47 return use_module('App::SCS::PageSet')->new(
0034f151 48 base_dir => $self->share_dir->catdir('pages'),
49 plugin_config => $self->page_plugin_config,
632f0e07 50 );
51}
52
53has web => (is => 'lazy');
54
55sub _build_web {
56 my ($self) = @_;
57 return use_module('App::SCS::Web')->new(
58 app => $self
59 );
60}
61
62sub BUILD {
63 my ($self) = @_;
64 $self->load_plugin(Core => {});
f50b4a35 65 my @plist = @{$self->config->{plugins}||[]};
66 while (my ($name, $conf) = splice @plist, 0, 2) {
67 $self->load_plugin($name, $conf);
632f0e07 68 }
69}
70
71sub load_plugin {
72 my ($self, $name, $config) = @_;
18968192 73 my $class = ($name =~ s/^\+// ? $name : "App::SCS::Plugin::${name}");
632f0e07 74 push(
75 @{$self->plugins},
18968192 76 use_module($class)->new(
632f0e07 77 app => $self,
78 config => $config
79 )
80 );
81 return;
82}
83
f50b4a35 84sub run_if_script {
85 my $self = shift;
86 if (caller(1)) {
87 return 1;
88 } else {
89 return $self->run;
90 }
91}
92
93sub run {
94 my $self = shift;
95 my $env = {
96 argv => \@ARGV,
97 stdin => \*STDIN,
98 stdout => \*STDOUT,
99 stderr => \*STDERR,
100 };
101 foreach my $p (@{$self->plugins}) {
102 return if $p->run_cli($env);
103 }
104 $self->web->run;
105}
106
632f0e07 1071;