Jifty/Dispatcher.pm

filter: Statementstmt 14.1% 54 / 381 Branchbran 0.0% 0 / 120 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 19.5% 16 / 82 Podpod 100% 33 / 33 totaltotal 15.2% 103 / 676 CC 86 SCAR 35.2
CC
86
Cov
10%
_match
156.0
_compile_condition
110.0
_do_dispatch
56.0
CRAP 33.6
« Jifty/Web/Form/Field/OrderedList.pm ↑ Summary Jifty/Continuation.pm »
1 package Jifty::Dispatcher;
2 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 113 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 113 ▶use Jifty::YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 113 ▶use base qw/Exporter Jifty::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 113 ▶use Carp::Clan; # croak
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Jifty::Dispatcher - The Jifty Dispatcher
12  
13 =head1 SYNOPSIS
14  
15 In B<MyApp::Dispatcher>:
16  
17 package MyApp::Dispatcher;
18 use Jifty::Dispatcher -base;
19  
20 under ['blog', 'wiki'] => [
21 run {
22 default model => "MyApp::Model::\u$1"
23 },
24 on PUT 'entries/*' => run {
25 set entry_id => $1;
26 show '/display/entry';
27 },
28 on '*/*' => run {
29 my ($page, $op) = ($1, $2);
30 my $item = get('model')->load($page) or next_rule;
31  
32 set item => $item;
33 set page => $page;
34 set op => $op;
35  
36 show "/display/$op";
37 },
38 on '*' => run { dispatch "$1/view" },
39 on '' => show '/display/list',
40 ];
41 under qr{logs/(\d+)} => [
42 when { $1 > 100 } => show '/error',
43 set model => 'MyApp::Model::Log',
44 run { dispatch "/wiki/LogPage-$1" },
45 ];
46 # ... more rules ...
47  
48 =head1 DESCRIPTION
49  
50 C<Jifty::Dispatcher> takes requests for pages, walks through a
51 dispatch table, possibly running code or transforming the request
52 before finally handing off control to the templating system to display
53 the page the user requested or whatever else the system has decided to
54 display instead.
55  
56 Generally, this is B<not> the place to be performing model and user
57 specific access control checks or updating your database based on what
58 the user has sent in. You want to do that in your model
59 classes. (Well, I<we> want you to do that, but you're free to ignore
60 our advice).
61  
62 The Dispatcher runs rules in several stages:
63  
64 =over
65  
66 =item before
67  
68 B<before> rules are run before Jifty evaluates actions. They're the
69 perfect place to enable or disable L<Jifty::Action>s using
70 L<Jifty::API/allow> and L<Jifty::API/deny> or to completely disallow
71 user access to private I<component> templates such as the F<_elements>
72 directory in a default Jifty application. They're also the right way
73 to enable L<Jifty::LetMe> actions.
74  
75 You can entirely stop processing with the C<redirect>, C<tangent> and
76 C<abort> directives, though L</after> rules will still run.
77  
78 =item on
79  
80 L<on> rules are run after Jifty evaluates actions, so they have full
81 access to the results actions users have performed. They're the right
82 place to set up view-specific objects or load up values for your
83 templates.
84  
85 Dispatcher directives are evaluated in order until we get to either a
86 C<show>, C<redirect>, C<tangent> or C<abort>.
87  
88 =item after
89  
90 L<after> rules let you clean up after rendering your page. Delete your
91 cache files, write your transaction logs, whatever.
92  
93 At this point, it's too late to C<show>, C<redirect>, C<tangent> or C<abort>
94 page display.
95  
96 =back
97  
98 C<Jifty::Dispatcher> is intended to replace all the F<autohandler>,
99 F<dhandler> and C<index.html> boilerplate code commonly found in Mason
100 applications, but there's nothing stopping you from using those
101 features in your application when they're more convenient.
102  
103 Each directive's code block runs in its own scope, but all share a
104 common C<$Dispatcher> object.
105  
106 =cut
107  
108 =head1 Plugins and rule ordering
109  
110 By default, L<Jifty::Plugin> dispatcher rules are added in the order
111 they are specified in the application's configuration file; that is,
112 after all the plugin dispatchers have run in order, then the
113 application's dispatcher runs. It is possible to specify rules which
114 should be reordered with respect to this rule, however. This is done
115 by using a variant on the C<before> and C<after> syntax:
116  
117 before plugin NAME =>
118 RULE(S);
119
120 after plugin NAME =>
121 RULE(S);
122  
123 after app,
124 RULE(S)
125  
126 C<NAME> may either be a string, which must match the plugin name
127 exactly, or a regular expression, which is matched against the plugin
128 name. The rule will be placed at the first boundary that it matches --
129 that is, given a C<before plugin qr/^Jifty::Plugin::Auth::/> and both
130 a C<Jifty::Plugin::Auth::Basic> and a C<Jifty::Plugin::Auth::Complex>,
131 the rules will be placed before the first.
132  
133 C<after app> inserts the following C<RULES> after the application's
134 dispatcher rules, and is identical to, but hopefully clearer than,
135 C<< after plugin Jifty => RULES >>.
136  
137 C<RULES> may either be a single C<before>, C<on>, C<under>, or
138 C<after> rule to change the ordering of, or an array reference of
139 rules to reorder.
140  
141 =cut
142  
143 =head1 Data your dispatch routines has access to
144  
145 =head2 request
146  
147 The current L<Jifty::Request> object.
148  
149 =head2 $Dispatcher
150  
151 The current dispatcher object.
152  
153 =head2 get $arg
154  
155 Return the argument value.
156  
157 =head1 Things your dispatch routine might do
158  
159 =head2 under $match => $rule
160  
161 Match against the current requested path. If matched, set the current
162 context to the directory and process the rule.
163  
164 The C<$rule> may be an array reference of more rules, a code reference, a
165 method name of your dispatcher class, or a fully qualified subroutine name.
166  
167 All wildcards in the C<$match> string becomes capturing regex patterns. You
168 can also pass in an array reference of matches, or a regex pattern.
169  
170 The C<$match> string may be qualified with a HTTP method name or protocol, such as
171  
172 =over
173  
174 =item GET
175  
176 =item POST
177  
178 =item PUT
179  
180 =item OPTIONS
181  
182 =item DELETE
183  
184 =item HEAD
185  
186 =item HTTPS
187  
188 =item HTTP
189  
190 =back
191  
192 =head2 on $match => $rule
193  
194 Like C<under>, except it has to match the whole path instead of just the prefix.
195 Does not set current directory context for its rules.
196  
197 =head2 before $match => $rule
198  
199 Just like C<on>, except it runs I<before> actions are evaluated.
200  
201 =head2 after $match => $rule
202  
203 Just like C<on>, except it runs I<after> the page is rendered.
204  
205  
206 =head2 when {...} => $rule
207  
208 Like C<under>, except using an user-supplied test condition. You can stick
209 any Perl you want inside the {...}; it's just an anonymous subroutine.
210  
211 =head2 run {...}
212  
213 Run a block of code unconditionally; all rules are allowed inside a C<run>
214 block, as well as user code. You can think of the {...} as an anonymous
215 subroutine.
216  
217 =head2 stream {...}
218  
219 Run a block of code unconditionally, which should return a coderef
220 that is a PSGI streamy response.
221  
222 =head2 set $arg => $val
223  
224 Adds an argument to what we're passing to our template, overriding
225 any value the user sent or we've already set.
226  
227 =head2 default $arg => $val
228  
229 Adds an argument to what we're passing to our template,
230 but only if it is not defined currently.
231  
232 =head2 del $arg
233  
234 Deletes an argument we were passing to our template.
235  
236 =head2 show $component
237  
238 Display the presentation component. If not specified, use the
239 request path as the default page.
240  
241 =head2 dispatch $path
242  
243 Dispatch again using $path as the request path, preserving args.
244  
245 =head2 next_rule
246  
247 Break out from the current C<run> block and go on the next rule.
248  
249 =head2 last_rule
250  
251 Break out from the current C<run> block and stop running rules in this stage.
252  
253 =head2 abort $code
254  
255 Abort the request; this skips straight to the cleanup stage.
256  
257 If C<$code> is specified, it's used as the HTTP status code.
258  
259 =head2 redirect $uri
260  
261 Redirect to another URI.
262  
263 =head2 tangent $uri
264  
265 Take a continuation here, and tangent to another URI.
266  
267 =head2 plugin
268  
269 =head2 app
270  
271 See L</Plugins and rule ordering>, above.
272  
273 =cut
274  
275 113 our @EXPORT = qw<
276 under run when set del default
277  
278 before on after
279  
280 show dispatch abort redirect tangent stream
281  
282 GET POST PUT HEAD DELETE OPTIONS
283  
284 HTTPS HTTP
285  
286 plugin app
287  
288 get next_rule last_rule
289  
290 already_run
291  
292 $Dispatcher
293 >;
294  
295 113 our $Dispatcher;
296 113 our $Request;
297  
298 0 ▶sub request { $Request }
Subroutine request: not called CC=1 SCAR=6.9
Pod documented
299 sub _ret (@);
300 0 ▶sub under ($$@) { _ret @_ } # partial match at beginning of path component
Subroutine under: not called CC=1 SCAR=6.9
Pod documented
301 0 ▶sub before ($$@) { _ret @_ } # exact match on the path component
Subroutine before: not called CC=1 SCAR=6.9
Pod documented
302 0 ▶sub on ($$@) { _ret @_ } # exact match on the path component
Subroutine on: not called CC=1 SCAR=6.9
Pod documented
303 0 ▶sub after ($$@) { _ret @_ } # exact match on the path component
Subroutine after: not called CC=1 SCAR=6.9
Pod documented
304 0 ▶sub when (&@) { _ret @_ } # exact match on the path component
Subroutine when: not called CC=1 SCAR=6.9
Pod documented
305 0 ▶sub run (&@) { _ret @_ } # execute a block of code
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
306 0 ▶sub stream (&@) { _ret @_ } # web return a PSGI-streamy response
Subroutine stream: not called CC=1 SCAR=6.9
Pod documented
307 0 ▶sub show (;$@) { _ret @_ } # render a page
Subroutine show: not called CC=1 SCAR=6.9
Pod documented
308 0 ▶sub dispatch ($@) { _ret @_ } # run dispatch again with another URI
Subroutine dispatch: not called CC=1 SCAR=6.9
Pod documented
309 0 ▶sub redirect ($@) { _ret @_ } # web redirect
Subroutine redirect: not called CC=1 SCAR=6.9
Pod documented
310 0 ▶sub tangent ($@) { _ret @_ } # web tangent
Subroutine tangent: not called CC=1 SCAR=6.9
Pod documented
311 0 ▶sub abort (;$@) { _ret @_ } # abort request
Subroutine abort: not called CC=1 SCAR=6.9
Pod documented
312 0 ▶sub default ($$@) { _ret @_ } # set parameter if it's not yet set
Subroutine default: not called CC=1 SCAR=6.9
Pod documented
313 0 ▶sub set ($$@) { _ret @_ } # set parameter
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
314 0 ▶sub del ($@) { _ret @_ } # remove parameter
Subroutine del: not called CC=1 SCAR=6.9
Pod documented
315 0 ▶sub get ($) {
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
316 0 my $val = $Request->template_argument( $_[0] );
317 0 ▶ return $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
318 0 return $Request->argument( $_[0] );
319 }
320  
321 sub _qualify ($@);
322 0 ▶sub GET ($) { _qualify method => @_ }
Subroutine GET: not called CC=1 SCAR=6.9
Pod documented
323 0 ▶sub POST ($) { _qualify method => @_ }
Subroutine POST: not called CC=1 SCAR=6.9
Pod documented
324 0 ▶sub PUT ($) { _qualify method => @_ }
Subroutine PUT: not called CC=1 SCAR=6.9
Pod documented
325 0 ▶sub HEAD ($) { _qualify method => @_ }
Subroutine HEAD: not called CC=1 SCAR=6.9
Pod documented
326 0 ▶sub DELETE ($) { _qualify method => @_ }
Subroutine DELETE: not called CC=1 SCAR=6.9
327 0 ▶sub OPTIONS ($) { _qualify method => @_ }
Subroutine OPTIONS: not called CC=1 SCAR=6.9
Pod documented
328  
329 0 ▶sub HTTPS ($) { _qualify https => @_ }
Subroutine HTTPS: not called CC=1 SCAR=6.9
Pod documented
330 0 ▶sub HTTP ($) { _qualify http => @_ }
Subroutine HTTP: not called CC=1 SCAR=6.9
Pod documented
331  
332 0 ▶sub plugin ($) { return { plugin => @_ } }
Subroutine plugin: not called CC=1 SCAR=6.9
Pod documented
333 0 ▶sub app () { return { plugin => 'Jifty' } }
Subroutine app: not called CC=1 SCAR=6.9
Pod documented
334  
335 113 our $CURRENT_STAGE;
336  
337 =head2 import
338  
339 Jifty::Dispatcher is an L<Exporter>, that is, part of its role is to
340 blast a bunch of symbols into another package. In this case, that
341 other package is the dispatcher for your application.
342  
343 You never call import directly. Just:
344  
345 use Jifty::Dispatcher -base;
346  
347 in C<MyApp::Dispatcher>
348  
349 =cut
350  
351 0 ▶sub import {
Subroutine import: not called CC=3 SCAR=18.4
352 0 my $class = shift;
353 0 my $pkg = caller;
354 0 my @args = grep { !/^-[Bb]ase/ } @_;
355  
356 113 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
357 113 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
358 0 for (qw(RULES_RUN RULES_SETUP RULES_CLEANUP RULES_DEFERRED)) {
359 0 @{ $pkg . '::' . $_ } = ();
360 }
361 0 ▶ if ( @args != @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args != @_) 0 0 0
362  
363 # User said "-base", let's push ourselves into their @ISA.
364 0 push @{ $pkg . '::ISA' }, $class;
365  
366 # Turn on strict and warnings for them too, a la Moose
367 0 strict->import;
368 0 warnings->import;
369 }
370  
371 0 $class->export_to_level( 1, @args );
372 }
373  
374 ###################################################
375 # Magically figure out the arity based on caller info.
376 0 ▶sub _ret (@) {
Subroutine _ret: not called CC=6 SCAR=37.4
377 0 my $pkg = caller(1);
378 0 my $sub = ( caller(1) )[3];
379 0 my $proto = prototype($sub);
380 0 my $op = $sub;
381  
382 0 $proto =~ tr/@;//d;
383 0 ▶ if ( my $idx = rindex( $op, '::' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $idx = rindex($op, "::")) 0 0 0
384 0 $op = substr( $op, $idx + 2 );
385 }
386  
387 0 ▶ if ($Dispatcher) {
Branch: 3 branches
BranchTrueFalseTotal
if ($Jifty::Dispatcher::Dispatcher) { } 0 0 0
elsif (wantarray) { } 0 0 0
elsif (defined wantarray) { } 0 0 0
388  
389 # We are under an operation -- carry the rule forward
390 0 foreach my $rule ( [ $op => splice( @_, 0, length($proto) ) ], @_ ) {
391 0 $Dispatcher->_handle_rule($rule);
392 }
393 } elsif (wantarray) {
394 0 ( [ $op => splice( @_, 0, length($proto) ) ], @_ );
395 } elsif ( defined wantarray ) {
396 0 [ [ $op => splice( @_, 0, length($proto) ) ], @_ ];
397 } else {
398 0 _push_rule($pkg, [ $op => splice( @_, 0, length($proto) ) ] );
399 }
400 }
401  
402 0 ▶sub _push_rule($$) {
Subroutine _push_rule: not called CC=8 SCAR=40.1
403 0 my($pkg, $rule) = @_;
404 0 my $op = $rule->[0];
405 0 my $ruleset;
406 0 ▶ if ( ($op eq "before" or $op eq "after") and ref $rule->[1] and ref $rule->[1] eq 'HASH' and $rule->[1]{plugin} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq 'before' || $op eq 'after' and ref $rule->[1] and ref $rule->[1] eq 'HASH' and $rule->[1]{'plugin'}) { } 0 0 0
elsif ($op eq 'before') { } 0 0 0
elsif ($op eq 'after') { } 0 0 0
Condition0%0/12
$op eq "before" || $op eq "after" and ref $rule->[1] and ref $rule->[1] eq "HASH" and $rule->[1]{"plugin"}
!ll&&!rl&&r
000
$op eq "before" || $op eq "after" and ref $rule->[1] and ref $rule->[1] eq "HASH"
!ll&&!rl&&r
000
$op eq "before" || $op eq "after" and ref $rule->[1]
!ll&&!rl&&r
000
$op eq "before" or $op eq "after"
l!l&&r!l&&!r
000
MC/DC0%0/5
$op eq "before" || $op eq "after" and ref $rule->[1] and ref $rule->[1] eq "HASH" and $rule->[1]{"plugin"}
$op eq "before" $op eq "after" ref $rule->[1] ref $rule->[1] eq "HASH" $rule->[1]{"plugin"}
Truth table0%0/9
A or B and C and D and E
A= $op eq "before"B= $op eq "after"C= ref $rule->[1]D= ref $rule->[1] eq "HASH"E= $rule->[1]{"plugin"}
ABCDEresult
00XXX0
1X0XX0
010XX0
1X10X0
0110X0
1X1100
011100
1X1111
011111
407 0 $ruleset = 'RULES_DEFERRED';
408 } elsif ( $op eq 'before' ) {
409 0 $ruleset = 'RULES_SETUP';
410 } elsif ( $op eq 'after' ) {
411 0 $ruleset = 'RULES_CLEANUP';
412 } else {
413 0 $ruleset = 'RULES_RUN';
414 }
415 113 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
416 # XXX TODO, need to spec stage here.
417 0 push @{ $pkg . '::' . $ruleset }, $rule;
418 }
419  
420 0 ▶sub _qualify ($@) {
Subroutine _qualify: not called CC=1 SCAR=6.9
421 0 my $key = shift;
422 0 my $op = ( caller(1) )[3];
423 0 $op =~ s/.*:://;
424 0 return { $key => $op, '' => $_[0] };
425 }
426  
427 =head2 rules STAGE
428  
429 Returns an array of all the rules for the stage STAGE.
430  
431 Valid values for STAGE are
432  
433 =over
434  
435 =item SETUP
436  
437 =item RUN
438  
439 =item CLEANUP
440  
441 =back
442  
443 =cut
444  
445 0 ▶sub rules {
Subroutine rules: not called CC=2 SCAR=10.1
Pod documented
446 0 my $self = shift;
447 0 my $stage = shift;
448 0 ▶ my $pkg = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
449 113 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
450 113 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
451 0 @{ $pkg . '::RULES_' . $stage };
452 }
453  
454 =head2 new
455  
456 Creates a new Jifty::Dispatcher object. You probably don't ever want
457 to do this. (Jifty.pm does it for you)
458  
459 =cut
460  
461 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
462 0 my $self = shift;
463 0 ▶ return $self if ref($self);
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 0 0 0
464  
465 0 bless(
466 { cwd => '',
467 path => '',
468 rule => undef,
469 @_,
470 } => $self
471 );
472 }
473  
474 =head2 handle_request
475  
476 Actually do what your dispatcher does. For now, the right thing
477 to do is to put the following two lines first:
478  
479 require MyApp::Dispatcher;
480 MyApp::Dispatcher->handle_request;
481  
482  
483 =cut
484  
485 0 ▶sub handle_request {
Subroutine handle_request: not called CC=5 SCAR=31.0
Pod documented
486 0 my $self = shift;
487  
488 0 local $Dispatcher = $self->new();
489  
490 # XXX TODO: refactor this out somehow?
491 # We don't want the previous mason request hanging aroudn once we start dispatching
492 113 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
493 0 local $HTML::Mason::Commands::m = undef;
494 # Mason introduces a DIE handler that generates a mason exception
495 # which in turn generates a backtrace. That's fine when you only
496 # do it once per request. But it's really, really painful when you
497 # do it often, as is the case with fragments
498 0 local $SIG{__DIE__} = 'DEFAULT';
499 0 local $Request = Jifty->web->request;
500  
501 0 my $handler = $Dispatcher->can("fragment_handler");
502 0 ▶ if ($Request->is_subrequest and $handler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Jifty::Dispatcher::Request->is_subrequest and $handler) 0 0 0
Condition0%0/3
$Jifty::Dispatcher::Request->is_subrequest and $handler
!ll&&!rl&&r
000
MC/DC0%0/2
$Jifty::Dispatcher::Request->is_subrequest and $handler
$Jifty::Dispatcher::Request->is_subrequest $handler
Truth table0%0/3
A and B
A= $Jifty::Dispatcher::Request->is_subrequestB= $handler
ABresult
0X0
100
111
503 0 $handler->();
504 0 return undef;
505 }
506 0 eval {
507 0 $Dispatcher->_do_dispatch( Jifty->web->request->path);
508 };
509 0 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
510 0 ▶ $self->log->warn(ref($err) . " " ."'$err'") if ( $err !~ /^ABORT/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $err =~ /^ABORT/ 0 0 0
511 }
512 0 return $Dispatcher->{stream};
513 }
514  
515 =head2 _handle_stage NAME, EXTRA_RULES
516  
517 Handles the all rules in the stage named C<NAME>. Additionally, any
518 other arguments passed after the stage C<NAME> are added to the end of
519 the rules for that stage.
520  
521 This is the unit which calling L</last_rule> skips to the end of.
522  
523 =cut
524  
525 0 ▶sub _handle_stage {
Subroutine _handle_stage: not called CC=4 SCAR=30.0
526 0 my ($self, $stage, @rules) = @_;
527  
528 # Set the current stage so that rules can make smarter choices;
529 0 local $CURRENT_STAGE = $stage;
530 0 Jifty->handler->call_trigger("before_dispatcher_$stage");
531  
532 0 eval { $self->_handle_rules( [ $self->rules($stage), @rules ] ); };
533 0 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
534 0 ▶ $self->log->warn( ref($err) . " " . "'$err'" )
Branch: 1 branch
BranchTrueFalseTotal
unless $err =~ /^(LAST RULE|ABORT)/ 0 0 0
535 if ( $err !~ /^(LAST RULE|ABORT)/ );
536 0 Jifty->handler->call_trigger("after_dispatcher_$stage");
537 0 ▶ return $err =~ /^ABORT/ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$err =~ /^ABORT/ ? : 0 0 0
538 }
539 0 Jifty->handler->call_trigger("after_dispatcher_$stage");
540 0 return 1;
541 }
542  
543 =head2 _handle_rules RULESET
544  
545 When handed an arrayref or array of rules (RULESET), walks through the
546 rules in order, executing as it goes.
547  
548  
549 =cut
550  
551 0 ▶sub _handle_rules ($) {
Subroutine _handle_rules: not called CC=3 SCAR=24.8
552 0 my ( $self, $rules ) = @_;
553  
554 0 my @rules;
555 {
556 0 local $@;
557 0 eval { @rules = @$rules };
558 0 ▶ @rules = $rules if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
559 }
560 0 RULE: foreach my $rule (@rules) {
561 0 $self->_handle_rule($rule);
562 }
563 }
564  
565 =head2 _handle_rule RULE
566  
567 When handed a single rule in the form of a coderef, C<_handle_rule>,
568 calls C<_do_run> on that rule and returns the result. When handed a
569 rule that turns out to be an array of subrules, recursively calls
570 itself and evaluates the subrules in order.
571  
572 =cut
573  
574 0 ▶sub _handle_rule {
Subroutine _handle_rule: not called CC=5 SCAR=34.0
575 0 my ( $self, $rule ) = @_;
576 0 my ( $op, @args );
577  
578 # Handle the case where $rule is an array reference.
579 0 ▶ if (ref($rule) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule eq 'ARRAY') { } 0 0 0
580 0 ( $op, @args ) = @$rule;
581 } else {
582 0 ( $op, @args ) = ( run => $rule );
583 }
584  
585 # Handle the case where $op is an array.
586 0 my $sub_rules;
587 0 ▶ if (ref($op) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $op eq "ARRAY") 0 0 0
588 0 $sub_rules = [ @$op, @args ];
589 }
590  
591 0 ▶ if ($sub_rules) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_rules) 0 0 0
592 0 for my $sub_rule (@$sub_rules) {
593 0 $self->_handle_rule($sub_rule);
594 }
595 }
596  
597 # Now we know op is a scalar.
598 0 local $self->{rule} = $op;
599 0 my $meth = "_do_$op";
600 0 $self->$meth(@args);
601  
602 }
603  
604 113 ▶no warnings 'exiting';
Subroutine BEGIN: called CC=1 SCAR=0.0
605  
606 0 ▶sub next_rule { next RULE }
Subroutine next_rule: not called CC=1 SCAR=6.9
Pod documented
607 0 ▶sub last_rule { die "LAST RULE" }
Subroutine last_rule: not called CC=1 SCAR=6.9
Pod documented
608  
609 =head2 _do_under
610  
611 This method is called by the dispatcher internally. You shouldn't need to.
612  
613 =cut
614  
615 0 ▶sub _do_under {
Subroutine _do_under: not called CC=3 SCAR=24.8
616 0 my ( $self, $cond, $rules ) = @_;
617 0 ▶ if ( my $regex = $self->_match($cond) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex = $self->_match($cond)) 0 0 0
618  
619 0 $self->log->debug("Matched 'under' rule $cond as $regex for ".$self->{'path'});
620 # match again to establish $1 $2 etc in the dynamic scope
621 0 $self->{path} =~ $regex;
622  
623 # enter the matched directory
624 0 local $self->{cwd} = substr( $self->{path}, 0, $+[0] );
625 0 ▶ chop $self->{cwd} if substr( $self->{cwd}, -1 ) eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if substr($self->{"cwd"}, -1) eq "/" 0 0 0
626  
627 0 $self->_handle_rules($rules);
628 }
629 }
630  
631 =head2 _do_when
632  
633 This method is called by the dispatcher internally. You shouldn't need to.
634  
635 =cut
636  
637 0 ▶sub _do_when {
Subroutine _do_when: not called CC=2 SCAR=17.9
638 0 my ( $self, $code, $rules ) = @_;
639 0 ▶ if ( $code->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$code()) 0 0 0
640 0 $self->_handle_rules($rules);
641 }
642 }
643  
644 =head2 _do_before
645  
646 This method is called by the dispatcher internally. You shouldn't need to.
647  
648 =cut
649  
650 0 ▶sub _do_before {
Subroutine _do_before: not called CC=2 SCAR=17.9
651 0 my ( $self, $cond, $rules ) = @_;
652 0 ▶ if ( my $regex = $self->_match($cond) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex = $self->_match($cond)) 0 0 0
653  
654 0 $self->log->debug("Matched 'before' rule $cond as $regex for ".$self->{'path'});
655 # match again to establish $1 $2 etc in the dynamic scope
656 0 $self->{path} =~ $regex;
657 0 $self->_handle_rules($rules);
658 }
659  
660 }
661  
662 =head2 _do_on
663  
664 This method is called by the dispatcher internally. You shouldn't need to.
665  
666 =cut
667  
668 0 ▶sub _do_on {
Subroutine _do_on: not called CC=2 SCAR=17.9
669 0 my ( $self, $cond, $rules ) = @_;
670 0 ▶ if ( my $regex = $self->_match($cond) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex = $self->_match($cond)) 0 0 0
671  
672 0 $self->log->debug("Matched 'on' rule $cond as $regex for ".$self->{'path'});
673 # match again to establish $1 $2 etc in the dynamic scope
674 0 $self->{path} =~ $regex;
675 0 $self->_handle_rules($rules);
676 }
677 }
678  
679 =head2 _do_after
680  
681 This method is called by the dispatcher internally. You shouldn't need to.
682  
683 =cut
684  
685 0 ▶sub _do_after {
Subroutine _do_after: not called CC=2 SCAR=17.9
686 0 my ( $self, $cond, $rules ) = @_;
687 0 ▶ if ( my $regex = $self->_match($cond) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex = $self->_match($cond)) 0 0 0
688 0 $self->log->debug("Matched 'after' rule $cond as $regex for ".$self->{'path'});
689 # match again to establish $1 $2 etc in the dynamic scope
690 0 $self->{path} =~ $regex;
691 0 $self->_handle_rules($rules);
692 }
693 }
694  
695 =head2 already_run
696  
697 Returns true if the code block has run once already in this request.
698 This can be useful for 'after' rules to ensure that they only run
699 once, even if there is a sub-dispatch which would cause it to run more
700 than once. The idiom is:
701  
702 after '/some/path/*' => run {
703 return if already_run;
704 # ...
705 };
706  
707 =cut
708  
709 0 ▶sub already_run {
Subroutine already_run: not called CC=2 SCAR=17.9
Pod documented
710 0 my $id = $Dispatcher->{call_rule};
711 0 ▶ return 1 if get "__seen_$id";
Branch: 1 branch
BranchTrueFalseTotal
if &get("__seen_$id") 0 0 0
712 0 set "__seen_$id" => 1;
713 0 return 0;
714 }
715  
716 0 ▶sub _do_run {
Subroutine _do_run: not called CC=2 SCAR=17.9
717 0 my ( $self, $code ) = @_;
718  
719 # Keep track of the coderef being run, so we can know about
720 # already_run
721 0 local $self->{call_rule} = $code;
722  
723 # establish void context and make a call
724 0 ▶ ( $self->can($code) || $code )->();
Condition0%0/3
$self->can($code) || $code
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->can($code) || $code
$self->can($code) $code
Truth table0%0/3
A || B
A= $self->can($code)B= $code
ABresult
1X1
011
000
725  
726 # XXX maybe call with all the $1..$x as @_ too? or is it too gonzo?
727 # $code->(map { substr($PATH, $-[$_], ($+[$_]-$-[$_])) } 1..$#-));
728  
729 0 return;
730 }
731  
732 =head2 _do_redirect PATH
733  
734 This method is called by the dispatcher internally. You shouldn't need to.
735  
736 Redirect the user to the URL provided in the mandatory PATH argument.
737  
738 =cut
739  
740 0 ▶sub _do_redirect {
Subroutine _do_redirect: not called CC=1 SCAR=6.9
741 0 my ( $self, $path ) = @_;
742 0 $self->log->debug("Redirecting to $path");
743 0 Jifty->web->redirect($path);
744 }
745  
746 =head2 _do_tangent PATH
747  
748 This method is called by the dispatcher internally. You shouldn't need to.
749  
750 Take a tangent to the URL provided in the mandatory PATH argument.
751 (See L<Jifty::Manual::Continuation> for more about tangents.)
752  
753 =cut
754  
755 0 ▶sub _do_tangent {
Subroutine _do_tangent: not called CC=1 SCAR=6.9
756 0 my ( $self, $path ) = @_;
757 0 $self->log->debug("Taking a tangent to $path");
758 0 Jifty->web->tangent(url => $path);
759 }
760  
761 =head2 _do_stream CODE
762  
763 The method is called by the dispatcher internally. You shouldn't need to.
764  
765 Take a coderef that returns a PSGI streamy response code.
766  
767 =cut
768  
769 0 ▶sub _do_stream {
Subroutine _do_stream: not called CC=1 SCAR=6.9
770 0 my ( $self, $code ) = @_;
771 0 $self->{stream} = $code->();
772 0 $self->_abort;
773 }
774  
775 =head2 _do_abort
776  
777 This method is called by the dispatcher internally. You shouldn't need to.
778  
779 Don't display any page. just stop.
780  
781 =cut
782  
783 0 ▶sub _do_abort {
Subroutine _do_abort: not called CC=4 SCAR=30.0
784 0 my $self = shift;
785 0 $self->log->debug("Aborting processing");
786 0 ▶ if (my $code = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = shift()) 0 0 0
787 # This is the status code
788 0 Jifty->web->response->status( $code );
789 0 ▶ if ( $code == 403 && !Jifty->web->response->body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 403 and not "Jifty"->web->response->body) 0 0 0
Condition0%0/3
$code == 403 and not "Jifty"->web->response->body
!ll&&!rl&&r
000
MC/DC0%0/2
$code == 403 and not "Jifty"->web->response->body
$code == 403 not "Jifty"->web->response->body
Truth table0%0/3
A and not(B)
A= $code == 403B= not "Jifty"->web->response->body
ABresult
0X0
100
111
790 0 Jifty->web->response->content_type('text/plain');
791 0 Jifty->web->response->body('403 Forbidden');
792 }
793 }
794 0 $self->_abort;
795 }
796  
797 0 ▶sub _abort { die "ABORT" }
Subroutine _abort: not called CC=1 SCAR=6.9
798  
799 =head2 _do_show [PATH]
800  
801 This method is called by the dispatcher internally. You shouldn't need to.
802  
803 Render a template. If the scalar argument "PATH" is given, render that component.
804 Otherwise, just render whatever we were going to anyway.
805  
806 =cut
807  
808  
809 0 ▶sub _do_show {
Subroutine _do_show: not called CC=6 SCAR=37.4
810 0 my $self = shift;
811 0 my $path;
812  
813 # Fix up the path
814 0 ▶ $path = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
815 0 ▶ $path = $self->{path} unless defined $path and length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 0 0 0
Condition0%0/3
defined $path and length $path
!ll&&!rl&&r
000
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table0%0/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
816  
817 0 ▶ unless ($CURRENT_STAGE eq 'RUN') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Jifty::Dispatcher::CURRENT_STAGE eq "RUN") 0 0 0
818 0 croak "You can't call a 'show' rule in a 'before' or 'after' block in the dispatcher. Not showing path $path";
819 }
820  
821 # If we've got a working directory (from an "under" rule) and we have
822 # a relative path, prepend the working directory
823 0 ▶ $path = "$self->{cwd}/$path" unless $path =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^/] 0 0 0
824  
825 0 Jifty->web->render_template( $path );
826  
827 0 last_rule;
828 }
829  
830 0 ▶sub _do_set {
Subroutine _do_set: not called CC=1 SCAR=1.2
831 0 my ( $self, $key, $value ) = @_;
832 113 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
833 0 $self->log->debug("Setting argument $key to $value");
834 0 $Request->template_argument($key, $value);
835 }
836  
837 0 ▶sub _do_del {
Subroutine _do_del: not called CC=1 SCAR=6.9
838 0 my ( $self, $key ) = @_;
839 0 $self->log->debug("Deleting argument $key");
840 0 $Request->delete($key);
841 }
842  
843 0 ▶sub _do_default {
Subroutine _do_default: not called CC=3 SCAR=18.7
844 0 my ( $self, $key, $value ) = @_;
845 113 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
846 0 $self->log->debug("Setting argument default $key to $value");
847 0 ▶ $Request->template_argument($key, $value)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Jifty::Dispatcher::Request->argument($key) or defined $Jifty::Dispatcher::Request->template_argument($key) 0 0 0
Condition0%0/3
defined $Jifty::Dispatcher::Request->argument($key) or defined $Jifty::Dispatcher::Request->template_argument($key)
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $Jifty::Dispatcher::Request->argument($key) or defined $Jifty::Dispatcher::Request->template_argument($key)
defined $Jifty::Dispatcher::Request->argument($key) defined $Jifty::Dispatcher::Request->template_argument($key)
Truth table0%0/3
A or B
A= defined $Jifty::Dispatcher::Request->argument($key)B= defined $Jifty::Dispatcher::Request->template_argument($key)
ABresult
1X1
011
000
848 unless defined $Request->argument($key) or defined $Request->template_argument($key);
849 }
850  
851 =head2 _do_dispatch [PATH]
852  
853 First, this routine runs all the C<before> dispatcher rules, then it runs
854 Jifty->web->handle_request(), then it runs all the main C<on> rules,
855 evaluating each one in turn. If it gets through all the rules without
856 running an C<abort>, C<redirect> or C<show> directive, it C<shows>
857 the template originally requested.
858  
859 Once it's done with that, it runs all the cleanup rules defined with C<after>.
860  
861 =cut
862  
863 0 ▶sub _do_dispatch {
Subroutine _do_dispatch: not called CC=7 SCAR=40.3
864 0 my $self = shift;
865  
866 # Requests should always start with a leading /
867 0 $self->{path} = "/".shift;
868 0 $self->{cwd} = '';
869  
870 # Normalize the path.
871 0 $self->{path} =~ s{/+}{/}g;
872  
873 0 $self->log->debug("Dispatching request to ".$self->{path});
874  
875 # Disable most actions on GET requests
876 0 ▶ Jifty->api->deny_for_get() if $self->_match_method('GET')
Branch: 1 branch
BranchTrueFalseTotal
if $self->_match_method("GET") and not "Jifty"->web->request->is_subrequest 0 0 0
Condition0%0/3
$self->_match_method("GET") and not "Jifty"->web->request->is_subrequest
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_match_method("GET") and not "Jifty"->web->request->is_subrequest
$self->_match_method("GET") not "Jifty"->web->request->is_subrequest
Truth table0%0/3
A and not(B)
A= $self->_match_method("GET")B= not "Jifty"->web->request->is_subrequest
ABresult
0X0
100
111
877 and not Jifty->web->request->is_subrequest;
878  
879 # Setup -- we we don't abort out of setup, then run the
880 # actions and then the RUN stage.
881 0 ▶ if ($self->_handle_stage('SETUP')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_handle_stage("SETUP")) 0 0 0
882 # Run actions
883 0 ▶ Jifty->web->handle_request unless Jifty->web->request->is_subrequest;
Branch: 1 branch
BranchTrueFalseTotal
unless "Jifty"->web->request->is_subrequest 0 0 0
884  
885 # Run, and show the page
886 0 $self->_handle_stage('RUN' => 'show');
887 }
888  
889 # Close the handle down, so the client can go on their merry way
890 0 ▶ unless (Jifty->web->request->is_subrequest) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Jifty"->web->request->is_subrequest) 0 0 0
891 0 Jifty->handler->call_trigger("before_flush");
892 0 Jifty->handler->buffer->flush_output;
893 # XXX: flush
894 #close(STDOUT);
895 #$Jifty::SERVER->close_client_sockets if $Jifty::SERVER;
896 0 Jifty->handler->call_trigger("after_flush");
897 }
898  
899 # Cleanup
900 0 $self->_handle_stage('CLEANUP');
901  
902 # Out to the next dispatcher's cleanup; since try/catch using die
903 # is slow, we only do this if we're not in the topmost dispatcher.
904 0 ▶ $self->_abort if $self->{path} ne "/";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"path"} ne "/" 0 0 0
905 }
906  
907 =head2 _match CONDITION
908  
909 Returns the regular expression matched if the current request fits
910 the condition defined by CONDITION.
911  
912 C<CONDITION> can be a regular expression, a "simple string" with shell
913 wildcard characters (C<*>, C<?>, C<#>, C<[]>, C<{}>) to match against,
914 or an arrayref or hashref of those. It should even be nestable.
915  
916 Arrayref conditions represents alternatives: the match succeeds as soon
917 as the first match is found.
918  
919 Hashref conditions are conjunctions: each non-empty hash key triggers a
920 separate C<_match_$keyname> call on the dispatcher object. For example, a
921 C<method> key would call C<_match_method> with its value to be matched against.
922 After each subcondition is tried (in lexicographical order) and succeeded,
923 the value associated with the C<''> key is matched again as the condition.
924  
925 =cut
926  
927 0 ▶sub _match {
Subroutine _match: not called CC=12 SCAR=50.5
928 0 my ( $self, $cond ) = @_;
929  
930 # Handle the case where $cond is an array.
931 0 ▶ if ( ref($cond) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cond eq 'ARRAY') { } 0 0 0
elsif (ref $cond eq 'HASH') { } 0 0 0
932 0 local $@;
933 0 my $rv = eval {
934 0 for my $sub_cond (@$cond)
935 {
936 0 ▶ return ( $self->_match($sub_cond) or next );
Condition0%0/2
$self->_match($sub_cond) || (next)
l!l
00
MC/DC0%0/1
$self->_match($sub_cond) || (next)
$self->_match($sub_cond)
Truth table0%0/2
A
A= $self->_match($sub_cond)
Aresult
11
00
937 }
938 };
939 0 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) { } 0 0 0
940 0 warn "$self _match failed: $err";
941 } else {
942 0 return $rv;
943 }
944 }
945  
946 # Handle the case where $cond is a hash.
947 elsif ( ref($cond) eq 'HASH' ) {
948 0 local $@;
949 0 my $rv = eval {
950 0 for my $key ( sort grep {length} keys %$cond )
951 {
952 0 my $meth = "_match_$key";
953 0 ▶ $self->$meth( $cond->{$key} ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->$meth($cond->{$key}) or return 0 0 0
954 }
955  
956 # All precondition passed, get original condition literal
957 0 ▶ return $self->_match( $cond->{''} ) if $cond->{''};
Branch: 1 branch
BranchTrueFalseTotal
if $cond->{""} 0 0 0
958  
959 # Or, if we don't have a literal, we win.
960 0 return 1;
961 };
962 0 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) { } 0 0 0
963 0 warn "$self _match failed: $err";
964 } else {
965 0 return $rv;
966 }
967 }
968  
969 # Now we know $cond is a scalar, match against it.
970 else {
971 0 ▶ my $regex = $self->_compile_condition($cond) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $regex = $self->_compile_condition($cond) or return 0 0 0
972 0 ▶ $self->{path} =~ $regex or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"path"} =~ /$regex/ or return 0 0 0
973 0 return $regex;
974 }
975 }
976  
977 =head2 _match_method METHOD
978  
979 Takes an HTTP method. Returns true if the current request
980 came in with that method.
981  
982 =cut
983  
984 0 ▶sub _match_method {
Subroutine _match_method: not called CC=1 SCAR=6.9
985 0 my ( $self, $method ) = @_;
986 #$self->log->debug("Matching method ".Jifty->web->request->method." against ".$method);
987 0 $Request->method eq uc($method);
988 }
989  
990 =head2 _match_https
991  
992 Returns true if the current request is under SSL.
993  
994 =cut
995  
996 0 ▶sub _match_https {
Subroutine _match_https: not called CC=1 SCAR=6.9
997 0 my $self = shift;
998 0 $self->log->debug("Matching request against HTTPS");
999 0 return Jifty->web->request->secure;
1000 }
1001  
1002 =head2 _match_http
1003  
1004 Returns true if the current request is not under SSL.
1005  
1006 =cut
1007  
1008 0 ▶sub _match_http {
Subroutine _match_http: not called CC=1 SCAR=6.9
1009 0 my $self = shift;
1010 0 $self->log->debug("Matching request against HTTP");
1011 0 return !Jifty->web->request->secure;
1012 }
1013  
1014 0 ▶sub _match_plugin {
Subroutine _match_plugin: not called CC=1 SCAR=6.9
1015 0 my ( $self, $plugin ) = @_;
1016 0 warn "Deferred check shouldn't happen";
1017 0 return 0;
1018 }
1019  
1020 =head2 _compile_condition CONDITION
1021  
1022 Takes a condition defined as a simple string and return it as a regex
1023 condition.
1024  
1025 =cut
1026  
1027  
1028 113 my %CONDITION_CACHE;
1029  
1030 0 ▶sub _compile_condition {
Subroutine _compile_condition: not called CC=10 SCAR=47.0
1031 0 my ( $self, $cond ) = @_;
1032  
1033 # Previously compiled (eg. a qr{} -- return it verbatim)
1034 0 ▶ return $cond if ref $cond;
Branch: 1 branch
BranchTrueFalseTotal
if ref $cond 0 0 0
1035  
1036 my $cachekey = join('-',
1037 (($Dispatcher->{rule} eq 'on') ? 'on' : 'in'),
1038 $self->{cwd},
1039 0 ▶ $cond);
Branch: 1 branch
BranchTrueFalseTotal
$Jifty::Dispatcher::Dispatcher->{'rule'} eq 'on' ? : 0 0 0
1040 0 ▶ unless ( $CONDITION_CACHE{$cachekey} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($CONDITION_CACHE{$cachekey}) 0 0 0
1041  
1042 0 my $compiled = $cond;
1043  
1044 # Escape and normalize
1045 0 $compiled = quotemeta($compiled);
1046 0 $compiled =~ s{(?:\\\/)+}{/}g;
1047 0 $compiled =~ s{/$}{};
1048  
1049 0 my $has_capture = ( $compiled =~ / \\ [*?#] /x );
1050 0 ▶ if ( $has_capture or $compiled =~ / \\ [[{] /x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_capture or $compiled =~ / \\ [[{] /x) 0 0 0
Condition0%0/3
$has_capture or $compiled =~ / \\ [[{] /x
l!l&&r!l&&!r
000
MC/DC0%0/2
$has_capture or $compiled =~ / \\ [[{] /x
$has_capture $compiled =~ / \\ [[{] /x
Truth table0%0/3
A or B
A= $has_captureB= $compiled =~ / \\ [[{] /x
ABresult
1X1
011
000
1051 0 $compiled = $self->_compile_glob($compiled);
1052 }
1053  
1054 0 ▶ if ( $compiled =~ m{^/} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($compiled =~ m[^/]) { } 0 0 0
elsif (length $compiled) { } 0 0 0
1055  
1056 # '/foo' => qr{^/foo}
1057 0 $compiled = "\\A$compiled";
1058 } elsif ( length($compiled) ) {
1059  
1060 # 'foo' => qr{^$cwd/foo}
1061 0 $compiled = "(?<=\\A\Q$self->{cwd}\E/)$compiled";
1062 } else {
1063  
1064 # empty path -- just match $cwd itself
1065 0 $compiled = "(?<=\\A\Q$self->{cwd}\E)";
1066 }
1067  
1068 0 ▶ if ( $Dispatcher->{rule} eq 'on' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Jifty::Dispatcher::Dispatcher->{'rule'} eq 'on') { } 0 0 0
1069  
1070 # "on" anchors on complete match only
1071 0 $compiled .= '/?\\z';
1072 } else {
1073  
1074 # "in" anchors on prefix match in directory boundary
1075 0 $compiled .= '(?=/|\\z)';
1076 }
1077  
1078 # Make all metachars into capturing submatches
1079 0 ▶ if ( !$has_capture ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_capture) 0 0 0
1080 0 $compiled = "($compiled)";
1081 }
1082 0 $CONDITION_CACHE{$cachekey} = qr{$compiled};
1083 }
1084 0 return $CONDITION_CACHE{$cachekey};
1085 }
1086  
1087 =head2 _compile_glob METAEXPRESSION
1088  
1089 Private function.
1090  
1091 Turns a metaexpression containing C<*>, C<?> and C<#> into a capturing regex pattern.
1092  
1093 Also supports the non-capturing C<[]> and C<{}> notations.
1094  
1095 The rules are:
1096  
1097 =over 4
1098  
1099 =item *
1100  
1101 A C<*> between two C</> characters, or between a C</> and end of string,
1102 should match one or more non-slash characters:
1103  
1104 /foo/*/bar
1105 /foo/*/
1106 /foo/*
1107 /*
1108  
1109 =item *
1110  
1111 All other C<*> can match zero or more non-slash characters:
1112  
1113 /*bar
1114 /foo*bar
1115 *
1116  
1117 =item *
1118  
1119 Two stars (C<**>) can match zero or more characters, including slash:
1120  
1121 /**/bar
1122 /foo/**
1123 **
1124  
1125 =item *
1126  
1127 Consecutive C<?> marks are captured together:
1128  
1129 /foo???bar # One capture for ???
1130 /foo??* # Two captures, one for ?? and one for *
1131  
1132 =item *
1133  
1134 The C<#> character captures one or more digit characters.
1135  
1136 =item *
1137  
1138 Brackets such as C<[a-z]> denote character classes; they are not captured.
1139  
1140 =item *
1141  
1142 Braces such as C<{xxx,yyy}]> denote alternations; they are not captured.
1143  
1144 =back
1145  
1146 =cut
1147  
1148 0 ▶sub _compile_glob {
Subroutine _compile_glob: not called CC=1 SCAR=6.9
1149 0 my ( $self, $glob ) = @_;
1150 0 $glob =~ s{
1151 # Stars between two slashes, or between a slash and end-of-string,
1152 # should at match one or more non-slash characters.
1153 (?<= /) # lookbehind for slash
1154 \\ \* # star
1155 (?= / | \z) # lookahead for slash or end-of-string
1156 }{([^/]+)}gx;
1157 0 $glob =~ s{
1158 # Two stars can match zero or more characters, including slash.
1159 \\ \* \\ \*
1160 }{(.*)}gx;
1161 0 $glob =~ s{
1162 # All other stars can match zero or more non-slash character.
1163 \\ \*
1164 }{([^/]*)}gx;
1165 0 $glob =~ s{
1166 # The number-sign character matches one or more digits.
1167 \\ \#
1168 }{(\\d+)}gx;
1169 0 $glob =~ s{
1170 # Consecutive question marks are captured as one unit;
1171 # we do this by capturing them and then repeat the result pattern
1172 # for that many times. The divide-by-two takes care of the
1173 # extra backslashes.
1174 ( (?: \\ \? )+ )
1175 0 }{([^/]{${ \( length($1)/2 ) }})}gx;
1176 0 $glob =~ s{
1177 # Brackets denote character classes
1178 (
1179 \\ \[ # opening
1180 (?: # one or more characters:
1181 \\ \\ \\ \] # ...escaped closing bracket
1182 |
1183 \\ [^\]] # ...escaped (but not the closing bracket)
1184 |
1185 [^\\] # ...normal
1186 )+
1187 \\ \] # closing
1188 )
1189 0 }{$self->_unescape($1)}egx;
1190 0 $glob =~ s{
1191 # Braces denote alternations
1192 \\ \{ ( # opening (not part of expression)
1193 (?: # zero or more characters:
1194 \\ \\ \\ \} # ...escaped closing brace
1195 |
1196 \\ [^\}] # ...escaped (but not the closing brace)
1197 |
1198 [^\\] # ...normal
1199 )+
1200 ) \\ \} # closing (not part of expression)
1201 0 }{'(?:'.join('|', split(/\\,/, $1, -1)).')'}egx;
1202 0 $glob;
1203 }
1204  
1205 0 ▶sub _unescape {
Subroutine _unescape: not called CC=1 SCAR=6.9
1206 0 my $self = shift;
1207 0 my $text = shift;
1208 0 $text =~ s{\\(.)}{$1}g;
1209 0 return $text;
1210 }
1211  
1212  
1213  
1214 =head2 import_plugins
1215  
1216 Imports rules from L<Jifty/plugins> into the main dispatcher's space.
1217  
1218 =cut
1219  
1220 0 ▶sub import_plugins {
Subroutine import_plugins: not called CC=6 SCAR=34.2
Pod documented
1221 0 my $self = shift;
1222  
1223 # Find the deferred rules
1224 0 my @deferred;
1225 0 push @deferred, $_->dispatcher->rules('DEFERRED') for Jifty->plugins;
1226 0 push @deferred, $self->rules('DEFERRED');
1227  
1228 # XXX TODO: Examine @deferred and find rules that cannot fire
1229 # because they match no plugins; they should become un-deferred in
1230 # the appropriate group. This is so 'before plugin qr/Auth/' runs
1231 # even if we have no auth plugin
1232  
1233 0 for my $stage (qw/SETUP RUN CLEANUP/) {
1234 0 my @groups;
1235 0 push @groups, {name => ref $_, rules => [$_->dispatcher->rules($stage)]} for Jifty->plugins;
1236 0 push @groups, {name => 'Jifty', rules => [$self->rules($stage)]};
1237  
1238 0 my @left;
1239 my @rules;
1240 0 for (@groups) {
1241 0 my $name = $_->{name};
1242 0 my @group_rules = @{$_->{rules}};
1243  
1244 # XXX TODO: 'after' rules should possibly be placed after
1245 # the *last* thing they could match
1246 0 push @rules, $self->_match_deferred(\@deferred, before => $name, $stage);
1247 0 push @rules, @group_rules;
1248 0 push @rules, $self->_match_deferred(\@deferred, after => $name, $stage);
1249 }
1250  
1251 113 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1252 0 @{ $self . "::RULES_$stage" } = @rules;
1253 }
1254 0 ▶ if (@deferred) {
Branch: 1 branch
BranchTrueFalseTotal
if (@deferred) 0 0 0
1255 0 warn "Leftover unmatched deferred rules: ".Jifty::YAML::Dump(\@deferred);
1256 }
1257 }
1258  
1259 0 ▶sub _match_deferred {
Subroutine _match_deferred: not called CC=7 SCAR=40.3
1260 0 my $self = shift;
1261 0 my ($deferred, $time, $name, $stage) = @_;
1262 0 my %stages = (SETUP => "before", RUN => "on", CLEANUP => "after");
1263 0 $stage = $stages{$stage};
1264  
1265 0 my @matches;
1266 0 for my $op (@{$deferred}) {
1267 # Only care if we're on the correct side of the correct plugin
1268 0 ▶ next unless $op->[0] eq $time;
Branch: 1 branch
BranchTrueFalseTotal
unless $op->[0] eq $time 0 0 0
1269  
1270 # Regex or string match, appropriately
1271 next unless (
1272 ref $op->[1]{plugin}
1273 ? ( $name =~ $op->[1]{plugin} )
1274 0 ▶ : ( $op->[1]{plugin} eq $name ) );
Branch: 2 branches
BranchTrueFalseTotal
unless ref $op->[1]{"plugin"} ? $name =~ /$op->[1]{"plugin"}/ : $op->[1]{"plugin"} eq $name 0 0 0
ref $op->[1]{'plugin'} ? : 0 0 0
1275  
1276 # Find the list of subrules
1277 0 ▶ my @subrules = ref $op->[2] eq "ARRAY" ? @{$op->[2]} : ($op->[2]);
Branch: 1 branch
BranchTrueFalseTotal
ref $op->[2] eq 'ARRAY' ? : 0 0 0
1278  
1279 # Only toplevel rules make sense (before, after, on)
1280 0 warn "Invalid subrule ".$_->[0] for grep {$_->[0] !~ /^(before|on|after)$/} @subrules;
1281 0 @subrules = grep {$_->[0] =~ /^(before|on|after)$/} @subrules;
1282  
1283 # Only match if the stage matches
1284 0 push @matches, grep {$_->[0] eq $stage} @subrules;
1285 0 @subrules = grep {$_->[0] ne $stage} @subrules;
1286  
1287 0 $op->[2] = [@subrules];
1288 }
1289  
1290 # Clean out any completely matched rules
1291 0 @$deferred = grep {@{$_->[2]}} @$deferred;
1292  
1293 0 return @matches;
1294 }
1295  
1296 113 1;
« Jifty/Web/Form/Field/OrderedList.pm ↑ Summary Jifty/Continuation.pm »