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