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)
29 Like C<forward> but doesn't return.
34 my ( $c, $command ) = @_;
35 $c->forward($command) 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} ) {
151 my $class = $command || '';
152 my $path = $class . '.pm';
155 unless ( $INC{$path} ) {
157 qq/Couldn't forward to "$class". Invalid or not loaded./;
159 $c->log->debug($error) if $c->debug;
163 unless ( UNIVERSAL::isa( $class, 'Catalyst::Base' ) ) {
165 qq/Can't forward to "$class". Class is not a Catalyst component./;
167 $c->log->debug($error) if $c->debug;
171 my $method = shift || 'process';
173 if ( my $code = $class->can($method) ) {
174 $c->actions->{reverse}->{"$code"} = "$class->$method";
175 $results = [ [ [ $class, $code ] ] ];
180 qq/Couldn't forward to "$class". Does not implement "$method"/;
182 $c->log->debug($error)
189 local $c->request->{arguments} = [ @{ $arguments } ];
191 for my $result ( @{$results} ) {
192 $c->execute( @{ $result->[0] } );
193 return if scalar @{ $c->error };
194 last unless $c->state;
200 =item $c->get_action( $action, $namespace, $inherit )
202 Get an action in a given namespace.
207 my ( $c, $action, $namespace, $inherit ) = @_;
208 return [] unless $action;
213 $namespace = '' if $namespace eq '/';
214 my $parent = $c->tree;
218 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
219 push @results, [$result] if $result;
220 my $visitor = Tree::Simple::Visitor::FindByPath->new;
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;
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}
244 push @results, [$match] if $match;
249 $c->actions->{private}->{ $parent->getUID }->{$action};
250 push @results, [$result] if $result;
257 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
258 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
262 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
263 my $name = $c->actions->{compiled}->[$i]->[0];
264 my $regex = $c->actions->{compiled}->[$i]->[1];
266 if ( my @snippets = ( $action =~ $regex ) ) {
267 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
275 =item $c->set_action( $action, $code, $namespace, $attrs )
277 Set an action in a given namespace.
282 my ( $c, $method, $code, $namespace, $attrs ) = @_;
285 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
289 for my $attr ( @{$attrs} ) {
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 ) {
299 if ( $flags{private} && ( keys %flags > 1 ) ) {
300 $c->log->debug( 'Bad action definition "'
301 . join( ' ', @{$attrs} )
302 . qq/" for "$namespace->$method"/ )
306 return unless keys %flags;
308 my $parent = $c->tree;
309 my $visitor = Tree::Simple::Visitor::FindByPath->new;
311 for my $part ( split '/', $prefix ) {
312 $visitor->setSearchPath($part);
313 $parent->accept($visitor);
314 my $child = $visitor->getResult;
317 $child = $parent->addChild( Tree::Simple->new($part) );
318 $visitor->setSearchPath($part);
319 $parent->accept($visitor);
320 $child = $visitor->getResult;
326 my $uid = $parent->getUID;
327 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
328 my $forward = $prefix ? "$prefix/$method" : $method;
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 }
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 }
344 my $reverse = $prefix ? "$prefix/$method" : $method;
346 if ( $flags{local} || $flags{global} || $flags{path} ) {
347 my $path = $flags{path} || $method;
350 if ( $path =~ /^\/(.+)/ ) {
355 $absolute = 1 if $flags{global};
356 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
357 $c->actions->{plain}->{$name} = [ $namespace, $code ];
360 if ( my $regex = $flags{regex} ) {
361 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
362 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
365 $c->actions->{reverse}->{"$code"} = $reverse;
368 =item $class->setup_actions($component)
370 Setup actions for a component.
377 # These are the core structures
389 $self->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
391 for my $comp ( keys %{ $self->components } ) {
393 # We only setup components that inherit from Catalyst::Base
394 next unless $comp->isa('Catalyst::Base');
396 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
397 my ( $code, $attrs ) = @{$action};
400 my @cache = ( $comp, @{"$comp\::ISA"} );
403 while ( my $namespace = shift @cache ) {
404 $namespaces{$namespace}++;
405 for my $isa ( @{"$comp\::ISA"} ) {
406 next if $namespaces{$isa};
412 for my $namespace ( keys %namespaces ) {
414 for my $sym ( values %{ $namespace . '::' } ) {
416 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
418 $name = *{$sym}{NAME};
419 $self->set_action( $name, $code, $comp, $attrs );
431 return unless $self->debug;
433 my $actions = $self->actions;
434 my $privates = Text::ASCIITable->new;
435 $privates->setCols( 'Private', 'Class' );
436 $privates->setColWidth( 'Private', 36, 1 );
437 $privates->setColWidth( 'Class', 37, 1 );
440 my ( $walker, $parent, $prefix ) = @_;
441 $prefix .= $parent->getNodeValue || '';
442 $prefix .= '/' unless $prefix =~ /\/$/;
443 my $uid = $parent->getUID;
445 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
446 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
447 $privates->addRow( "$prefix$action", $class );
450 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
453 $walker->( $walker, $self->tree, '' );
454 $self->log->debug( "Loaded private actions:\n" . $privates->draw )
455 if ( @{ $privates->{tbl_rows} } );
457 my $publics = Text::ASCIITable->new;
458 $publics->setCols( 'Public', 'Private' );
459 $publics->setColWidth( 'Public', 36, 1 );
460 $publics->setColWidth( 'Private', 37, 1 );
462 for my $plain ( sort keys %{ $actions->{plain} } ) {
463 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
464 my $reverse = $self->actions->{reverse}->{$code};
465 $reverse = $reverse ? "/$reverse" : $code;
466 $publics->addRow( "/$plain", $reverse );
469 $self->log->debug( "Loaded public actions:\n" . $publics->draw )
470 if ( @{ $publics->{tbl_rows} } );
472 my $regexes = Text::ASCIITable->new;
473 $regexes->setCols( 'Regex', 'Private' );
474 $regexes->setColWidth( 'Regex', 36, 1 );
475 $regexes->setColWidth( 'Private', 37, 1 );
477 for my $regex ( sort keys %{ $actions->{regex} } ) {
478 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
479 my $reverse = $self->actions->{reverse}->{$code};
480 $reverse = $reverse ? "/$reverse" : $code;
481 $regexes->addRow( $regex, $reverse );
484 $self->log->debug( "Loaded regex actions:\n" . $regexes->draw )
485 if ( @{ $regexes->{tbl_rows} } );
492 Sebastian Riedel, C<sri@cpan.org>
496 This program is free software, you can redistribute it and/or modify it under
497 the same terms as Perl itself.