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)
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().
113 $c->forward('index');
114 $c->forward(qw/MyApp::Model::CDBI::Foo do_stuff/);
115 $c->forward('MyApp::View::TT');
124 $c->log->debug('Nothing to forward to') if $c->debug;
128 my $caller = caller(0);
131 if ( $command =~ /^\// ) {
132 $command =~ /^\/(.*)\/(\w+)$/;
133 $namespace = $1 || '/';
134 $command = $2 || $command;
140 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} )
144 my $results = $c->get_action( $command, $namespace );
146 unless ( @{$results} ) {
147 my $class = $command || '';
148 my $path = $class . '.pm';
151 unless ( $INC{$path} ) {
153 qq/Couldn't forward to "$class". Invalid or not loaded./;
155 $c->log->debug($error) if $c->debug;
159 unless ( UNIVERSAL::isa( $class, 'Catalyst::Base' ) ) {
161 qq/Can't forward to "$class". Class is not a Catalyst component./;
163 $c->log->debug($error) if $c->debug;
167 my $method = shift || 'process';
169 if ( my $code = $class->can($method) ) {
170 $c->actions->{reverse}->{"$code"} = "$class->$method";
171 $results = [ [ [ $class, $code ] ] ];
176 qq/Couldn't forward to "$class". Does not implement "$method"/;
178 $c->log->debug($error)
185 for my $result ( @{$results} ) {
186 $c->execute( @{ $result->[0] } );
187 return if scalar @{ $c->error };
188 last unless $c->state;
194 =item $c->get_action( $action, $namespace, $inherit )
196 Get an action in a given namespace.
201 my ( $c, $action, $namespace, $inherit ) = @_;
202 return [] unless $action;
207 $namespace = '' if $namespace eq '/';
208 my $parent = $c->tree;
212 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
213 push @results, [$result] if $result;
214 my $visitor = Tree::Simple::Visitor::FindByPath->new;
216 for my $part ( split '/', $namespace ) {
217 $visitor->setSearchPath($part);
218 $parent->accept($visitor);
219 my $child = $visitor->getResult;
220 my $uid = $child->getUID if $child;
221 my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
222 push @results, [$match] if $match;
223 $parent = $child if $child;
231 my $visitor = Tree::Simple::Visitor::FindByPath->new;
232 $visitor->setSearchPath( split '/', $namespace );
233 $parent->accept($visitor);
234 my $child = $visitor->getResult;
235 my $uid = $child->getUID if $child;
236 my $match = $c->actions->{private}->{$uid}->{$action}
238 push @results, [$match] if $match;
243 $c->actions->{private}->{ $parent->getUID }->{$action};
244 push @results, [$result] if $result;
251 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
252 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
256 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
257 my $name = $c->actions->{compiled}->[$i]->[0];
258 my $regex = $c->actions->{compiled}->[$i]->[1];
260 if ( my @snippets = ( $action =~ $regex ) ) {
261 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
269 =item $c->set_action( $action, $code, $namespace, $attrs )
271 Set an action in a given namespace.
276 my ( $c, $method, $code, $namespace, $attrs ) = @_;
279 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
283 for my $attr ( @{$attrs} ) {
284 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
285 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
286 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) { $flags{path} = $1 }
287 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
288 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
293 if ( $flags{private} && ( keys %flags > 1 ) ) {
294 $c->log->debug( 'Bad action definition "'
295 . join( ' ', @{$attrs} )
296 . qq/" for "$namespace->$method"/ )
300 return unless keys %flags;
302 my $parent = $c->tree;
303 my $visitor = Tree::Simple::Visitor::FindByPath->new;
305 for my $part ( split '/', $prefix ) {
306 $visitor->setSearchPath($part);
307 $parent->accept($visitor);
308 my $child = $visitor->getResult;
311 $child = $parent->addChild( Tree::Simple->new($part) );
312 $visitor->setSearchPath($part);
313 $parent->accept($visitor);
314 $child = $visitor->getResult;
320 my $uid = $parent->getUID;
321 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
322 my $forward = $prefix ? "$prefix/$method" : $method;
324 if ( $flags{path} ) {
325 $flags{path} =~ s/^\w+//;
326 $flags{path} =~ s/\w+$//;
327 if ( $flags{path} =~ /^'(.*)'$/ ) { $flags{path} = $1 }
328 if ( $flags{path} =~ /^"(.*)"$/ ) { $flags{path} = $1 }
331 if ( $flags{regex} ) {
332 $flags{regex} =~ s/^\w+//;
333 $flags{regex} =~ s/\w+$//;
334 if ( $flags{regex} =~ /^'(.*)'$/ ) { $flags{regex} = $1 }
335 if ( $flags{regex} =~ /^"(.*)"$/ ) { $flags{regex} = $1 }
338 my $reverse = $prefix ? "$prefix/$method" : $method;
340 if ( $flags{local} || $flags{global} || $flags{path} ) {
341 my $path = $flags{path} || $method;
344 if ( $path =~ /^\/(.+)/ ) {
349 $absolute = 1 if $flags{global};
350 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
351 $c->actions->{plain}->{$name} = [ $namespace, $code ];
354 if ( my $regex = $flags{regex} ) {
355 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
356 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
359 $c->actions->{reverse}->{"$code"} = $reverse;
362 =item $class->setup_actions($component)
364 Setup actions for a component.
371 # These are the core structures
383 $self->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
385 for my $comp ( keys %{ $self->components } ) {
387 # We only setup components that inherit from Catalyst::Base
388 next unless $comp->isa('Catalyst::Base');
390 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
391 my ( $code, $attrs ) = @{$action};
394 my @cache = ( $comp, @{"$comp\::ISA"} );
397 while ( my $namespace = shift @cache ) {
398 $namespaces{$namespace}++;
399 for my $isa ( @{"$comp\::ISA"} ) {
400 next if $namespaces{$isa};
406 for my $namespace ( keys %namespaces ) {
408 for my $sym ( values %{ $namespace . '::' } ) {
410 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
412 $name = *{$sym}{NAME};
413 $self->set_action( $name, $code, $comp, $attrs );
425 return unless $self->debug;
427 my $actions = $self->actions;
428 my $privates = Text::ASCIITable->new;
429 $privates->setCols( 'Private', 'Class' );
430 $privates->setColWidth( 'Private', 36, 1 );
431 $privates->setColWidth( 'Class', 37, 1 );
434 my ( $walker, $parent, $prefix ) = @_;
435 $prefix .= $parent->getNodeValue || '';
436 $prefix .= '/' unless $prefix =~ /\/$/;
437 my $uid = $parent->getUID;
439 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
440 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
441 $privates->addRow( "$prefix$action", $class );
444 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
447 $walker->( $walker, $self->tree, '' );
448 $self->log->debug( 'Loaded private actions', $privates->draw )
449 if ( @{ $privates->{tbl_rows} } );
451 my $publics = Text::ASCIITable->new;
452 $publics->setCols( 'Public', 'Private' );
453 $publics->setColWidth( 'Public', 36, 1 );
454 $publics->setColWidth( 'Private', 37, 1 );
456 for my $plain ( sort keys %{ $actions->{plain} } ) {
457 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
458 my $reverse = $self->actions->{reverse}->{$code};
459 $reverse = $reverse ? "/$reverse" : $code;
460 $publics->addRow( "/$plain", $reverse );
463 $self->log->debug( 'Loaded public actions', $publics->draw )
464 if ( @{ $publics->{tbl_rows} } );
466 my $regexes = Text::ASCIITable->new;
467 $regexes->setCols( 'Regex', 'Private' );
468 $regexes->setColWidth( 'Regex', 36, 1 );
469 $regexes->setColWidth( 'Private', 37, 1 );
471 for my $regex ( sort keys %{ $actions->{regex} } ) {
472 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
473 my $reverse = $self->actions->{reverse}->{$code};
474 $reverse = $reverse ? "/$reverse" : $code;
475 $regexes->addRow( $regex, $reverse );
478 $self->log->debug( 'Loaded regex actions', $regexes->draw )
479 if ( @{ $regexes->{tbl_rows} } );
486 Sebastian Riedel, C<sri@cpan.org>
490 This program is free software, you can redistribute it and/or modify it under
491 the same terms as Perl itself.