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 [args]))
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);
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] );
143 if ( $command =~ /^\// ) {
144 $command =~ /^\/(.*)\/(\w+)$/;
145 $namespace = $1 || '/';
146 $command = $2 || $command;
152 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} )
156 my $results = $c->get_action( $command, $namespace );
158 unless ( @{$results} ) {
159 my $class = $command || '';
160 my $path = $class . '.pm';
163 unless ( $INC{$path} ) {
165 qq/Couldn't forward to "$class". Invalid or not loaded./;
167 $c->log->debug($error) if $c->debug;
171 unless ( UNIVERSAL::isa( $class, 'Catalyst::Base' ) ) {
173 qq/Can't forward to "$class". Class is not a Catalyst component./;
175 $c->log->debug($error) if $c->debug;
179 my $method = shift || 'process';
181 if ( my $code = $class->can($method) ) {
182 $c->actions->{reverse}->{"$code"} = "$class->$method";
183 $results = [ [ [ $class, $code ] ] ];
188 qq/Couldn't forward to "$class". Does not implement "$method"/;
190 $c->log->debug($error)
197 for my $result ( @{$results} ) {
198 $c->execute( @{ $result->[0] } );
199 last if scalar @{ $c->error };
200 last unless $c->state;
202 $c->req->args( $args ) if $args;
204 return if scalar @{ $c->error };
208 =item $c->get_action( $action, $namespace, $inherit )
210 Get an action in a given namespace.
215 my ( $c, $action, $namespace, $inherit ) = @_;
216 return [] unless $action;
221 $namespace = '' if $namespace eq '/';
222 my $parent = $c->tree;
226 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
227 push @results, [$result] if $result;
228 my $visitor = Tree::Simple::Visitor::FindByPath->new;
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;
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}
252 push @results, [$match] if $match;
257 $c->actions->{private}->{ $parent->getUID }->{$action};
258 push @results, [$result] if $result;
265 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
266 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
270 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
271 my $name = $c->actions->{compiled}->[$i]->[0];
272 my $regex = $c->actions->{compiled}->[$i]->[1];
274 if ( my @snippets = ( $action =~ $regex ) ) {
275 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
283 =item $c->set_action( $action, $code, $namespace, $attrs )
285 Set an action in a given namespace.
290 my ( $c, $method, $code, $namespace, $attrs ) = @_;
293 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
297 for my $attr ( @{$attrs} ) {
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 ) {
307 if ( $flags{private} && ( keys %flags > 1 ) ) {
308 $c->log->debug( 'Bad action definition "'
309 . join( ' ', @{$attrs} )
310 . qq/" for "$namespace->$method"/ )
314 return unless keys %flags;
316 my $parent = $c->tree;
317 my $visitor = Tree::Simple::Visitor::FindByPath->new;
319 for my $part ( split '/', $prefix ) {
320 $visitor->setSearchPath($part);
321 $parent->accept($visitor);
322 my $child = $visitor->getResult;
325 $child = $parent->addChild( Tree::Simple->new($part) );
326 $visitor->setSearchPath($part);
327 $parent->accept($visitor);
328 $child = $visitor->getResult;
334 my $uid = $parent->getUID;
335 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
336 my $forward = $prefix ? "$prefix/$method" : $method;
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 }
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 }
352 my $reverse = $prefix ? "$prefix/$method" : $method;
354 if ( $flags{local} || $flags{global} || $flags{path} ) {
355 my $path = $flags{path} || $method;
358 if ( $path =~ /^\/(.+)/ ) {
363 $absolute = 1 if $flags{global};
364 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
365 $c->actions->{plain}->{$name} = [ $namespace, $code ];
368 if ( my $regex = $flags{regex} ) {
369 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
370 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
373 $c->actions->{reverse}->{"$code"} = $reverse;
376 =item $class->setup_actions($component)
378 Setup actions for a component.
385 # These are the core structures
397 $self->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
399 for my $comp ( keys %{ $self->components } ) {
401 # We only setup components that inherit from Catalyst::Base
402 next unless $comp->isa('Catalyst::Base');
404 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
405 my ( $code, $attrs ) = @{$action};
408 my @cache = ( $comp, @{"$comp\::ISA"} );
411 while ( my $namespace = shift @cache ) {
412 $namespaces{$namespace}++;
413 for my $isa ( @{"$comp\::ISA"} ) {
414 next if $namespaces{$isa};
420 for my $namespace ( keys %namespaces ) {
422 for my $sym ( values %{ $namespace . '::' } ) {
424 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
426 $name = *{$sym}{NAME};
427 $self->set_action( $name, $code, $comp, $attrs );
439 return unless $self->debug;
441 my $actions = $self->actions;
442 my $privates = Text::ASCIITable->new;
443 $privates->setCols( 'Private', 'Class' );
444 $privates->setColWidth( 'Private', 36, 1 );
445 $privates->setColWidth( 'Class', 37, 1 );
448 my ( $walker, $parent, $prefix ) = @_;
449 $prefix .= $parent->getNodeValue || '';
450 $prefix .= '/' unless $prefix =~ /\/$/;
451 my $uid = $parent->getUID;
453 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
454 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
455 $privates->addRow( "$prefix$action", $class );
458 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
461 $walker->( $walker, $self->tree, '' );
462 $self->log->debug( "Loaded private actions:\n" . $privates->draw )
463 if ( @{ $privates->{tbl_rows} } );
465 my $publics = Text::ASCIITable->new;
466 $publics->setCols( 'Public', 'Private' );
467 $publics->setColWidth( 'Public', 36, 1 );
468 $publics->setColWidth( 'Private', 37, 1 );
470 for my $plain ( sort keys %{ $actions->{plain} } ) {
471 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
472 my $reverse = $self->actions->{reverse}->{$code};
473 $reverse = $reverse ? "/$reverse" : $code;
474 $publics->addRow( "/$plain", $reverse );
477 $self->log->debug( "Loaded public actions:\n" . $publics->draw )
478 if ( @{ $publics->{tbl_rows} } );
480 my $regexes = Text::ASCIITable->new;
481 $regexes->setCols( 'Regex', 'Private' );
482 $regexes->setColWidth( 'Regex', 36, 1 );
483 $regexes->setColWidth( 'Private', 37, 1 );
485 for my $regex ( sort keys %{ $actions->{regex} } ) {
486 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
487 my $reverse = $self->actions->{reverse}->{$code};
488 $reverse = $reverse ? "/$reverse" : $code;
489 $regexes->addRow( $regex, $reverse );
492 $self->log->debug( "Loaded regex actions:\n" . $regexes->draw )
493 if ( @{ $regexes->{tbl_rows} } );
500 Sebastian Riedel, C<sri@cpan.org>
504 This program is free software, you can redistribute it and/or modify it under
505 the same terms as Perl itself.