Added multiple paths support
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Dispatcher.pm
CommitLineData
68a748b9 1package Catalyst::Dispatcher;
1abd6db7 2
3use strict;
fbcc39ad 4use base 'Class::Accessor::Fast';
a2f2cde9 5use Catalyst::Exception;
f05af9ba 6use Catalyst::Utils;
fbcc39ad 7use Catalyst::Action;
1abd6db7 8use Text::ASCIITable;
1abd6db7 9use Tree::Simple;
10use Tree::Simple::Visitor::FindByPath;
11
fbcc39ad 12# Stringify to class
13use overload '""' => sub { return ref shift }, fallback => 1;
14
15__PACKAGE__->mk_accessors(qw/actions tree/);
1abd6db7 16
1abd6db7 17=head1 NAME
18
9c053379 19Catalyst::Dispatcher - The Catalyst Dispatcher
1abd6db7 20
21=head1 SYNOPSIS
22
23See L<Catalyst>.
24
25=head1 DESCRIPTION
26
27=head1 METHODS
28
29=over 4
30
fbcc39ad 31=item $self->detach( $c, $command [, \@arguments ] )
6ef62eb2 32
33=cut
34
35sub detach {
fbcc39ad 36 my ( $self, $c, $command, @args ) = @_;
bd7d2e94 37 $c->forward( $command, @args ) if $command;
fbcc39ad 38 die $Catalyst::DETACH;
6ef62eb2 39}
40
fbcc39ad 41=item $self->dispatch($c)
1abd6db7 42
43=cut
44
45sub dispatch {
fbcc39ad 46 my ( $self, $c ) = @_;
1abd6db7 47 my $action = $c->req->action;
48 my $namespace = '';
49 $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
50 if $action eq 'default';
99fe1710 51
1abd6db7 52 unless ($namespace) {
53 if ( my $result = $c->get_action($action) ) {
fbcc39ad 54 $namespace =
55 Catalyst::Utils::class2prefix( $result->[0]->[0]->namespace,
e494bd6b 56 $c->config->{case_sensitive} );
1abd6db7 57 }
58 }
99fe1710 59
1abd6db7 60 my $default = $action eq 'default' ? $namespace : undef;
2d16b61a 61 my $results = $c->get_action( $action, $default, $default ? 1 : 0 );
1abd6db7 62 $namespace ||= '/';
99fe1710 63
1abd6db7 64 if ( @{$results} ) {
65
fbcc39ad 66 # Errors break the normal flow and the end action is instantly run
67 my $error = 0;
68
1abd6db7 69 # Execute last begin
70 $c->state(1);
2d16b61a 71 if ( my $begin = @{ $c->get_action( 'begin', $namespace, 1 ) }[-1] ) {
fbcc39ad 72 $begin->[0]->execute($c);
73 $error++ if scalar @{ $c->error };
1abd6db7 74 }
75
76 # Execute the auto chain
1196a46d 77 my $autorun = 0;
9ddd9d05 78 for my $auto ( @{ $c->get_action( 'auto', $namespace, 1 ) } ) {
fbcc39ad 79 last if $error;
9ddd9d05 80 $autorun++;
fbcc39ad 81 $auto->[0]->execute($c);
82 $error++ if scalar @{ $c->error };
1abd6db7 83 last unless $c->state;
84 }
85
86 # Execute the action or last default
e5f21aa2 87 my $mkay = $autorun ? $c->state ? 1 : 0 : 1;
61cfdd57 88 if ( ( my $action = $c->req->action ) && $mkay ) {
fbcc39ad 89 unless ($error) {
90 if ( my $result =
91 @{ $c->get_action( $action, $default, 1 ) }[-1] )
92 {
93 $result->[0]->execute($c);
94 $error++ if scalar @{ $c->error };
95 }
1abd6db7 96 }
97 }
98
99 # Execute last end
2d16b61a 100 if ( my $end = @{ $c->get_action( 'end', $namespace, 1 ) }[-1] ) {
fbcc39ad 101 $end->[0]->execute($c);
1abd6db7 102 }
fbcc39ad 103 }
104
105 else {
1abd6db7 106 my $path = $c->req->path;
107 my $error = $path
108 ? qq/Unknown resource "$path"/
109 : "No default action defined";
110 $c->log->error($error) if $c->debug;
111 $c->error($error);
112 }
113}
114
fbcc39ad 115=item $self->forward( $c, $command [, \@arguments ] )
1abd6db7 116
117=cut
118
119sub forward {
fbcc39ad 120 my $self = shift;
1abd6db7 121 my $c = shift;
122 my $command = shift;
99fe1710 123
1abd6db7 124 unless ($command) {
125 $c->log->debug('Nothing to forward to') if $c->debug;
126 return 0;
127 }
99fe1710 128
71c3bcc3 129 # Relative forwards from detach
fbcc39ad 130 my $caller = ( caller(1) )[0]->isa('Catalyst::Dispatcher')
131 && ( ( caller(2) )[3] =~ /::detach$/ ) ? caller(3) : caller(1);
71c3bcc3 132
1abd6db7 133 my $namespace = '/';
e0fc6749 134 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
99fe1710 135
fbcc39ad 136 my $results = [];
137
1abd6db7 138 if ( $command =~ /^\// ) {
fbcc39ad 139 if ( $command =~ /^\/(\w+)$/ ) {
140 $results = $c->get_action( $1, $namespace );
141 }
142 else {
143 my $command_copy = $command;
144 my @extra_args;
145 DESCEND: while ( $command_copy =~ s/^\/(.*)\/(\w+)$/\/$1/ ) {
146 my $tail = $2;
147 if ( $results = $c->get_action( $tail, $1 ) ) {
148 $command = $tail;
149 $namespace = $command_copy;
150 push( @{$arguments}, @extra_args );
151 last DESCEND;
152 }
153 unshift( @extra_args, $tail );
154 }
155 }
db0ca23c 156 $command =~ s/^\///;
1abd6db7 157 }
99fe1710 158
e494bd6b 159 else {
160 $namespace =
161 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} )
162 || '/';
fbcc39ad 163 $results = $c->get_action( $command, $namespace );
e494bd6b 164 }
99fe1710 165
1abd6db7 166 unless ( @{$results} ) {
bd7d2e94 167
fbcc39ad 168 unless ( $c->components->{$command} ) {
bd7d2e94 169 my $error =
170qq/Couldn't forward to command "$command". Invalid action or component./;
3b2ed580 171 $c->error($error);
172 $c->log->debug($error) if $c->debug;
1abd6db7 173 return 0;
174 }
bd7d2e94 175
1f6bb799 176 my $class = $command;
1abd6db7 177 my $method = shift || 'process';
99fe1710 178
1f6bb799 179 if ( my $code = $c->components->{$class}->can($method) ) {
fbcc39ad 180 my $action = Catalyst::Action->new(
181 {
182 code => $code,
183 reverse => "$class->$method",
184 namespace => $class,
185 }
186 );
187 $results = [ [$action] ];
188 }
189
190 else {
bd7d2e94 191 my $error =
192 qq/Couldn't forward to "$class". Does not implement "$method"/;
3b2ed580 193 $c->error($error);
194 $c->log->debug($error)
1abd6db7 195 if $c->debug;
196 return 0;
197 }
99fe1710 198
1abd6db7 199 }
bd7d2e94 200
201 local $c->request->{arguments} = [ @{$arguments} ];
99fe1710 202
1abd6db7 203 for my $result ( @{$results} ) {
fbcc39ad 204 $result->[0]->execute($c);
e0fc6749 205 return if scalar @{ $c->error };
1abd6db7 206 last unless $c->state;
207 }
99fe1710 208
1abd6db7 209 return $c->state;
210}
211
fbcc39ad 212=item $self->prepare_action($c)
213
214=cut
215
216sub prepare_action {
217 my ( $self, $c ) = @_;
218 my $path = $c->req->path;
219 my @path = split /\//, $c->req->path;
220 $c->req->args( \my @args );
221
222 while (@path) {
223 $path = join '/', @path;
224 if ( my $result = ${ $c->get_action($path) }[0] ) {
225
226 # It's a regex
227 if ($#$result) {
228 my $match = $result->[1];
229 my @snippets = @{ $result->[2] };
230 $c->log->debug(
231 qq/Requested action is "$path" and matched "$match"/)
232 if $c->debug;
233 $c->log->debug(
234 'Snippets are "' . join( ' ', @snippets ) . '"' )
235 if ( $c->debug && @snippets );
236 $c->req->action($match);
237 $c->req->snippets( \@snippets );
238 }
239
240 else {
241 $c->req->action($path);
242 $c->log->debug(qq/Requested action is "$path"/) if $c->debug;
243 }
1abd6db7 244
fbcc39ad 245 $c->req->match($path);
246 last;
247 }
248 unshift @args, pop @path;
249 }
250
251 unless ( $c->req->action ) {
252 $c->req->action('default');
253 $c->req->match('');
254 }
255
256 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
257 if ( $c->debug && @args );
258}
259
260=item $self->get_action( $c, $action, $namespace, $inherit )
1abd6db7 261
262=cut
263
264sub get_action {
fbcc39ad 265 my ( $self, $c, $action, $namespace, $inherit ) = @_;
1abd6db7 266 return [] unless $action;
267 $namespace ||= '';
2d16b61a 268 $inherit ||= 0;
99fe1710 269
1abd6db7 270 if ($namespace) {
271 $namespace = '' if $namespace eq '/';
fbcc39ad 272 my $parent = $self->tree;
1abd6db7 273 my @results;
99fe1710 274
2d16b61a 275 if ($inherit) {
fbcc39ad 276 my $result =
277 $self->actions->{private}->{ $parent->getUID }->{$action};
1abd6db7 278 push @results, [$result] if $result;
279 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 280
fbcc39ad 281 SEARCH:
1abd6db7 282 for my $part ( split '/', $namespace ) {
283 $visitor->setSearchPath($part);
284 $parent->accept($visitor);
285 my $child = $visitor->getResult;
286 my $uid = $child->getUID if $child;
fbcc39ad 287 my $match = $self->actions->{private}->{$uid}->{$action}
288 if $uid;
1abd6db7 289 push @results, [$match] if $match;
6f2a2de7 290 if ($child) {
291 $parent = $child;
292 }
293 else {
294 last SEARCH;
295 }
1abd6db7 296 }
99fe1710 297
1abd6db7 298 }
99fe1710 299
1abd6db7 300 else {
99fe1710 301
1abd6db7 302 if ($namespace) {
303 my $visitor = Tree::Simple::Visitor::FindByPath->new;
304 $visitor->setSearchPath( split '/', $namespace );
305 $parent->accept($visitor);
306 my $child = $visitor->getResult;
307 my $uid = $child->getUID if $child;
fbcc39ad 308 my $match = $self->actions->{private}->{$uid}->{$action}
1abd6db7 309 if $uid;
310 push @results, [$match] if $match;
311 }
99fe1710 312
1abd6db7 313 else {
314 my $result =
fbcc39ad 315 $self->actions->{private}->{ $parent->getUID }->{$action};
1abd6db7 316 push @results, [$result] if $result;
317 }
99fe1710 318
1abd6db7 319 }
320 return \@results;
321 }
99fe1710 322
fbcc39ad 323 elsif ( my $p = $self->actions->{plain}->{$action} ) { return [ [$p] ] }
324 elsif ( my $r = $self->actions->{regex}->{$action} ) { return [ [$r] ] }
99fe1710 325
1abd6db7 326 else {
99fe1710 327
fbcc39ad 328 for my $i ( 0 .. $#{ $self->actions->{compiled} } ) {
329 my $name = $self->actions->{compiled}->[$i]->[0];
330 my $regex = $self->actions->{compiled}->[$i]->[1];
99fe1710 331
2d752b2a 332 if ( my @snippets = ( $action =~ $regex ) ) {
fbcc39ad 333 return [
334 [ $self->actions->{regex}->{$name}, $name, \@snippets ] ];
1abd6db7 335 }
99fe1710 336
1abd6db7 337 }
338 }
339 return [];
340}
341
fbcc39ad 342=item $self->set_action( $c, $action, $code, $namespace, $attrs )
1abd6db7 343
344=cut
345
346sub set_action {
fbcc39ad 347 my ( $self, $c, $method, $code, $namespace, $attrs ) = @_;
1abd6db7 348
e494bd6b 349 my $prefix =
350 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
351 || '';
1abd6db7 352 my %flags;
353
354 for my $attr ( @{$attrs} ) {
0299ba22 355 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
356 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
749472d6 357 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) {
358 push @{ $flags{path} }, $1;
359 }
360 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
0299ba22 361 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
749472d6 362 push @{ $flags{regex} }, $2;
0299ba22 363 }
1abd6db7 364 }
365
d0e524d2 366 if ( $flags{private} && ( keys %flags > 1 ) ) {
8d4e224b 367 $c->log->debug( 'Bad action definition "'
d0e524d2 368 . join( ' ', @{$attrs} )
8d4e224b 369 . qq/" for "$namespace->$method"/ )
370 if $c->debug;
d0e524d2 371 return;
372 }
1abd6db7 373 return unless keys %flags;
374
fbcc39ad 375 my $parent = $self->tree;
1abd6db7 376 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 377
1abd6db7 378 for my $part ( split '/', $prefix ) {
379 $visitor->setSearchPath($part);
380 $parent->accept($visitor);
381 my $child = $visitor->getResult;
99fe1710 382
1abd6db7 383 unless ($child) {
384 $child = $parent->addChild( Tree::Simple->new($part) );
385 $visitor->setSearchPath($part);
386 $parent->accept($visitor);
387 $child = $visitor->getResult;
388 }
99fe1710 389
1abd6db7 390 $parent = $child;
391 }
99fe1710 392
fbcc39ad 393 my $reverse = $prefix ? "$prefix/$method" : $method;
394
395 my $action = Catalyst::Action->new(
396 {
397 code => $code,
398 reverse => $reverse,
399 namespace => $namespace,
400 }
401 );
402
403 my $uid = $parent->getUID;
404 $self->actions->{private}->{$uid}->{$method} = $action;
405
749472d6 406 my @path;
407 for my $path ( @{ $flags{path} } ) {
408 $path =~ s/^\w+//;
409 $path =~ s/\w+$//;
410 if ( $path =~ /^\s*'(.*)'\s*$/ ) { $path = $1 }
411 if ( $path =~ /^\s*"(.*)"\s*$/ ) { $path = $1 }
412 push @path, $path;
1abd6db7 413 }
749472d6 414 $flags{path} = \@path;
415
416 my @regex;
417 for my $regex ( @{ $flags{regex} } ) {
418 $regex =~ s/^\w+//;
419 $regex =~ s/\w+$//;
420 if ( $regex =~ /^\s*'(.*)'\s*$/ ) { $regex = $1 }
421 if ( $regex =~ /^\s*"(.*)"\s*$/ ) { $regex = $1 }
422 push @regex, $regex;
1abd6db7 423 }
749472d6 424 $flags{regex} = \@regex;
1abd6db7 425
749472d6 426 if ( $flags{local} || $flags{global} ) {
427 push( @{ $flags{path} }, $prefix ? "/$prefix/$method" : "/$method" )
428 if $flags{local};
99fe1710 429
749472d6 430 push( @{ $flags{path} }, "/$method" ) if $flags{global};
431 }
99fe1710 432
749472d6 433 for my $path ( @{ $flags{path} } ) {
434 if ( $path =~ /^\// ) { $path =~ s/^\/// }
435 else { $path = $prefix ? "$prefix/$path" : $path }
436 $self->actions->{plain}->{$path} = $action;
1abd6db7 437 }
99fe1710 438
749472d6 439 for my $regex ( @{ $flags{regex} } ) {
fbcc39ad 440 push @{ $self->actions->{compiled} }, [ $regex, qr#$regex# ];
441 $self->actions->{regex}->{$regex} = $action;
1abd6db7 442 }
1abd6db7 443}
444
fbcc39ad 445=item $self->setup_actions( $class, $component )
1abd6db7 446
447=cut
448
449sub setup_actions {
fbcc39ad 450 my ( $self, $class ) = @_;
99fe1710 451
12e28165 452 # These are the core structures
453 $self->actions(
454 {
455 plain => {},
456 private => {},
457 regex => {},
fbcc39ad 458 compiled => []
12e28165 459 }
460 );
461
462 # We use a tree
463 $self->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
e494bd6b 464
fbcc39ad 465 for my $comp ( keys %{ $class->components } ) {
e494bd6b 466
467 # We only setup components that inherit from Catalyst::Base
a268a011 468 next unless $comp->isa('Catalyst::Base');
99fe1710 469
812a28c9 470 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
1abd6db7 471 my ( $code, $attrs ) = @{$action};
472 my $name = '';
473 no strict 'refs';
474 my @cache = ( $comp, @{"$comp\::ISA"} );
475 my %namespaces;
99fe1710 476
1abd6db7 477 while ( my $namespace = shift @cache ) {
478 $namespaces{$namespace}++;
479 for my $isa ( @{"$comp\::ISA"} ) {
480 next if $namespaces{$isa};
481 push @cache, $isa;
482 $namespaces{$isa}++;
483 }
484 }
99fe1710 485
1abd6db7 486 for my $namespace ( keys %namespaces ) {
99fe1710 487
1abd6db7 488 for my $sym ( values %{ $namespace . '::' } ) {
99fe1710 489
1abd6db7 490 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
99fe1710 491
1abd6db7 492 $name = *{$sym}{NAME};
fbcc39ad 493 $class->set_action( $name, $code, $comp, $attrs );
1abd6db7 494 last;
495 }
99fe1710 496
1abd6db7 497 }
99fe1710 498
1abd6db7 499 }
99fe1710 500
1abd6db7 501 }
99fe1710 502
1abd6db7 503 }
e494bd6b 504
fbcc39ad 505 return unless $class->debug;
99fe1710 506
1abd6db7 507 my $actions = $self->actions;
508 my $privates = Text::ASCIITable->new;
5fbed090 509 $privates->setCols( 'Private', 'Class' );
510 $privates->setColWidth( 'Private', 36, 1 );
511 $privates->setColWidth( 'Class', 37, 1 );
99fe1710 512
1abd6db7 513 my $walker = sub {
514 my ( $walker, $parent, $prefix ) = @_;
515 $prefix .= $parent->getNodeValue || '';
516 $prefix .= '/' unless $prefix =~ /\/$/;
517 my $uid = $parent->getUID;
99fe1710 518
1abd6db7 519 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
fbcc39ad 520 my $action_obj = $actions->{private}->{$uid}->{$action};
521 $privates->addRow( "$prefix$action", $action_obj->namespace );
1abd6db7 522 }
99fe1710 523
1abd6db7 524 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
525 };
99fe1710 526
1abd6db7 527 $walker->( $walker, $self->tree, '' );
fbcc39ad 528 $class->log->debug( "Loaded private actions:\n" . $privates->draw )
a268a011 529 if ( @{ $privates->{tbl_rows} } );
99fe1710 530
1abd6db7 531 my $publics = Text::ASCIITable->new;
532 $publics->setCols( 'Public', 'Private' );
699e1247 533 $publics->setColWidth( 'Public', 36, 1 );
534 $publics->setColWidth( 'Private', 37, 1 );
99fe1710 535
1abd6db7 536 for my $plain ( sort keys %{ $actions->{plain} } ) {
fbcc39ad 537 my $action = $actions->{plain}->{$plain};
538 $publics->addRow( "/$plain", "/$action" );
1abd6db7 539 }
99fe1710 540
fbcc39ad 541 $class->log->debug( "Loaded public actions:\n" . $publics->draw )
a268a011 542 if ( @{ $publics->{tbl_rows} } );
99fe1710 543
1abd6db7 544 my $regexes = Text::ASCIITable->new;
545 $regexes->setCols( 'Regex', 'Private' );
699e1247 546 $regexes->setColWidth( 'Regex', 36, 1 );
547 $regexes->setColWidth( 'Private', 37, 1 );
99fe1710 548
1abd6db7 549 for my $regex ( sort keys %{ $actions->{regex} } ) {
fbcc39ad 550 my $action = $actions->{regex}->{$regex};
551 $regexes->addRow( $regex, "/$action" );
1abd6db7 552 }
99fe1710 553
fbcc39ad 554 $class->log->debug( "Loaded regex actions:\n" . $regexes->draw )
a268a011 555 if ( @{ $regexes->{tbl_rows} } );
1abd6db7 556}
557
1abd6db7 558=back
559
560=head1 AUTHOR
561
562Sebastian Riedel, C<sri@cpan.org>
563
564=head1 COPYRIGHT
565
566This program is free software, you can redistribute it and/or modify it under
567the same terms as Perl itself.
568
569=cut
570
5711;