dba9001626ad8f9d0a24e6cd2bd82bce4a2e3eb5
[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   Template => {
36     class => 'App::SCS::Plugin::Core::PagePlugin::Template',
37     config => sub { templates => $self->templates },
38   },
39   Include => {
40     class => 'App::SCS::Plugin::Core::PagePlugin::Include',
41     config => sub { includes => $self->includes },
42   },
43   PageData => 'App::SCS::Plugin::Core::PagePlugin::PageData',
44 }
45
46 sub default_page_plugins {
47   Template => {
48     name => 'layout',
49   },
50   PageData => {},
51 }
52
53 sub page_dispatchers {
54   my ($self) = @_;
55   sub (/) {
56     $self->pages->get({ path => 'index' });
57   },
58   sub (/**) {
59     [ 302, [ 'Location' => "/$_[1]/" ], [] ]
60   },
61   sub (/**:path/) {
62     $self->pages->get({ %_ })
63   },
64   sub () {
65     $self->pages
66          ->get({ path => 'error_404' })
67          ->$_call_if_object(with_plugin_config => Status => 404);
68   },
69 }
70
71 sub provides_pages {
72   my ($self) = @_;
73   "/", map "$_/", $self->pages->all_paths;
74 }
75
76 1;