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