File Coverage

blib/lib/Ado.pm
Criterion Covered Total %
statement 79 79 100.0
branch 27 28 96.4
condition 10 12 83.3
subroutine 15 15 100.0
pod 6 6 100.0
total 137 140 97.8


line stmt bran cond sub pod time code
1             package Ado;
2 24     24   390067 use Mojo::Base 'Mojolicious';
  24         9345  
  24         141  
3 24     24   1130150 use File::Spec::Functions qw(splitdir catdir catfile);
  24         58  
  24         1753  
4 24     24   144 use List::Util 'first';
  24         44  
  24         1286  
5 24     24   126 use Mojo::Util 'class_to_path';
  24         55  
  24         1586  
6              
7             our $AUTHORITY = 'cpan:BEROV';
8             our $VERSION = '0.935';
9             our $CODENAME = 'U+2C0A GLAGOLITIC CAPITAL LETTER INITIAL IZHE (Ⰺ)';
10              
11 24     24   8023 use Ado::Control;
  24         50  
  24         176  
12 24     24   7162 use Ado::Sessions;
  24         54  
  24         194  
13             my $CLASS = __PACKAGE__;
14              
15             has ado_home => sub {
16             my @home = splitdir $INC{class_to_path $CLASS};
17             while (pop @home) {
18             if (-s catfile(@home, 'bin', lc $CLASS)) { # bin/..
19             local $ENV{MOJO_HOME} = catdir(@home);
20             return Mojo::Home->new->detect();
21             }
22             }
23             Carp::croak("$CLASS installation directory not found!");
24             };
25              
26 1     1 1 1048 sub CODENAME { return $CODENAME }
27             has sessions => sub { Ado::Sessions::get_instance(shift->config) };
28              
29             has home => sub {
30             return Mojo::Home->new->detect(); #fallback to Mojo::Home
31             };
32              
33              
34             sub _initialise {
35 25     25   64 my ($app) = @_;
36 25         77 my $home = $app->home;
37 25         204 my $mode = $app->mode;
38 25         262 my $ado_home = $app->ado_home;
39              
40             # add paths to bundled files if needed.
41 25         2833 my $templates_dir = $ado_home->rel_file('templates');
42 25         493 my $site_templates = $home->rel_file('site_templates');
43 25         413 my $renderer_paths = $app->renderer->paths;
44 25         207 my $public_dir = $ado_home->rel_file('public');
45 25         361 my $static_paths = $app->static->paths;
46             push @$renderer_paths, $templates_dir
47 25 100   25   293 unless (first { $_ eq $templates_dir } @$renderer_paths);
  25         287  
48             push @$static_paths, $public_dir
49 25 100   25   458 unless (first { $_ eq $public_dir } @$static_paths);
  25         71  
50              
51 25         381 $app->secrets([Mojo::Util::sha1_sum($app->moniker . $mode . $home),]);
52 25 50       1412 unshift @$renderer_paths, $site_templates if -d $site_templates;
53 25         472 $app->controller_class("${CLASS}::Control");
54 25         200 $app->routes->namespaces(["${CLASS}::Control"]);
55 25         283 $app->plugins->namespaces(['Mojolicious::Plugin', "${CLASS}::Plugin",]);
56 25         221 unshift @{$app->commands->namespaces}, "${CLASS}::Command";
  25         129  
57 25         1008 return $app;
58             }
59              
60             # This method will run once at server start
61             sub startup {
62 25     25 1 208820 shift->_initialise()->load_config()->load_plugins()->load_routes()->define_mime_types();
63 25         67 return;
64             }
65              
66             #load ado.conf
67             sub load_config {
68 25     25 1 61 my ($app) = @_;
69 25         67 my $app_config = $app->home->rel_file('etc/' . $app->moniker . '.conf');
70             $ENV{MOJO_CONFIG} //=
71 25 100 66     845 -s $app_config
72             ? $app_config
73             : $app->ado_home->rel_file('etc/' . lc($CLASS) . '.conf');
74 25         571 $app->plugin('Config');
75 25         57146 return $app;
76             }
77              
78             sub load_plugins {
79 25     25 1 62 my ($app) = @_;
80              
81 25   100     68 my $plugins = $app->config('plugins') || [];
82 25         283 foreach my $plugin (@$plugins) {
83 185 100       379725 $app->log->debug('Loading Plugin ' . (ref $plugin ? $plugin->{name} : $plugin));
84 185 100       9111 if (ref $plugin eq 'HASH') {
    100          
85 23         120 $app->plugin($plugin->{name} => $plugin->{config});
86             }
87             elsif ($plugin) {
88 161         456 $app->plugin($plugin);
89             }
90             }
91              
92 25         100 return $app;
93             }
94              
95             #load routes defined in ado.conf
96             sub load_routes {
97 121     121 1 333 my ($app, $config_routes) = @_;
98 121   100     356 $config_routes ||= $app->config('routes') || [];
      66        
99 121         644 my $routes = $app->routes;
100              
101             # Hide Ado::Control methods and attributes from router.
102 121         932 $routes->hide(
103             qw(
104             debug config require_format list_for_json
105             validate_input
106             )
107             );
108              
109 121         1217 foreach my $route (@$config_routes) {
110             my ($pattern, $over, $to, $via, $params) =
111 357         7675 ($route->{route}, $route->{over}, $route->{to}, $route->{via}, $route->{params});
112              
113 357 100       722 next unless $to;
114 333 100       1207 my $r = $params ? $routes->route($pattern, %$params) : $routes->route($pattern);
115              
116 333 100       61439 if ($over) {
117 27 100       129 if (ref $over eq 'HASH') { $r->over(%$over); }
  26         128  
118 1         4 else { $r->over($over); }
119             }
120 333 100       2167 if ($via) {
121 286         672 $r->via(@$via);
122             }
123 333 100       4587 $r->to(ref $to eq 'HASH' ? %$to : $to);
124             }
125              
126             # Default "/perldoc" page is Ado/Manual
127 121         3413 my $perldoc = $routes->lookup('perldocmodule');
128 121 100       2286 if ($perldoc) { $perldoc->to->{module} = 'Ado/Manual'; }
  119         263  
129              
130 121         1403 return $app;
131             }
132              
133             sub define_mime_types {
134 25     25 1 62 my ($app) = @_;
135 25   100     89 my $mimes = $app->config('types') || {}; #HASHREF
136 25         297 foreach my $mime (keys %$mimes) {
137              
138             # Add new MIME type or redefine any existing
139 46         1762 $app->types->type($mime => $mimes->{$mime});
140             }
141 25         294 return $app;
142             }
143              
144             1;
145              
146              
147             =pod
148              
149             =encoding utf8
150              
151             =head1 NAME
152              
153             Ado - a rapid active commotion (framework for web-projects on Mojolicious)
154              
155             =head1 SYNOPSIS
156              
157             require Mojolicious::Commands;
158             Mojolicious::Commands->start_app('Ado');
159              
160             =head1 DESCRIPTION
161              
162             L is a framework for web-projects based on L, written in the
163             L. This is the base
164             application class. Ado C L. For a more detailed description
165             on what Ado is and how to get started with Ado see B>.
166              
167             =head1 ATTRIBUTES
168              
169             Ado inherits all attributes from Mojolicious and implements the following new
170             ones.
171              
172             =head2 ado_home
173              
174             Returns an instance of L pointing to the base directory where
175             L is installed.
176              
177             ~$ ado eval 'say app->ado_home'
178             /home/berov/opt/public_dev/Ado
179              
180             =head2 CODENAME
181              
182             Returns the current C.
183              
184             =head2 home
185              
186             #/where/is/your_app/rootdir
187             $app->home;
188              
189             Returns the root directory into which $app is installed. The guessing order is
190             the following:
191              
192             =over
193              
194             =item 1. If C<$ENV{MOJO_HOME}> is defined, it is honored.
195              
196             =item 2. The upper directory of the directory in which the starting executable
197             C<$app-Emoniker> is found, e.g. C. This may happen to be the same
198             as L.
199              
200             =item 3. Fallback to L. This is the usual behavior of any L
201             application.
202              
203             =back
204              
205             =head2 sessions
206              
207             Access the L instance. Instantiates one of
208             L, L
209             or L depending on configuration and returns it.
210             By default (no configuration in C)
211             a L is returned.
212              
213              
214             =head1 METHODS
215              
216             Ado inherits all methods from Mojolicious and implements
217             the following new ones.
218              
219              
220             =head2 startup
221              
222             Sets various default paths like C, C, C.
223             Defines L as sha1_sum of C<$moniker.$mode. $home>. Sets
224             L and L to
225             L<${CLASS}::Control>. Sets L to
226             C<['Mojolicious::Plugin', "${CLASS}::Plugin"]>. Sets
227             L to C<${CLASS}::Command>. C<$CLASS> is
228             usually L. Then calls the following methods in the order they are listed.
229             Returns void.
230              
231             You can amend this behavior in the application configuration file.
232              
233             =head2 load_config
234              
235             Checks C<$ENV{MOJO_CONFIG}> and if not set sets it to
236             C<$app-Ehome/etc/ado.conf>. Loads L to do the
237             rest of the dirty work. Returns $app.
238              
239             =head2 load_plugins
240              
241             Does not accept any parameters. Loads plugins listed in
242             C<$config-E{plugins}>. C<$config-E{plugins}> is an C in
243             which each element is a C with keys C and C or string
244             representing the plugin name. The name of the plugin is expected to be string
245             that can be passed to L. The C values is another
246             C containing the configuration for the plugin. Plugins can be
247             L or L specific plugins. Every L::Foo must
248             inherit from L which C L. Of course
249             Mojolicious plugins can be used - we count on this. There are plenty of
250             examples on CPAN. Returns $app.
251              
252             =head2 load_routes
253              
254             Does not accept any parameters. Loads predefined routes from
255             C<$config-Eroutes>. C<$config-Eroutes> is an C in which each
256             element is a C with keys corresponding to a method name and value the
257             parameters that will be passed to the method. Currently we use the C
258             value to pass it to L,C value is the second
259             parameter to instantiate the route. C and C values are passed to the
260             newly created route. See L and
261             L for more.
262              
263             Returns $app.
264              
265             =head2 define_mime_types
266              
267             Defines any MIME types listed in C in C {...}>. Returns
268             $app.
269              
270             =head1 SPONSORS
271              
272             The original author.
273              
274             Become a sponsor and help make L the ERP for the enterprise!
275              
276             =head1 SEE ALSO
277              
278             L, L,
279             L,
280              
281             =head1 AUTHOR
282              
283             Красимир Беров (Krasimir Berov)
284              
285             =head1 COPYRIGHT AND LICENSE
286              
287             Copyright 2013-2016 Красимир Беров (Krasimir Berov).
288              
289             This program is free software, you can redistribute it and/or modify it under
290             the terms of the GNU Lesser General Public License v3 (LGPL-3.0). You may
291             copy, distribute and modify the software provided that modifications are open
292             source. However, software that includes the license may release under a
293             different license.
294              
295             See http://opensource.org/licenses/lgpl-3.0.html for more information.
296              
297             =cut