1 package Catalyst::Dispatcher;
4 use base 'Class::Data::Inheritable';
5 use Catalyst::Exception;
9 use Tree::Simple::Visitor::FindByPath;
11 __PACKAGE__->mk_classdata($_) for qw/actions tree/;
15 Catalyst::Dispatcher - The Catalyst Dispatcher
27 =item $c->detach( $command [, \@arguments ] )
29 Like C<forward> but doesn't return.
34 my ( $c, $command, @args ) = @_;
35 $c->forward( $command, @args ) if $command;
36 die $Catalyst::Engine::DETACH;
41 Dispatch request to actions.
47 my $action = $c->req->action;
49 $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
50 if $action eq 'default';
53 if ( my $result = $c->get_action($action) ) {
54 $namespace = Catalyst::Utils::class2prefix( $result->[0]->[0]->[0],
55 $c->config->{case_sensitive} );
59 my $default = $action eq 'default' ? $namespace : undef;
60 my $results = $c->get_action( $action, $default, $default ? 1 : 0 );
67 if ( my $begin = @{ $c->get_action( 'begin', $namespace, 1 ) }[-1] ) {
68 $c->execute( @{ $begin->[0] } );
69 return if scalar @{ $c->error };
72 # Execute the auto chain
74 for my $auto ( @{ $c->get_action( 'auto', $namespace, 1 ) } ) {
76 $c->execute( @{ $auto->[0] } );
77 return if scalar @{ $c->error };
78 last unless $c->state;
81 # Execute the action or last default
82 my $mkay = $autorun ? $c->state ? 1 : 0 : 1;
83 if ( ( my $action = $c->req->action ) && $mkay ) {
84 if ( my $result = @{ $c->get_action( $action, $default, 1 ) }[-1] )
86 $c->execute( @{ $result->[0] } );
91 if ( my $end = @{ $c->get_action( 'end', $namespace, 1 ) }[-1] ) {
92 $c->execute( @{ $end->[0] } );
93 return if scalar @{ $c->error };
98 my $path = $c->req->path;
100 ? qq/Unknown resource "$path"/
101 : "No default action defined";
102 $c->log->error($error) if $c->debug;
107 =item $c->forward( $command [, \@arguments ] )
109 Forward processing to a private action or a method from a class.
110 If you define a class without method it will default to process().
111 also takes an optional arrayref containing arguments to be passed
112 to the new function. $c->req->args will be reset upon returning
116 $c->forward('index');
117 $c->forward(qw/MyApp::Model::CDBI::Foo do_stuff/);
118 $c->forward('MyApp::View::TT');
127 $c->log->debug('Nothing to forward to') if $c->debug;
131 my $caller = caller(0);
133 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
135 if ( $command =~ /^\// ) {
136 $command =~ /^\/(.*)\/(\w+)$/;
137 $namespace = $1 || '/';
138 $command = $2 || $command;
144 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} )
148 my $results = $c->get_action( $command, $namespace );
150 unless ( @{$results} ) {
152 unless ( $c->components->{$command} ) {
154 qq/Couldn't forward to command "$command". Invalid action or component./;
156 $c->log->debug($error) if $c->debug;
160 my $class = $command;
161 my $method = shift || 'process';
163 if ( my $code = $c->components->{$class}->can($method) ) {
164 $c->actions->{reverse}->{"$code"} = "$class->$method";
165 $results = [ [ [ $class, $code ] ] ];
170 qq/Couldn't forward to "$class". Does not implement "$method"/;
172 $c->log->debug($error)
179 local $c->request->{arguments} = [ @{$arguments} ];
181 for my $result ( @{$results} ) {
182 $c->execute( @{ $result->[0] } );
183 return if scalar @{ $c->error };
184 last unless $c->state;
190 =item $c->get_action( $action, $namespace, $inherit )
192 Get an action in a given namespace.
197 my ( $c, $action, $namespace, $inherit ) = @_;
198 return [] unless $action;
203 $namespace = '' if $namespace eq '/';
204 my $parent = $c->tree;
208 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
209 push @results, [$result] if $result;
210 my $visitor = Tree::Simple::Visitor::FindByPath->new;
212 for my $part ( split '/', $namespace ) {
213 $visitor->setSearchPath($part);
214 $parent->accept($visitor);
215 my $child = $visitor->getResult;
216 my $uid = $child->getUID if $child;
217 my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
218 push @results, [$match] if $match;
219 $parent = $child if $child;
227 my $visitor = Tree::Simple::Visitor::FindByPath->new;
228 $visitor->setSearchPath( split '/', $namespace );
229 $parent->accept($visitor);
230 my $child = $visitor->getResult;
231 my $uid = $child->getUID if $child;
232 my $match = $c->actions->{private}->{$uid}->{$action}
234 push @results, [$match] if $match;
239 $c->actions->{private}->{ $parent->getUID }->{$action};
240 push @results, [$result] if $result;
247 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
248 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
252 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
253 my $name = $c->actions->{compiled}->[$i]->[0];
254 my $regex = $c->actions->{compiled}->[$i]->[1];
256 if ( my @snippets = ( $action =~ $regex ) ) {
257 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
265 =item $c->set_action( $action, $code, $namespace, $attrs )
267 Set an action in a given namespace.
272 my ( $c, $method, $code, $namespace, $attrs ) = @_;
275 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
279 for my $attr ( @{$attrs} ) {
280 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
281 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
282 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) { $flags{path} = $1 }
283 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
284 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
289 if ( $flags{private} && ( keys %flags > 1 ) ) {
290 $c->log->debug( 'Bad action definition "'
291 . join( ' ', @{$attrs} )
292 . qq/" for "$namespace->$method"/ )
296 return unless keys %flags;
298 my $parent = $c->tree;
299 my $visitor = Tree::Simple::Visitor::FindByPath->new;
301 for my $part ( split '/', $prefix ) {
302 $visitor->setSearchPath($part);
303 $parent->accept($visitor);
304 my $child = $visitor->getResult;
307 $child = $parent->addChild( Tree::Simple->new($part) );
308 $visitor->setSearchPath($part);
309 $parent->accept($visitor);
310 $child = $visitor->getResult;
316 my $uid = $parent->getUID;
317 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
318 my $forward = $prefix ? "$prefix/$method" : $method;
320 if ( $flags{path} ) {
321 $flags{path} =~ s/^\w+//;
322 $flags{path} =~ s/\w+$//;
323 if ( $flags{path} =~ /^'(.*)'$/ ) { $flags{path} = $1 }
324 if ( $flags{path} =~ /^"(.*)"$/ ) { $flags{path} = $1 }
327 if ( $flags{regex} ) {
328 $flags{regex} =~ s/^\w+//;
329 $flags{regex} =~ s/\w+$//;
330 if ( $flags{regex} =~ /^'(.*)'$/ ) { $flags{regex} = $1 }
331 if ( $flags{regex} =~ /^"(.*)"$/ ) { $flags{regex} = $1 }
334 my $reverse = $prefix ? "$prefix/$method" : $method;
336 if ( $flags{local} || $flags{global} || $flags{path} ) {
337 my $path = $flags{path} || $method;
340 if ( $path =~ /^\/(.+)/ ) {
345 $absolute = 1 if $flags{global};
346 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
347 $c->actions->{plain}->{$name} = [ $namespace, $code ];
350 if ( my $regex = $flags{regex} ) {
351 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
352 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
355 $c->actions->{reverse}->{"$code"} = $reverse;
358 =item $class->setup_actions($component)
360 Setup actions for a component.
367 # These are the core structures
379 $self->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
381 for my $comp ( keys %{ $self->components } ) {
383 # We only setup components that inherit from Catalyst::Base
384 next unless $comp->isa('Catalyst::Base');
386 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
387 my ( $code, $attrs ) = @{$action};
390 my @cache = ( $comp, @{"$comp\::ISA"} );
393 while ( my $namespace = shift @cache ) {
394 $namespaces{$namespace}++;
395 for my $isa ( @{"$comp\::ISA"} ) {
396 next if $namespaces{$isa};
402 for my $namespace ( keys %namespaces ) {
404 for my $sym ( values %{ $namespace . '::' } ) {
406 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
408 $name = *{$sym}{NAME};
409 $self->set_action( $name, $code, $comp, $attrs );
421 return unless $self->debug;
423 my $actions = $self->actions;
424 my $privates = Text::ASCIITable->new;
425 $privates->setCols( 'Private', 'Class' );
426 $privates->setColWidth( 'Private', 36, 1 );
427 $privates->setColWidth( 'Class', 37, 1 );
430 my ( $walker, $parent, $prefix ) = @_;
431 $prefix .= $parent->getNodeValue || '';
432 $prefix .= '/' unless $prefix =~ /\/$/;
433 my $uid = $parent->getUID;
435 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
436 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
437 $privates->addRow( "$prefix$action", $class );
440 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
443 $walker->( $walker, $self->tree, '' );
444 $self->log->debug( "Loaded private actions:\n" . $privates->draw )
445 if ( @{ $privates->{tbl_rows} } );
447 my $publics = Text::ASCIITable->new;
448 $publics->setCols( 'Public', 'Private' );
449 $publics->setColWidth( 'Public', 36, 1 );
450 $publics->setColWidth( 'Private', 37, 1 );
452 for my $plain ( sort keys %{ $actions->{plain} } ) {
453 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
454 my $reverse = $self->actions->{reverse}->{$code};
455 $reverse = $reverse ? "/$reverse" : $code;
456 $publics->addRow( "/$plain", $reverse );
459 $self->log->debug( "Loaded public actions:\n" . $publics->draw )
460 if ( @{ $publics->{tbl_rows} } );
462 my $regexes = Text::ASCIITable->new;
463 $regexes->setCols( 'Regex', 'Private' );
464 $regexes->setColWidth( 'Regex', 36, 1 );
465 $regexes->setColWidth( 'Private', 37, 1 );
467 for my $regex ( sort keys %{ $actions->{regex} } ) {
468 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
469 my $reverse = $self->actions->{reverse}->{$code};
470 $reverse = $reverse ? "/$reverse" : $code;
471 $regexes->addRow( $regex, $reverse );
474 $self->log->debug( "Loaded regex actions:\n" . $regexes->draw )
475 if ( @{ $regexes->{tbl_rows} } );
482 Sebastian Riedel, C<sri@cpan.org>
486 This program is free software, you can redistribute it and/or modify it under
487 the same terms as Perl itself.