7d9dfc609524c9f02b4eb2cf6350f4d893fffdb9
[scpubgit/App-SCS.git] / lib / App / SCS / Plugin / Core.pm
1 package App::SCS::Plugin::Core;
2
3 use Module::Runtime qw(use_module);
4 use IO::All;
5 use Moo;
6 no warnings::illegalproto;
7 use Safe::Isa;
8
9 with 'App::SCS::Role::Plugin';
10
11 has templates => (is => 'lazy');
12
13 sub _build_templates {
14   my ($self) = @_;
15   return use_module('App::SCS::PageSet')->new(
16     base_dir => io->dir($self->app->share_dir)->catdir('templates'),
17     plugin_config => {
18       plugin_map => $self->app->page_plugin_config->{plugin_map}
19     }
20   );
21 }
22
23 has includes => (is => 'lazy');
24
25 sub _build_includes {
26   my ($self) = @_;
27   return use_module('App::SCS::PageSet')->new(
28     base_dir => io->dir($self->app->config->{share_dir})->catdir('includes'),
29   );
30 }
31
32 sub page_plugins {
33   my ($self) = @_;
34   PageList => 'App::SCS::Plugin::Core::PagePlugin::PageList',
35   RemovePlugin => 'App::SCS::Plugin::Core::PagePlugin::RemovePlugin',
36   Template => {
37     class => 'App::SCS::Plugin::Core::PagePlugin::Template',
38     config => sub { templates => $self->templates },
39   },
40   Include => {
41     class => 'App::SCS::Plugin::Core::PagePlugin::Include',
42     config => sub { includes => $self->includes },
43   },
44   PageData => 'App::SCS::Plugin::Core::PagePlugin::PageData',
45 }
46
47 sub default_page_plugins {
48   Template => {
49     name => 'layout',
50   },
51   PageData => {},
52 }
53
54 sub page_dispatchers {
55   my ($self) = @_;
56   sub (/) {
57     $self->pages->get({ path => 'index' });
58   },
59   sub (/**) {
60     [ 302, [ 'Location' => "/$_[1]/" ], [] ]
61   },
62   sub (/**:path/) {
63     $self->pages->get({ %_ })
64   },
65   sub () {
66     $self->pages
67          ->get({ path => 'error_404' })
68          ->$_call_if_object(with_plugin_config => Status => 404);
69   },
70 }
71
72 sub provides_pages {
73   my ($self) = @_;
74   "/", map "$_/", $self->pages->all_paths;
75 }
76
77 1;