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