File Coverage

blib/lib/Bootylicious/Plugin/BootyConfig.pm
Criterion Covered Total %
statement 68 72 94.4
branch 19 26 73.0
condition 1 2 50.0
subroutine 11 12 91.6
pod 1 1 100.0
total 100 113 88.5


line stmt bran cond sub pod time code
1             package Bootylicious::Plugin::BootyConfig;
2              
3 3     3   2836 use strict;
  3         6  
  3         107  
4 3     3   12 use warnings;
  3         4  
  3         86  
5              
6 3     3   10 use base 'Mojolicious::Plugin';
  3         4  
  3         287  
7              
8 3     3   16 use Mojolicious::Controller;
  3         3  
  3         23  
9              
10             sub register {
11 4     4 1 177 my ($self, $app, $conf) = @_;
12              
13 4         21 my $c = Mojolicious::Controller->new(app => $app);
14              
15 4   50     39 $conf ||= {};
16              
17 4         21 $app->log->level('error');
18              
19             # Default plugins
20 4         180 $app->plugin('charset' => {charset => 'utf-8'});
21 4         5061 $app->plugin('PODRenderer', {no_perldoc => 1});
22 4         52605 $app->plugin('TagHelpers');
23 4         5953 $app->plugin(
24             validator => {
25             messages => {
26             REQUIRED => 'Required',
27             EMAIL_CONSTRAINT_FAILED => "Doesn't look like an email to me",
28             URL_CONSTRAINT_FAILED => "Doesn't look like an url to me"
29             }
30             }
31             );
32              
33 4 100       109826 $conf->{default} = $self->_default unless exists $conf->{default};
34 4         19 my $config = $app->plugin('JSONConfig' => $conf);
35              
36 4         7579 $app->secrets([$config->{secret}]);
37              
38             # Config access
39             $app->helper(
40             config => sub {
41 316     316   164395 my $self = shift;
42              
43 316 100       844 if (@_) {
44 308 50       2263 return $config->{$_[0]} if @_ == 1;
45              
46 0         0 $config = {%$config, @_};
47             }
48              
49 8         38 return $config;
50             }
51 4         45 );
52              
53             # Set appropriate log level
54 4         281 $app->log->level($config->{loglevel});
55              
56             # Additional Perl modules
57 4         84 $self->_setup_inc($config->{perl5lib});
58              
59             # CGI hack
60 4 50       15 $ENV{SCRIPT_NAME} = $config->{base} if defined $config->{base};
61              
62             # Don't use set locale unless it is explicitly specified via a config file
63 4         57 $ENV{LC_ALL} = 'C';
64              
65             # set proper templates base dir, if defined
66 4 100       16 push @{$app->renderer->paths}, $app->home->rel_dir($config->{templates_directory})
  3         12  
67             if defined $config->{templates_directory};
68              
69             # set proper public base dir, if defined
70 4 100       283 push @{$app->static->paths}, $app->home->rel_dir($config->{public_directory})
  3         16  
71             if defined $config->{public_directory};
72              
73 4         82 $app->defaults(title => '', description => '', layout => 'wrapper');
74              
75             # Parser helpers
76 4     5   82 $app->helper(parsers => sub { $config->{_parsers} });
  5         330  
77              
78             $app->helper(
79             add_parser => sub {
80 4     4   176 my $self = shift;
81 4         7 my ($ext, $cb) = @_;
82              
83 4         14 $config->{_parsers}->{$ext} = $cb;
84             }
85 4         88 );
86              
87 4         74 $app->plugin('booty_helpers');
88              
89             $c->add_parser(
90 4     0   147 pod => sub { $app->renderer->helpers->{pod_to_html}->(undef, @_) });
  0         0  
91              
92 4 100       14 if (my $theme = $config->{theme}) {
93 1         6 my $theme_class = join '::' => 'Bootylicious::Theme',
94             Mojo::ByteStream->new($theme)->camelize;
95              
96 1         59 $app->renderer->classes([$theme_class]);
97 1         12 $app->static->classes([$theme_class]);
98              
99 1         11 $app->plugin($theme_class);
100             }
101              
102             # Load additional plugins
103 4         121 $self->_load_plugins($app, $config->{plugins});
104             }
105              
106             sub _setup_inc {
107 4     4   8 my $self = shift;
108 4         8 my $perl5lib = shift;
109              
110 4 50       22 return unless $perl5lib;
111              
112 0 0       0 push @INC, $_ for (ref $perl5lib eq 'ARRAY' ? @{$perl5lib} : $perl5lib);
  0         0  
113             }
114              
115             sub _load_plugins {
116 4     4   7 my $self = shift;
117 4         10 my ($app, $plugins_arrayref) = @_;
118              
119 4 100       33 return unless $plugins_arrayref;
120 1 50       3 $plugins_arrayref = [$plugins_arrayref]
121             unless ref $plugins_arrayref eq 'ARRAY';
122              
123 1         1 my @plugins;
124              
125             my $prev;
126 1         2 while (my $plugin = shift @{$plugins_arrayref}) {
  3         9  
127 2 100       6 if (ref($plugin) eq 'HASH') {
128 1 50       3 next unless $plugins[-1];
129              
130 1         2 $plugins[-1]->{args} = $plugin;
131             }
132             else {
133 1         4 push @plugins, {name => $plugin, args => {}};
134             }
135             }
136              
137 1         2 foreach my $plugin (@plugins) {
138 1         5 $app->plugin($plugin->{name} => $plugin->{args});
139             }
140             }
141              
142             sub _default {
143 3     3   102 { author => 'whoami',
144             email => '',
145             title => 'Just another blog',
146             about => 'Perl hacker',
147             description => 'I do not know if I need this',
148              
149             cuttag => '[cut]',
150             cuttext => 'Keep reading',
151             pagelimit => 10,
152             datefmt => '%a, %d %b %Y',
153              
154             menu => [
155             index => '/',
156             tags => '/tags.html',
157             archive => '/articles.html'
158             ],
159             footer =>
160             'Powered by Bootylicious',
161             theme => '',
162              
163             comments_enabled => 1,
164             rss_enabled => 1,
165              
166             meta => [],
167             css => [],
168             js => [],
169              
170             strings => {
171             'archive' => 'Archive',
172             'archive-description' => 'Articles index',
173             'tags' => 'Tags',
174             'tags-description' => 'Tags overview',
175             'tag' => 'Tag',
176             'tag-description' => 'Articles with tag [_1]',
177             'draft' => 'Draft',
178             'permalink-to' => 'Permalink to',
179             'later' => 'Later',
180             'earlier' => 'Earlier',
181             'not-found' => 'The page you are looking for was not found',
182             'error' => 'Internal error occurred :('
183             },
184             template_handler => 'ep',
185              
186             perl5lib => '',
187             loglevel => 'error',
188              
189             articles_directory => 'articles',
190             pages_directory => 'pages',
191             drafts_directory => 'drafts',
192             public_directory => 'public',
193             templates_directory => 'templates',
194             };
195             }
196              
197             1;