- Nailed one dispatcher forward-with-args-in-uri bug
[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;
805d0f30 147 $results = $c->get_action( $tail, $1 );
148 if ( @{$results} ) {
fbcc39ad 149 $command = $tail;
150 $namespace = $command_copy;
151 push( @{$arguments}, @extra_args );
152 last DESCEND;
153 }
154 unshift( @extra_args, $tail );
155 }
156 }
db0ca23c 157 $command =~ s/^\///;
1abd6db7 158 }
99fe1710 159
e494bd6b 160 else {
161 $namespace =
162 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} )
163 || '/';
fbcc39ad 164 $results = $c->get_action( $command, $namespace );
e494bd6b 165 }
99fe1710 166
1abd6db7 167 unless ( @{$results} ) {
bd7d2e94 168
fbcc39ad 169 unless ( $c->components->{$command} ) {
bd7d2e94 170 my $error =
171qq/Couldn't forward to command "$command". Invalid action or component./;
3b2ed580 172 $c->error($error);
173 $c->log->debug($error) if $c->debug;
1abd6db7 174 return 0;
175 }
bd7d2e94 176
1f6bb799 177 my $class = $command;
1abd6db7 178 my $method = shift || 'process';
99fe1710 179
1f6bb799 180 if ( my $code = $c->components->{$class}->can($method) ) {
fbcc39ad 181 my $action = Catalyst::Action->new(
182 {
183 code => $code,
184 reverse => "$class->$method",
185 namespace => $class,
186 }
187 );
188 $results = [ [$action] ];
189 }
190
191 else {
bd7d2e94 192 my $error =
193 qq/Couldn't forward to "$class". Does not implement "$method"/;
3b2ed580 194 $c->error($error);
195 $c->log->debug($error)
1abd6db7 196 if $c->debug;
197 return 0;
198 }
99fe1710 199
1abd6db7 200 }
bd7d2e94 201
202 local $c->request->{arguments} = [ @{$arguments} ];
99fe1710 203
1abd6db7 204 for my $result ( @{$results} ) {
fbcc39ad 205 $result->[0]->execute($c);
e0fc6749 206 return if scalar @{ $c->error };
1abd6db7 207 last unless $c->state;
208 }
99fe1710 209
1abd6db7 210 return $c->state;
211}
212
fbcc39ad 213=item $self->prepare_action($c)
214
215=cut
216
217sub prepare_action {
218 my ( $self, $c ) = @_;
219 my $path = $c->req->path;
220 my @path = split /\//, $c->req->path;
221 $c->req->args( \my @args );
222
223 while (@path) {
224 $path = join '/', @path;
225 if ( my $result = ${ $c->get_action($path) }[0] ) {
226
227 # It's a regex
228 if ($#$result) {
229 my $match = $result->[1];
230 my @snippets = @{ $result->[2] };
231 $c->log->debug(
232 qq/Requested action is "$path" and matched "$match"/)
233 if $c->debug;
234 $c->log->debug(
235 'Snippets are "' . join( ' ', @snippets ) . '"' )
236 if ( $c->debug && @snippets );
237 $c->req->action($match);
238 $c->req->snippets( \@snippets );
239 }
240
241 else {
242 $c->req->action($path);
243 $c->log->debug(qq/Requested action is "$path"/) if $c->debug;
244 }
1abd6db7 245
fbcc39ad 246 $c->req->match($path);
247 last;
248 }
249 unshift @args, pop @path;
250 }
251
252 unless ( $c->req->action ) {
253 $c->req->action('default');
254 $c->req->match('');
255 }
256
257 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
258 if ( $c->debug && @args );
259}
260
261=item $self->get_action( $c, $action, $namespace, $inherit )
1abd6db7 262
263=cut
264
265sub get_action {
fbcc39ad 266 my ( $self, $c, $action, $namespace, $inherit ) = @_;
1abd6db7 267 return [] unless $action;
268 $namespace ||= '';
2d16b61a 269 $inherit ||= 0;
99fe1710 270
1abd6db7 271 if ($namespace) {
272 $namespace = '' if $namespace eq '/';
fbcc39ad 273 my $parent = $self->tree;
1abd6db7 274 my @results;
99fe1710 275
2d16b61a 276 if ($inherit) {
fbcc39ad 277 my $result =
278 $self->actions->{private}->{ $parent->getUID }->{$action};
1abd6db7 279 push @results, [$result] if $result;
280 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 281
fbcc39ad 282 SEARCH:
1abd6db7 283 for my $part ( split '/', $namespace ) {
284 $visitor->setSearchPath($part);
285 $parent->accept($visitor);
286 my $child = $visitor->getResult;
287 my $uid = $child->getUID if $child;
fbcc39ad 288 my $match = $self->actions->{private}->{$uid}->{$action}
289 if $uid;
1abd6db7 290 push @results, [$match] if $match;
6f2a2de7 291 if ($child) {
292 $parent = $child;
293 }
294 else {
295 last SEARCH;
296 }
1abd6db7 297 }
99fe1710 298
1abd6db7 299 }
99fe1710 300
1abd6db7 301 else {
99fe1710 302
1abd6db7 303 if ($namespace) {
304 my $visitor = Tree::Simple::Visitor::FindByPath->new;
305 $visitor->setSearchPath( split '/', $namespace );
306 $parent->accept($visitor);
307 my $child = $visitor->getResult;
308 my $uid = $child->getUID if $child;
fbcc39ad 309 my $match = $self->actions->{private}->{$uid}->{$action}
1abd6db7 310 if $uid;
311 push @results, [$match] if $match;
312 }
99fe1710 313
1abd6db7 314 else {
315 my $result =
fbcc39ad 316 $self->actions->{private}->{ $parent->getUID }->{$action};
1abd6db7 317 push @results, [$result] if $result;
318 }
99fe1710 319
1abd6db7 320 }
321 return \@results;
322 }
99fe1710 323
fbcc39ad 324 elsif ( my $p = $self->actions->{plain}->{$action} ) { return [ [$p] ] }
325 elsif ( my $r = $self->actions->{regex}->{$action} ) { return [ [$r] ] }
99fe1710 326
1abd6db7 327 else {
99fe1710 328
fbcc39ad 329 for my $i ( 0 .. $#{ $self->actions->{compiled} } ) {
330 my $name = $self->actions->{compiled}->[$i]->[0];
331 my $regex = $self->actions->{compiled}->[$i]->[1];
99fe1710 332
2d752b2a 333 if ( my @snippets = ( $action =~ $regex ) ) {
fbcc39ad 334 return [
335 [ $self->actions->{regex}->{$name}, $name, \@snippets ] ];
1abd6db7 336 }
99fe1710 337
1abd6db7 338 }
339 }
340 return [];
341}
342
fbcc39ad 343=item $self->set_action( $c, $action, $code, $namespace, $attrs )
1abd6db7 344
345=cut
346
347sub set_action {
fbcc39ad 348 my ( $self, $c, $method, $code, $namespace, $attrs ) = @_;
1abd6db7 349
e494bd6b 350 my $prefix =
351 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
352 || '';
1abd6db7 353 my %flags;
354
355 for my $attr ( @{$attrs} ) {
0299ba22 356 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
357 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
749472d6 358 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) {
359 push @{ $flags{path} }, $1;
360 }
361 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
0299ba22 362 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
749472d6 363 push @{ $flags{regex} }, $2;
0299ba22 364 }
1abd6db7 365 }
366
d0e524d2 367 if ( $flags{private} && ( keys %flags > 1 ) ) {
8d4e224b 368 $c->log->debug( 'Bad action definition "'
d0e524d2 369 . join( ' ', @{$attrs} )
8d4e224b 370 . qq/" for "$namespace->$method"/ )
371 if $c->debug;
d0e524d2 372 return;
373 }
1abd6db7 374 return unless keys %flags;
375
fbcc39ad 376 my $parent = $self->tree;
1abd6db7 377 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 378
1abd6db7 379 for my $part ( split '/', $prefix ) {
380 $visitor->setSearchPath($part);
381 $parent->accept($visitor);
382 my $child = $visitor->getResult;
99fe1710 383
1abd6db7 384 unless ($child) {
385 $child = $parent->addChild( Tree::Simple->new($part) );
386 $visitor->setSearchPath($part);
387 $parent->accept($visitor);
388 $child = $visitor->getResult;
389 }
99fe1710 390
1abd6db7 391 $parent = $child;
392 }
99fe1710 393
fbcc39ad 394 my $reverse = $prefix ? "$prefix/$method" : $method;
395
396 my $action = Catalyst::Action->new(
397 {
398 code => $code,
399 reverse => $reverse,
400 namespace => $namespace,
401 }
402 );
403
404 my $uid = $parent->getUID;
405 $self->actions->{private}->{$uid}->{$method} = $action;
406
749472d6 407 my @path;
408 for my $path ( @{ $flags{path} } ) {
409 $path =~ s/^\w+//;
410 $path =~ s/\w+$//;
411 if ( $path =~ /^\s*'(.*)'\s*$/ ) { $path = $1 }
412 if ( $path =~ /^\s*"(.*)"\s*$/ ) { $path = $1 }
413 push @path, $path;
1abd6db7 414 }
749472d6 415 $flags{path} = \@path;
416
417 my @regex;
418 for my $regex ( @{ $flags{regex} } ) {
419 $regex =~ s/^\w+//;
420 $regex =~ s/\w+$//;
421 if ( $regex =~ /^\s*'(.*)'\s*$/ ) { $regex = $1 }
422 if ( $regex =~ /^\s*"(.*)"\s*$/ ) { $regex = $1 }
423 push @regex, $regex;
1abd6db7 424 }
749472d6 425 $flags{regex} = \@regex;
1abd6db7 426
749472d6 427 if ( $flags{local} || $flags{global} ) {
428 push( @{ $flags{path} }, $prefix ? "/$prefix/$method" : "/$method" )
429 if $flags{local};
99fe1710 430
749472d6 431 push( @{ $flags{path} }, "/$method" ) if $flags{global};
432 }
99fe1710 433
749472d6 434 for my $path ( @{ $flags{path} } ) {
435 if ( $path =~ /^\// ) { $path =~ s/^\/// }
436 else { $path = $prefix ? "$prefix/$path" : $path }
437 $self->actions->{plain}->{$path} = $action;
1abd6db7 438 }
99fe1710 439
749472d6 440 for my $regex ( @{ $flags{regex} } ) {
fbcc39ad 441 push @{ $self->actions->{compiled} }, [ $regex, qr#$regex# ];
442 $self->actions->{regex}->{$regex} = $action;
1abd6db7 443 }
1abd6db7 444}
445
fbcc39ad 446=item $self->setup_actions( $class, $component )
1abd6db7 447
448=cut
449
450sub setup_actions {
fbcc39ad 451 my ( $self, $class ) = @_;
99fe1710 452
12e28165 453 # These are the core structures
454 $self->actions(
455 {
456 plain => {},
457 private => {},
458 regex => {},
fbcc39ad 459 compiled => []
12e28165 460 }
461 );
462
463 # We use a tree
464 $self->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
e494bd6b 465
fbcc39ad 466 for my $comp ( keys %{ $class->components } ) {
e494bd6b 467
468 # We only setup components that inherit from Catalyst::Base
a268a011 469 next unless $comp->isa('Catalyst::Base');
99fe1710 470
812a28c9 471 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
1abd6db7 472 my ( $code, $attrs ) = @{$action};
473 my $name = '';
474 no strict 'refs';
475 my @cache = ( $comp, @{"$comp\::ISA"} );
476 my %namespaces;
99fe1710 477
1abd6db7 478 while ( my $namespace = shift @cache ) {
479 $namespaces{$namespace}++;
480 for my $isa ( @{"$comp\::ISA"} ) {
481 next if $namespaces{$isa};
482 push @cache, $isa;
483 $namespaces{$isa}++;
484 }
485 }
99fe1710 486
1abd6db7 487 for my $namespace ( keys %namespaces ) {
99fe1710 488
1abd6db7 489 for my $sym ( values %{ $namespace . '::' } ) {
99fe1710 490
1abd6db7 491 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
99fe1710 492
1abd6db7 493 $name = *{$sym}{NAME};
fbcc39ad 494 $class->set_action( $name, $code, $comp, $attrs );
1abd6db7 495 last;
496 }
99fe1710 497
1abd6db7 498 }
99fe1710 499
1abd6db7 500 }
99fe1710 501
1abd6db7 502 }
99fe1710 503
1abd6db7 504 }
e494bd6b 505
fbcc39ad 506 return unless $class->debug;
99fe1710 507
1abd6db7 508 my $actions = $self->actions;
509 my $privates = Text::ASCIITable->new;
5fbed090 510 $privates->setCols( 'Private', 'Class' );
511 $privates->setColWidth( 'Private', 36, 1 );
512 $privates->setColWidth( 'Class', 37, 1 );
99fe1710 513
1abd6db7 514 my $walker = sub {
515 my ( $walker, $parent, $prefix ) = @_;
516 $prefix .= $parent->getNodeValue || '';
517 $prefix .= '/' unless $prefix =~ /\/$/;
518 my $uid = $parent->getUID;
99fe1710 519
1abd6db7 520 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
fbcc39ad 521 my $action_obj = $actions->{private}->{$uid}->{$action};
522 $privates->addRow( "$prefix$action", $action_obj->namespace );
1abd6db7 523 }
99fe1710 524
1abd6db7 525 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
526 };
99fe1710 527
1abd6db7 528 $walker->( $walker, $self->tree, '' );
fbcc39ad 529 $class->log->debug( "Loaded private actions:\n" . $privates->draw )
a268a011 530 if ( @{ $privates->{tbl_rows} } );
99fe1710 531
1abd6db7 532 my $publics = Text::ASCIITable->new;
533 $publics->setCols( 'Public', 'Private' );
699e1247 534 $publics->setColWidth( 'Public', 36, 1 );
535 $publics->setColWidth( 'Private', 37, 1 );
99fe1710 536
1abd6db7 537 for my $plain ( sort keys %{ $actions->{plain} } ) {
fbcc39ad 538 my $action = $actions->{plain}->{$plain};
539 $publics->addRow( "/$plain", "/$action" );
1abd6db7 540 }
99fe1710 541
fbcc39ad 542 $class->log->debug( "Loaded public actions:\n" . $publics->draw )
a268a011 543 if ( @{ $publics->{tbl_rows} } );
99fe1710 544
1abd6db7 545 my $regexes = Text::ASCIITable->new;
546 $regexes->setCols( 'Regex', 'Private' );
699e1247 547 $regexes->setColWidth( 'Regex', 36, 1 );
548 $regexes->setColWidth( 'Private', 37, 1 );
99fe1710 549
1abd6db7 550 for my $regex ( sort keys %{ $actions->{regex} } ) {
fbcc39ad 551 my $action = $actions->{regex}->{$regex};
552 $regexes->addRow( $regex, "/$action" );
1abd6db7 553 }
99fe1710 554
fbcc39ad 555 $class->log->debug( "Loaded regex actions:\n" . $regexes->draw )
a268a011 556 if ( @{ $regexes->{tbl_rows} } );
1abd6db7 557}
558
1abd6db7 559=back
560
561=head1 AUTHOR
562
563Sebastian Riedel, C<sri@cpan.org>
564
565=head1 COPYRIGHT
566
567This program is free software, you can redistribute it and/or modify it under
568the same terms as Perl itself.
569
570=cut
571
5721;