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 # Relative forwards from detach
132 my $caller = ( caller(0) )[0]->isa('Catalyst::Dispatcher')
133 && ( ( caller(1) )[3] =~ /::detach$/ ) ? caller(1) : caller(0);
136 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
138 if ( $command =~ /^\// ) {
139 $command =~ /^\/(.*)\/(\w+)$/;
140 $namespace = $1 || '/';
141 $command = $2 || $command;
147 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} )
151 my $results = $c->get_action( $command, $namespace );
153 unless ( @{$results} ) {
155 unless ( defined( $c->components->{$command} ) ) {
157 qq/Couldn't forward to command "$command". Invalid action or component./;
159 $c->log->debug($error) if $c->debug;
163 my $class = $command;
164 my $method = shift || 'process';
166 if ( my $code = $c->components->{$class}->can($method) ) {
167 $c->actions->{reverse}->{"$code"} = "$class->$method";
168 $results = [ [ [ $class, $code ] ] ];
173 qq/Couldn't forward to "$class". Does not implement "$method"/;
175 $c->log->debug($error)
182 local $c->request->{arguments} = [ @{$arguments} ];
184 for my $result ( @{$results} ) {
185 $c->execute( @{ $result->[0] } );
186 return if scalar @{ $c->error };
187 last unless $c->state;
193 =item $c->get_action( $action, $namespace, $inherit )
195 Get an action in a given namespace.
200 my ( $c, $action, $namespace, $inherit ) = @_;
201 return [] unless $action;
206 $namespace = '' if $namespace eq '/';
207 my $parent = $c->tree;
211 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
212 push @results, [$result] if $result;
213 my $visitor = Tree::Simple::Visitor::FindByPath->new;
215 for my $part ( split '/', $namespace ) {
216 $visitor->setSearchPath($part);
217 $parent->accept($visitor);
218 my $child = $visitor->getResult;
219 my $uid = $child->getUID if $child;
220 my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
221 push @results, [$match] if $match;
222 $parent = $child if $child;
230 my $visitor = Tree::Simple::Visitor::FindByPath->new;
231 $visitor->setSearchPath( split '/', $namespace );
232 $parent->accept($visitor);
233 my $child = $visitor->getResult;
234 my $uid = $child->getUID if $child;
235 my $match = $c->actions->{private}->{$uid}->{$action}
237 push @results, [$match] if $match;
242 $c->actions->{private}->{ $parent->getUID }->{$action};
243 push @results, [$result] if $result;
250 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
251 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
255 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
256 my $name = $c->actions->{compiled}->[$i]->[0];
257 my $regex = $c->actions->{compiled}->[$i]->[1];
259 if ( my @snippets = ( $action =~ $regex ) ) {
260 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
268 =item $c->set_action( $action, $code, $namespace, $attrs )
270 Set an action in a given namespace.
275 my ( $c, $method, $code, $namespace, $attrs ) = @_;
278 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
282 for my $attr ( @{$attrs} ) {
283 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
284 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
285 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) { $flags{path} = $1 }
286 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
287 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
292 if ( $flags{private} && ( keys %flags > 1 ) ) {
293 $c->log->debug( 'Bad action definition "'
294 . join( ' ', @{$attrs} )
295 . qq/" for "$namespace->$method"/ )
299 return unless keys %flags;
301 my $parent = $c->tree;
302 my $visitor = Tree::Simple::Visitor::FindByPath->new;
304 for my $part ( split '/', $prefix ) {
305 $visitor->setSearchPath($part);
306 $parent->accept($visitor);
307 my $child = $visitor->getResult;
310 $child = $parent->addChild( Tree::Simple->new($part) );
311 $visitor->setSearchPath($part);
312 $parent->accept($visitor);
313 $child = $visitor->getResult;
319 my $uid = $parent->getUID;
320 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
321 my $forward = $prefix ? "$prefix/$method" : $method;
323 if ( $flags{path} ) {
324 $flags{path} =~ s/^\w+//;
325 $flags{path} =~ s/\w+$//;
326 if ( $flags{path} =~ /^\s*'(.*)'\s*$/ ) { $flags{path} = $1 }
327 if ( $flags{path} =~ /^\s*"(.*)"\s*$/ ) { $flags{path} = $1 }
330 if ( $flags{regex} ) {
331 $flags{regex} =~ s/^\w+//;
332 $flags{regex} =~ s/\w+$//;
333 if ( $flags{regex} =~ /^\s*'(.*)'\s*$/ ) { $flags{regex} = $1 }
334 if ( $flags{regex} =~ /^\s*"(.*)"\s*$/ ) { $flags{regex} = $1 }
337 my $reverse = $prefix ? "$prefix/$method" : $method;
339 if ( $flags{local} || $flags{global} || $flags{path} ) {
340 my $path = $flags{path} || $method;
343 if ( $path =~ /^\/(.+)/ ) {
348 $absolute = 1 if $flags{global};
349 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
350 $c->actions->{plain}->{$name} = [ $namespace, $code ];
353 if ( my $regex = $flags{regex} ) {
354 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
355 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
358 $c->actions->{reverse}->{"$code"} = $reverse;
361 =item $class->setup_actions($component)
363 Setup actions for a component.
370 # These are the core structures
382 $self->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
384 for my $comp ( keys %{ $self->components } ) {
386 # We only setup components that inherit from Catalyst::Base
387 next unless $comp->isa('Catalyst::Base');
389 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
390 my ( $code, $attrs ) = @{$action};
393 my @cache = ( $comp, @{"$comp\::ISA"} );
396 while ( my $namespace = shift @cache ) {
397 $namespaces{$namespace}++;
398 for my $isa ( @{"$comp\::ISA"} ) {
399 next if $namespaces{$isa};
405 for my $namespace ( keys %namespaces ) {
407 for my $sym ( values %{ $namespace . '::' } ) {
409 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
411 $name = *{$sym}{NAME};
412 $self->set_action( $name, $code, $comp, $attrs );
424 return unless $self->debug;
426 my $actions = $self->actions;
427 my $privates = Text::ASCIITable->new;
428 $privates->setCols( 'Private', 'Class' );
429 $privates->setColWidth( 'Private', 36, 1 );
430 $privates->setColWidth( 'Class', 37, 1 );
433 my ( $walker, $parent, $prefix ) = @_;
434 $prefix .= $parent->getNodeValue || '';
435 $prefix .= '/' unless $prefix =~ /\/$/;
436 my $uid = $parent->getUID;
438 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
439 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
440 $privates->addRow( "$prefix$action", $class );
443 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
446 $walker->( $walker, $self->tree, '' );
447 $self->log->debug( "Loaded private actions:\n" . $privates->draw )
448 if ( @{ $privates->{tbl_rows} } );
450 my $publics = Text::ASCIITable->new;
451 $publics->setCols( 'Public', 'Private' );
452 $publics->setColWidth( 'Public', 36, 1 );
453 $publics->setColWidth( 'Private', 37, 1 );
455 for my $plain ( sort keys %{ $actions->{plain} } ) {
456 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
457 my $reverse = $self->actions->{reverse}->{$code};
458 $reverse = $reverse ? "/$reverse" : $code;
459 $publics->addRow( "/$plain", $reverse );
462 $self->log->debug( "Loaded public actions:\n" . $publics->draw )
463 if ( @{ $publics->{tbl_rows} } );
465 my $regexes = Text::ASCIITable->new;
466 $regexes->setCols( 'Regex', 'Private' );
467 $regexes->setColWidth( 'Regex', 36, 1 );
468 $regexes->setColWidth( 'Private', 37, 1 );
470 for my $regex ( sort keys %{ $actions->{regex} } ) {
471 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
472 my $reverse = $self->actions->{reverse}->{$code};
473 $reverse = $reverse ? "/$reverse" : $code;
474 $regexes->addRow( $regex, $reverse );
477 $self->log->debug( "Loaded regex actions:\n" . $regexes->draw )
478 if ( @{ $regexes->{tbl_rows} } );
485 Sebastian Riedel, C<sri@cpan.org>
489 This program is free software, you can redistribute it and/or modify it under
490 the same terms as Perl itself.