File Coverage

blib/lib/Mojolicious/Plugin/ConfigRoutes.pm
Criterion Covered Total %
statement 6 43 13.9
branch 0 32 0.0
condition 0 6 0.0
subroutine 2 3 66.6
pod 1 1 100.0
total 9 85 10.5


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::ConfigRoutes;
2 1     1   30816 use Mojo::Base 'Mojolicious::Plugin';
  1         12600  
  1         9  
3              
4 1     1   2842 use File::Spec::Functions 'file_name_is_absolute';
  1         1074  
  1         1042  
5              
6             our $VERSION=0.04;
7              
8             sub register {
9 0     0 1   my ($self, $app, $conf) = @_;
10 0           my $r = $app->routes;# Router
11            
12 0           my @routes;
13 0 0         if (ref($conf) eq 'HASH') {
    0          
14 0 0         if ($conf->{'file'}) {
15 0 0         $conf->{file} = $app->home->rel_file($conf->{file}) unless file_name_is_absolute $conf->{file};
16 0           my @do;
17 0 0         if (-e $conf->{file}) {@do = do $conf->{file};}
  0            
  0            
18             else {die qq{Config routes file [$conf->{file}] missing, maybe you need to create it?\n};}
19 0 0         my $do = {@do} if @do > 1; # вернулся список, тогда ключ=>значение
20 0 0         $do = $do[0] if @do == 1;
21            
22 0 0         if (ref($do) eq 'HASH') {
    0          
23 0 0 0       push @{$conf->{namespaces}}, @{$do->{namespaces}} if $do->{namespaces} && ref($do->{namespaces}) eq 'ARRAY';# общее переключение папка с контроллерами маршрутов
  0            
  0            
24 0 0 0       push @{$conf->{routes}}, @{$do->{routes}} if $do->{routes} && ref($do->{routes}) eq 'ARRAY';
  0            
  0            
25             } elsif (ref($do) eq 'ARRAY') {# просто маршруты [...]
26 0           push @{$conf->{routes}}, @$do;
  0            
27             }
28             }
29 0 0         if ($conf->{routes}) {
30 0           push @routes, @{$conf->{routes}};
  0            
31             }
32 0 0         push @{$r->namespaces}, @{$conf->{namespaces}} if $conf->{namespaces};
  0            
  0            
33            
34             } elsif (ref($conf) eq 'ARRAY') {
35 0           push @routes, @$conf;
36             }
37            
38            
39 0           for my $t (@routes) {
40 0           my $_r = $r;# будет цепочка объектов для маршрута
41 0           for( my $i = 0; $i < @$t; $i += 2 ) {
42 0           my $m = $t->[$i]; # method
43 0           my $a = $t->[$i+1];# args to meth
44 0 0         if (my $meth = $_r->can($m)) {
45             #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! НЕЛЬЗЯ ИСПОЛЬЗОВАТЬ $_r!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
46             #~ $app->log->debug("Apply method for $_r->$m(", (map {"\t'".join("' => '", grep {defined;} @$a[($_*2)..($_*2+1)])."',"} (0..($#$a/2))), ")",);#$app->dumper($a),
47 0 0         $_r = $_r->$meth(ref($a) ? (ref($a) eq 'ARRAY' ? @$a : %$a) : $a);# apply method
    0          
48             } else {
49 0           $app->log->warn("Can't method [$m] for [$_r]",);#, will be applied as attr
50             }
51             }
52             }
53            
54 0           $app->log->debug("Маршруты: ", $app->dumper($r));
55             }
56              
57              
58             =encoding utf8
59              
60             =head1 ПРИВЕТСТВИЕ SALUTE
61            
62             Доброго всем! Доброго здоровья! Доброго духа!
63            
64             Hello all! Nice health! Good thinks!
65              
66             ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !
67              
68             =head1 NAME
69              
70             Mojolicious::Plugin::ConfigRoutes - is a Perl-ish configuration of routes plugin.
71              
72             =head1 VERSION
73              
74             Version 0.03
75              
76             =head1 SYNOPSIS
77              
78              
79             $app->plugin(ConfigRoutes =>{...});
80             $app->plugin(ConfigRoutes =>[[...], [...], ..., [...],]);
81             ...
82              
83             This plugin can launch many times.
84              
85             Array ref of array refs is arranged description of routes. The format is described on option routes below.
86              
87             Hash ref has the following options.
88            
89             =head1 OPTIONS
90              
91             L supports the following options.
92              
93             =head2 file
94              
95             $app->plugin(ConfigRoutes =>{file => 'ConfigRoutes.pm'});
96              
97             File name or full path to configuration file that do (perldoc -f do) and must create list or array ref or hash ref.
98              
99             =over 4
100              
101             =item * Returned list would be consider as pairs of key=>value. Keys are namespaces and routes. Values are arranged array refs, see options below.
102              
103             =item * Returned hash ref with pairs of key=>value. Keys are namespaces and routes. Values are arranged array refs, see options below.
104              
105             =item * Returned array ref would be consider as arranged routes, see format on option routes below.
106              
107             =back
108              
109             =head2 routes
110              
111             Value is array ref of the arranged routes [[],[],...[],]:
112              
113             $app->plugin(ConfigRoutes =>{routes => [[ => , => , ... ],...]});
114              
115             Methods of L as keys in one route must be strongly arranged pairs with their values in order to apply to $app->routes object. For example:
116              
117             # the standard in startup
118             $r->bridge('/foo')->to('foo#foo')->route('/bar')->to(controller=>'bar', action=>'bar',...)->...;
119             $r->;
120             ...
121             # becomes structure
122             $app->plugin(ConfigRoutes =>{routes => [[bridge=>'/foo', to=>'foo#for', route=>'/bar', to=>{controller=>'bar', action=>'bar',...}, ...], [], ...]);
123            
124              
125             Values of keys(methods) within route can be $scalar or [array ref] or {hash ref}. Array ref and hash ref are treated as lists when apply to their methods.
126              
127             =head2 namespaces
128              
129             Value is array ref of L
130              
131             $app->plugin(ConfigRoutes =>{namespaces => ['Foo::Bar::Controller']});
132              
133              
134             =head1 NOTE
135              
136             If pointed and options together then aplly first and routes after.
137              
138             The and namespaces from are similar.
139              
140             =head1 EXAMPLES of config file
141              
142             =head2 List or hash ref allow options
143              
144             (# or {
145             namespaces=>['MyFoo1', 'MyFoo2'],
146             routes => [
147             [
148             bridge=>'/my',
149             to=>'Auth#user',
150             route=>'/profile/:action/:user',
151             to=>{controller=>'profile',},
152             name=>'UserProfile',
153             ],
154             [,
155             get=>'...',
156             to=>{cb=>sub{...},},
157             ],
158             ],
159             );
160              
161             =head2 Array ref - only routes
162              
163             [
164             [
165             bridge=>'/my',
166             to=>'Auth#user',
167             route=>'/profile/:action/:user',
168             to=>{controller=>'profile',},
169             name=>'UserProfile',
170             ],
171             [,
172             get=>'/',
173             to=>{cb=>sub{...},},
174             ],
175             ];
176              
177             =head1 AUTHOR
178              
179             Mikhail Che, C<< >>
180              
181             =head1 BUGS
182              
183             Please report any bugs or feature requests to C, or through
184             the web interface at L. I will be notified, and then you'll
185             automatically be notified of progress on your bug as I make changes.
186              
187              
188             =head1 SUPPORT
189              
190             You can find documentation for this module with the perldoc command.
191              
192             perldoc Mojolicious::Plugin::ConfigRoutes
193              
194              
195             You can also look for information at:
196              
197             =over 4
198              
199             =item * RT: CPAN's request tracker (report bugs here)
200              
201             L
202              
203             =item * AnnoCPAN: Annotated CPAN documentation
204              
205             L
206              
207             =item * CPAN Ratings
208              
209             L
210              
211             =item * Search CPAN
212              
213             L
214              
215             =back
216              
217              
218             =head1 ACKNOWLEDGEMENTS
219              
220              
221             =head1 LICENSE AND COPYRIGHT
222              
223             Copyright 2013 Mikhail Che.
224              
225             This program is free software; you can redistribute it and/or modify it
226             under the terms of the the Artistic License (2.0). You may obtain a
227             copy of the full license at:
228              
229             L
230              
231             Any use, modification, and distribution of the Standard or Modified
232             Versions is governed by this Artistic License. By using, modifying or
233             distributing the Package, you accept this license. Do not use, modify,
234             or distribute the Package, if you do not accept this license.
235              
236             If your Modified Version has been derived from a Modified Version made
237             by someone other than you, you are nevertheless required to ensure that
238             your Modified Version complies with the requirements of this license.
239              
240             This license does not grant you the right to use any trademark, service
241             mark, tradename, or logo of the Copyright Holder.
242              
243             This license includes the non-exclusive, worldwide, free-of-charge
244             patent license to make, have made, use, offer to sell, sell, import and
245             otherwise transfer the Package with respect to any patent claims
246             licensable by the Copyright Holder that are necessarily infringed by the
247             Package. If you institute patent litigation (including a cross-claim or
248             counterclaim) against any party alleging that the Package constitutes
249             direct or contributory patent infringement, then this Artistic License
250             to you shall terminate on the date that such litigation is filed.
251              
252             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
253             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
254             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
255             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
256             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
257             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
258             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
259             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
260              
261              
262             =cut
263              
264              
265              
266             1;