Nile/Dispatcher.pm

filter: Statementstmt 8.4% 9 / 107 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 20.0% 2 / 10 Podpod 0.0% 0 / 4 totaltotal 4.8% 11 / 228 CC 41 SCAR 63.5
CC
41
Cov
5%
dispatch_action
930.0
action
56.0
route
42.0
CRAP 571.1
↑ Summary Nile/HTTP/PSGI.pm »
1 # Copyright Infomation
2 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D.
4 # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com
5 # Email : mewsoft@cpan.org, support@mewsoft.com
6 # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved.
7 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 package Nile::Dispatcher;
9  
10 1 our $VERSION = '0.55';
11 1 our $AUTHORITY = 'cpan:MEWSOFT';
12  
13 =pod
14  
15 =encoding utf8
16  
17 =head1 NAME
18  
19 Nile::Dispatcher - Application action dispatcher.
20  
21 =head1 SYNOPSIS
22
23 # dispatch the default route or detect route from request
24 $app->dispatcher->dispatch;
25  
26 # dispatch specific route and request method
27 $app->dispatcher->dispatch($route, $request_method);
28 $app->dispatcher->dispatch('/accounts/register/create');
29 $app->dispatcher->dispatch('/accounts/register/save', 'POST');
30  
31 =head1 DESCRIPTION
32  
33 Nile::Dispatcher - Application action dispatcher.
34  
35 =cut
36  
37 1 ▶use Nile::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use Capture::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40 =head2 dispatch()
41
42 # dispatch the default route or detect route from request
43 $app->dispatcher->dispatch;
44  
45 # dispatch specific route and request method
46 $app->dispatcher->dispatch($route, $request_method);
47  
48 Process the action and send output to client.
49  
50 =cut
51  
52 0 ▶sub dispatch {
Subroutine dispatch: not called CC=1 SCAR=6.9
Pod undocumented
53  
54 0 my $self = shift;
55
56 0 my $content = $self->dispatch_action(@_);
57  
58 0 return $content;
59  
60 }
61 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62 =head2 dispatch_action()
63
64 # dispatch the default route or detect route from request
65 $content = $app->dispatcher->dispatch_action;
66  
67 # dispatch specific route and request method
68 $content = $app->dispatcher->dispatch_action($route, $request_method);
69  
70 Process the action and return output.
71  
72 =cut
73  
74 0 ▶sub dispatch_action {
Subroutine dispatch_action: not called CC=30 SCAR=68.4
Pod undocumented
75  
76 0 my ($self, $route, $request_method) = @_;
77
78 0 my $app = $self->app;
79  
80 0 ▶ $request_method ||= $app->request->request_method;
Condition0%0/3
$request_method ||= $app->request->request_method
l!l&&r!l&&!r
000
MC/DC0%0/2
$request_method ||= $app->request->request_method
$request_method $app->request->request_method
Truth table0%0/3
A ||= B
A= $request_methodB= $app->request->request_method
ABresult
1X1
011
000
81 0 ▶ $request_method ||= "ajax" if ($app->request->is_ajax);
Branch: 1 branch
BranchTrueFalseTotal
if $app->request->is_ajax 0 0 0
Condition0%0/2
$request_method ||= "ajax"
l!l
00
MC/DC0%0/1
$request_method ||= "ajax"
$request_method
Truth table0%0/2
A
A= $request_method
Aresult
11
00
82 0 ▶ $request_method ||= "*";
Condition0%0/2
$request_method ||= "*"
l!l
00
MC/DC0%0/1
$request_method ||= "*"
$request_method
Truth table0%0/2
A
A= $request_method
Aresult
11
00
83  
84 0 $route = $self->route($route);
85 0 ▶ $route ||= "";
Condition0%0/2
$route ||= ""
l!l
00
MC/DC0%0/1
$route ||= ""
$route
Truth table0%0/2
A
A= $route
Aresult
11
00
86  
87 # beginning slash. forum/topic => /forum/topic
88 0 ▶ $route = "/$route" if ($route !~ /^\//);
Branch: 1 branch
BranchTrueFalseTotal
unless $route =~ m[^/] 0 0 0
89  
90 #$match->{action}, $match->{args}, $match->{query}, $match->{uri}, $match->{code}, $match->{route}
91 0 my $match = $app->router->match($route, $request_method);
92 #$app->dump($match);
93
94 0 ▶ if ($match->{action}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match->{"action"}) 0 0 0
95 0 $route = $match->{action};
96 0 ▶ while (my($k, $v) = each %{$match->{args}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$match{"args"};}) 0 0 0
97 0 $app->request->add_param($k, $v);
98 }
99 }
100 #------------------------------------------------------
101 0 my ($content, @result);
102 0 undef $@;
103 #------------------------------------------------------
104 # inline actions. $app->action("get", "/home", sub {...});
105 # inline actions. $app->capture("get", "/home", sub {...});
106 0 ▶ if (ref($route) eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $route eq "CODE") 0 0 0
107 0 ▶ if (defined $match->{route}->{attributes} && $match->{route}->{attributes} =~ /capture/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match->{"route"}{"attributes"} and $match->{"route"}{"attributes"} =~ /capture/i) 0 0 0
Condition0%0/3
defined $match->{"route"}{"attributes"} and $match->{"route"}{"attributes"} =~ /capture/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $match->{"route"}{"attributes"} and $match->{"route"}{"attributes"} =~ /capture/i
defined $match->{"route"}{"attributes"} $match->{"route"}{"attributes"} =~ /capture/i
Truth table0%0/3
A and B
A= defined $match->{"route"}{"attributes"}B= $match->{"route"}{"attributes"} =~ /capture/i
ABresult
0X0
100
111
108 # run the action and capture output of print statements
109 0 ▶ ($content, @result) = Capture::Tiny::capture_merged {eval {$route->($self->app)}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 }
111 0 ▶ if (defined $match->{route}->{attributes} && $match->{route}->{attributes} =~ /command/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match->{'route'}{'attributes'} and $match->{'route'}{'attributes'} =~ /command/i) { } 0 0 0
Condition0%0/3
defined $match->{"route"}{"attributes"} and $match->{"route"}{"attributes"} =~ /command/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $match->{"route"}{"attributes"} and $match->{"route"}{"attributes"} =~ /command/i
defined $match->{"route"}{"attributes"} $match->{"route"}{"attributes"} =~ /command/i
Truth table0%0/3
A and B
A= defined $match->{"route"}{"attributes"}B= $match->{"route"}{"attributes"} =~ /command/i
ABresult
0X0
100
111
112 # run the action and capture output of print statements
113 0 ▶ ($content, @result) = Capture::Tiny::capture_merged {eval {$route->($self->app)}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 0 $content .= join "", @result;
115 }
116 else {
117 # run the action and get the returned content
118 0 $content = eval {$route->($self->app)};
119 }
120  
121 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
122 0 $app->abort("Dispatcher error. Inline action dispatcher error for route '$route'.\n\n$@");
123 }
124  
125 0 return $content;
126 }
127 #------------------------------------------------------
128 # if route is '/' then use the default route
129 0 ▶ if (!$route || $route eq "/") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $route or $route eq "/") 0 0 0
Condition0%0/3
not $route or $route eq "/"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $route or $route eq "/"
not $route $route eq "/"
Truth table0%0/3
not(A) or B
A= not $routeB= $route eq "/"
ABresult
1X1
011
000
130 0 $route = $app->var->get("default_route");
131 }
132  
133 0 ▶ $route ||= $app->abort("Dispatcher error. No route defined.");
Condition0%0/3
$route ||= $app->abort("Dispatcher error. No route defined.")
l!l&&r!l&&!r
000
MC/DC0%0/2
$route ||= $app->abort("Dispatcher error. No route defined.")
$route $app->abort("Dispatcher error. No route defined.")
Truth table0%0/3
A ||= B
A= $routeB= $app->abort("Dispatcher error. No route defined.")
ABresult
1X1
011
000
134
135 0 my ($module, $controller, $action) = $self->action($route);
136  
137 0 my $class = "Nile::Module::${module}::${controller}";
138
139 0 undef $@;
140 0 eval "use $class;";
141  
142 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
143 0 $app->abort("Dispatcher error. Module error for route '$route' class '$class'.\n\n$@");
144 }
145
146 0 my $object = $class->new();
147  
148 0 ▶ if (!$object->can($action)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($object->can($action)) 0 0 0
149 # try /Accounts => Accounts/Accounts/Accounts
150 0 ▶ if (($module eq $controller) && ($action eq "index")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module eq $controller and $action eq 'index') { } 0 0 0
Condition0%0/3
$module eq $controller and $action eq "index"
!ll&&!rl&&r
000
MC/DC0%0/2
$module eq $controller and $action eq "index"
$module eq $controller $action eq "index"
Truth table0%0/3
A and B
A= $module eq $controllerB= $action eq "index"
ABresult
0X0
100
111
151 # try /Accounts => Accounts/Accounts/Accounts
152 0 ▶ if ($object->can($module)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($object->can($module)) { } 0 0 0
elsif ($object->can(lc $module)) { } 0 0 0
153 0 $action = $module;
154 }
155 # try /Accounts => Accounts/Accounts/accounts
156 elsif ($object->can(lc($module))) {
157 0 $action = lc($module);
158 }
159 }
160 else {
161 0 $app->abort("Dispatcher error. Module '$class' action '$action' does not exist.");
162 }
163 }
164
165 0 my $meta = $object->meta;
166
167 0 my $attrs = $meta->get_method($action)->attributes;
168 #$app->dump($attrs);
169
170 # sub home: Action Capture Public {...}
171 0 ▶ if (!grep(/^(action|capture|command|public)$/i, @$attrs)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^(action|capture|command|public)$/i, @$attrs) 0 0 0
172 0 $app->abort("Dispatcher error. Module '$class' method '$action' is not marked as 'Action' or 'Capture'.");
173 }
174  
175 #Methods: HEAD, POST, GET, PUT, DELETE, PATCH, [ajax]
176  
177 0 ▶ if ($request_method ne "*" && !grep(/^$request_method$/i, @$attrs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request_method ne "*" and not grep(/^$request_method$/i, @$attrs)) 0 0 0
Condition0%0/3
$request_method ne "*" and not grep(/^$request_method$/i, @$attrs)
!ll&&!rl&&r
000
MC/DC0%0/2
$request_method ne "*" and not grep(/^$request_method$/i, @$attrs)
$request_method ne "*" not grep(/^$request_method$/i, @$attrs)
Truth table0%0/3
A and not(B)
A= $request_method ne "*"B= not grep(/^$request_method$/i, @$attrs)
ABresult
0X0
100
111
178 0 $app->abort("Dispatcher error. Module '$class' action '$action' request method '$request_method' is not allowed.");
179 }
180
181 # add method "me" or one of its alt
182 0 $app->add_object_context($object, $meta);
183
184 0 undef $@;
185  
186 0 ▶ if (grep(/^(capture)$/i, @$attrs)) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep /^(capture)$/i, @$attrs) { } 0 0 0
elsif (grep /^(command)$/i, @$attrs) { } 0 0 0
187 # run the action and capture output of print statements. sub home: Capture {...}
188 0 ▶ ($content, @result) = Capture::Tiny::capture_merged {eval {$object->$action($self->app)}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 }
190 elsif (grep(/^(command)$/i, @$attrs)) {
191 # run the action and capture output of print statements and return value. sub home: Command {...}
192 0 ▶ ($content, @result) = Capture::Tiny::capture_merged {eval {$object->$action($self->app)}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
193 0 $content .= join "", @result;
194 }
195 else {
196 # run the action and get the returned content sub home: Action {...}
197 0 $content = eval {$object->$action($self->app)};
198 }
199  
200 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
201 0 $content = "Module error: Module '$class' method '$action'. $@\n$content\n";
202 }
203  
204 0 return $content;
205 }
206 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
207 =head2 action()
208
209 my ($module, $controller, $action) = $app->dispatcher->action($route);
210 #route /module/controller/action returns (Module, Controller, action)
211 #route /module/action returns (Module, Module, action)
212 #route /module returns (Module, Module, index)
213  
214 Find the action module, controller and method name from the provided route.
215  
216 =cut
217  
218 0 ▶sub action {
Subroutine action: not called CC=7 SCAR=40.3
Pod undocumented
219  
220 0 my ($self, $route) = @_;
221  
222 0 ▶ $route || return;
Branch: 1 branch
BranchTrueFalseTotal
$route or return 0 0 0
223 0 my ($module, $controller, $action);
224
225 0 $route =~ s/^\/+//;
226
227 0 my @parts = split(/\//, $route);
228  
229 0 ▶ if (scalar @parts == 3) {
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @parts == 3) { } 0 0 0
elsif (scalar @parts == 2) { } 0 0 0
elsif (scalar @parts == 1) { } 0 0 0
230 0 ($module, $controller, $action) = @parts;
231 }
232 elsif (scalar @parts == 2) {
233 0 $module = $parts[0];
234 0 $controller = $parts[0];
235 0 $action = $parts[1];
236 }
237 elsif (scalar @parts == 1) {
238 0 $module = $parts[0];
239 0 $controller = $parts[0];
240 0 $action = "index";
241 }
242
243 0 ▶ $module ||= "";
Condition0%0/2
$module ||= ""
l!l
00
MC/DC0%0/1
$module ||= ""
$module
Truth table0%0/2
A
A= $module
Aresult
11
00
244 0 ▶ $controller ||= "";
Condition0%0/2
$controller ||= ""
l!l
00
MC/DC0%0/1
$controller ||= ""
$controller
Truth table0%0/2
A
A= $controller
Aresult
11
00
245
246 0 return (ucfirst($module), ucfirst($controller), $action);
247 }
248 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
249 =head2 route()
250
251 my $route = $app->dispatcher->route($route);
252
253 Detects the current request path if not provided from the request params named as
254 'action', 'route', or 'cmd' in the post or get methods:
255
256 # uri route
257 /blog/?action=register
258
259 # form route
260 <input type="hidden" name="action" value="register" />
261  
262 If not found, it will try to detect the route from the request uri after the path part
263
264 # assuming application path is /blog, so /register will be the route
265 /blog/register
266  
267 =cut
268  
269 0 ▶sub route {
Subroutine route: not called CC=6 SCAR=37.4
Pod undocumented
270 0 my ($self, $route) = @_;
271
272 0 my $app = $self->app;
273  
274 # if no route, try to find route from the request param named by action_name
275 0 ▶ if (!$route) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($route) 0 0 0
276 # allow multiple names separated with commas, i.e. 'action', 'action,route,cmd'.
277 0 my @action_name = split(/\,/, $app->var->get("action_name"));
278 0 foreach (@action_name) {
279 0 ▶ last if ($route = $app->request->param($_));
Branch: 1 branch
BranchTrueFalseTotal
if $route = $app->request->param($_) 0 0 0
280 }
281 }
282
283 # if no route, get the route from the query string in the REQUEST_URI
284 0 ▶ $route ||= $app->request->url_path;
Condition0%0/3
$route ||= $app->request->url_path
l!l&&r!l&&!r
000
MC/DC0%0/2
$route ||= $app->request->url_path
$route $app->request->url_path
Truth table0%0/3
A ||= B
A= $routeB= $app->request->url_path
ABresult
1X1
011
000
285
286 0 ▶ if ($route) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route) 0 0 0
287 0 $route =~ s!^/!!g;
288 0 $route =~ s!/$!!g;
289 }
290  
291 0 return $route;
292 }
293 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
294  
295 =pod
296  
297 =head1 Bugs
298  
299 This project is available on github at L<https://github.com/mewsoft/Nile>.
300  
301 =head1 HOMEPAGE
302  
303 Please visit the project's homepage at L<https://metacpan.org/release/Nile>.
304  
305 =head1 SOURCE
306  
307 Source repository is at L<https://github.com/mewsoft/Nile>.
308  
309 =head1 SEE ALSO
310  
311 See L<Nile> for details about the complete framework.
312  
313 =head1 AUTHOR
314  
315 Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org>
316 Website: http://www.mewsoft.com
317  
318 =head1 COPYRIGHT AND LICENSE
319  
320 Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com,
321 L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com>
322  
323 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
324  
325 =cut
326  
327 1 1;
↑ Summary Nile/HTTP/PSGI.pm »