1 package Catalyst::Dispatcher;
4 use base 'Class::Accessor::Fast';
5 use Catalyst::Exception;
8 use Catalyst::ActionContainer;
11 use Tree::Simple::Visitor::FindByPath;
14 use overload '""' => sub { return ref shift }, fallback => 1;
16 __PACKAGE__->mk_accessors(qw/actions tree/);
20 Catalyst::Dispatcher - The Catalyst Dispatcher
32 =item $self->detach( $c, $command [, \@arguments ] )
37 my ( $self, $c, $command, @args ) = @_;
38 $c->forward( $command, @args ) if $command;
39 die $Catalyst::DETACH;
42 =item $self->dispatch($c)
47 my ( $self, $c ) = @_;
48 my $action = $c->req->action;
50 $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
51 if $action eq 'default';
54 if ( my $result = $c->get_action($action) ) {
56 Catalyst::Utils::class2prefix( $result->[0]->[0]->namespace,
57 $c->config->{case_sensitive} );
61 my $default = $action eq 'default' ? $namespace : undef;
62 my $results = $c->get_action( $action, $default, $default ? 1 : 0 );
67 # Errors break the normal flow and the end action is instantly run
72 if ( my $begin = @{ $c->get_action( 'begin', $namespace, 1 ) }[-1] ) {
73 $begin->[0]->execute($c);
74 $error++ if scalar @{ $c->error };
77 # Execute the auto chain
79 for my $auto ( @{ $c->get_action( 'auto', $namespace, 1 ) } ) {
82 $auto->[0]->execute($c);
83 $error++ if scalar @{ $c->error };
84 last unless $c->state;
87 # Execute the action or last default
88 my $mkay = $autorun ? $c->state ? 1 : 0 : 1;
89 if ( ( my $action = $c->req->action ) && $mkay ) {
92 @{ $c->get_action( $action, $default, 1 ) }[-1] )
94 $result->[0]->execute($c);
95 $error++ if scalar @{ $c->error };
101 if ( my $end = @{ $c->get_action( 'end', $namespace, 1 ) }[-1] ) {
102 $end->[0]->execute($c);
107 my $path = $c->req->path;
109 ? qq/Unknown resource "$path"/
110 : "No default action defined";
111 $c->log->error($error) if $c->debug;
116 =item $self->forward( $c, $command [, \@arguments ] )
126 $c->log->debug('Nothing to forward to') if $c->debug;
130 # Relative forwards from detach
131 my $caller = ( caller(1) )[0]->isa('Catalyst::Dispatcher')
132 && ( ( caller(2) )[3] =~ /::detach$/ ) ? caller(3) : caller(1);
134 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
138 my $command_copy = $command;
140 unless ( $command_copy =~ s/^\/// ) {
142 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} ) || '';
143 $command_copy = "${namespace}/${command}";
146 unless ( $command_copy =~ /\// ) {
147 $results = $c->get_action( $command_copy, '/' );
151 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
153 $results = $c->get_action( $tail, $1 );
156 push( @{$arguments}, @extra_args );
159 unshift( @extra_args, $tail );
163 unless ( @{$results} ) {
165 unless ( $c->components->{$command} ) {
167 qq/Couldn't forward to command "$command". Invalid action or component./;
169 $c->log->debug($error) if $c->debug;
173 my $class = $command;
174 my $method = shift || 'process';
176 if ( my $code = $c->components->{$class}->can($method) ) {
177 my $action = Catalyst::Action->new(
180 reverse => "$class->$method",
184 $results = [ [$action] ];
189 qq/Couldn't forward to "$class". Does not implement "$method"/;
191 $c->log->debug($error)
198 local $c->request->{arguments} = [ @{$arguments} ];
200 for my $result ( @{$results} ) {
201 $result->[0]->execute($c);
202 return if scalar @{ $c->error };
203 last unless $c->state;
209 =item $self->prepare_action($c)
214 my ( $self, $c ) = @_;
215 my $path = $c->req->path;
216 my @path = split /\//, $c->req->path;
217 $c->req->args( \my @args );
220 $path = join '/', @path;
221 if ( my $result = ${ $c->get_action($path) }[0] ) {
225 my $match = $result->[1];
226 my @snippets = @{ $result->[2] };
228 qq/Requested action is "$path" and matched "$match"/)
231 'Snippets are "' . join( ' ', @snippets ) . '"' )
232 if ( $c->debug && @snippets );
233 $c->req->action($match);
234 $c->req->snippets( \@snippets );
238 $c->req->action($path);
239 $c->log->debug(qq/Requested action is "$path"/) if $c->debug;
242 $c->req->match($path);
245 unshift @args, pop @path;
248 unless ( $c->req->action ) {
249 $c->req->action('default');
253 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
254 if ( $c->debug && @args );
257 =item $self->get_action( $c, $action, $namespace, $inherit )
262 my ( $self, $c, $action, $namespace, $inherit ) = @_;
263 return [] unless $action;
269 my $parent = $self->tree;
272 if ($namespace ne '/') {
274 my $visitor = Tree::Simple::Visitor::FindByPath->new;
275 my @path = split('/', $namespace);
276 $visitor->setSearchPath( @path );
277 $parent->accept($visitor);
281 @match = $visitor->getResults;
282 @match = ($parent) unless @match;
284 if (!defined $visitor->getResult) {
285 my $extra = $path[(scalar @match) - 1];
287 $visitor->setSearchPath($extra);
288 $match[-1]->accept($visitor);
289 push(@match, $visitor->getResult) if defined $visitor->getResult;
292 @match = ($visitor->getResult) if $visitor->getResult;
297 @match = ($parent) unless @match;
301 foreach my $child (@match) {
302 my $node = $child->getNodeValue->actions;
303 push(@results, [ $node->{$action} ]) if defined $node->{$action};
308 elsif ( my $p = $self->actions->{plain}->{$action} ) { return [ [$p] ] }
309 elsif ( my $r = $self->actions->{regex}->{$action} ) { return [ [$r] ] }
313 for my $i ( 0 .. $#{ $self->actions->{compiled} } ) {
314 my $name = $self->actions->{compiled}->[$i]->[0];
315 my $regex = $self->actions->{compiled}->[$i]->[1];
317 if ( my @snippets = ( $action =~ $regex ) ) {
319 [ $self->actions->{regex}->{$name}, $name, \@snippets ] ];
327 =item $self->set_action( $c, $action, $code, $namespace, $attrs )
332 my ( $self, $c, $method, $code, $namespace, $attrs ) = @_;
335 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
339 for my $attr ( @{$attrs} ) {
340 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
341 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
342 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) {
343 push @{ $flags{path} }, $1;
345 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
346 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
347 push @{ $flags{regex} }, $2;
351 if ( $flags{private} && ( keys %flags > 1 ) ) {
352 $c->log->debug( 'Bad action definition "'
353 . join( ' ', @{$attrs} )
354 . qq/" for "$namespace->$method"/ )
358 return unless keys %flags;
360 my $parent = $self->tree;
361 my $visitor = Tree::Simple::Visitor::FindByPath->new;
364 for my $part ( split '/', $prefix ) {
365 $visitor->setSearchPath($part);
366 $parent->accept($visitor);
367 my $child = $visitor->getResult;
370 my $container = Catalyst::ActionContainer->new(
371 { part => $part, actions => {} });
372 $child = $parent->addChild( Tree::Simple->new($container) );
373 $visitor->setSearchPath($part);
374 $parent->accept($visitor);
375 $child = $visitor->getResult;
382 my $reverse = $prefix ? "$prefix/$method" : $method;
384 my $action = Catalyst::Action->new(
388 namespace => $namespace,
392 $parent->getNodeValue->actions->{$method} = $action;
395 for my $path ( @{ $flags{path} } ) {
398 if ( $path =~ /^\s*'(.*)'\s*$/ ) { $path = $1 }
399 if ( $path =~ /^\s*"(.*)"\s*$/ ) { $path = $1 }
402 $flags{path} = \@path;
405 for my $regex ( @{ $flags{regex} } ) {
408 if ( $regex =~ /^\s*'(.*)'\s*$/ ) { $regex = $1 }
409 if ( $regex =~ /^\s*"(.*)"\s*$/ ) { $regex = $1 }
412 $flags{regex} = \@regex;
414 if ( $flags{local} || $flags{global} ) {
415 push( @{ $flags{path} }, $prefix ? "/$prefix/$method" : "/$method" )
418 push( @{ $flags{path} }, "/$method" ) if $flags{global};
421 for my $path ( @{ $flags{path} } ) {
422 if ( $path =~ /^\// ) { $path =~ s/^\/// }
423 else { $path = $prefix ? "$prefix/$path" : $path }
424 $self->actions->{plain}->{$path} = $action;
427 for my $regex ( @{ $flags{regex} } ) {
428 push @{ $self->actions->{compiled} }, [ $regex, qr#$regex# ];
429 $self->actions->{regex}->{$regex} = $action;
433 =item $self->setup_actions( $class, $component )
438 my ( $self, $class ) = @_;
440 # These are the core structures
451 my $container = Catalyst::ActionContainer->new(
452 { part => '/', actions => {} } );
453 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
455 for my $comp ( keys %{ $class->components } ) {
457 # We only setup components that inherit from Catalyst::Base
458 next unless $comp->isa('Catalyst::Base');
460 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
461 my ( $code, $attrs ) = @{$action};
464 my @cache = ( $comp, @{"$comp\::ISA"} );
467 while ( my $namespace = shift @cache ) {
468 $namespaces{$namespace}++;
469 for my $isa ( @{"$comp\::ISA"} ) {
470 next if $namespaces{$isa};
476 for my $namespace ( keys %namespaces ) {
478 for my $sym ( values %{ $namespace . '::' } ) {
480 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
482 $name = *{$sym}{NAME};
483 $class->set_action( $name, $code, $comp, $attrs );
495 return unless $class->debug;
497 my $actions = $self->actions;
498 my $privates = Text::ASCIITable->new;
499 $privates->setCols( 'Private', 'Class' );
500 $privates->setColWidth( 'Private', 36, 1 );
501 $privates->setColWidth( 'Class', 37, 1 );
504 my ( $walker, $parent, $prefix ) = @_;
505 $prefix .= $parent->getNodeValue || '';
506 $prefix .= '/' unless $prefix =~ /\/$/;
507 my $node = $parent->getNodeValue->actions;
509 for my $action ( keys %{ $node } ) {
510 my $action_obj = $node->{$action};
511 $privates->addRow( "$prefix$action", $action_obj->namespace );
514 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
517 $walker->( $walker, $self->tree, '' );
518 $class->log->debug( "Loaded private actions:\n" . $privates->draw )
519 if ( @{ $privates->{tbl_rows} } );
521 my $publics = Text::ASCIITable->new;
522 $publics->setCols( 'Public', 'Private' );
523 $publics->setColWidth( 'Public', 36, 1 );
524 $publics->setColWidth( 'Private', 37, 1 );
526 for my $plain ( sort keys %{ $actions->{plain} } ) {
527 my $action = $actions->{plain}->{$plain};
528 $publics->addRow( "/$plain", "/$action" );
531 $class->log->debug( "Loaded public actions:\n" . $publics->draw )
532 if ( @{ $publics->{tbl_rows} } );
534 my $regexes = Text::ASCIITable->new;
535 $regexes->setCols( 'Regex', 'Private' );
536 $regexes->setColWidth( 'Regex', 36, 1 );
537 $regexes->setColWidth( 'Private', 37, 1 );
539 for my $regex ( sort keys %{ $actions->{regex} } ) {
540 my $action = $actions->{regex}->{$regex};
541 $regexes->addRow( $regex, "/$action" );
544 $class->log->debug( "Loaded regex actions:\n" . $regexes->draw )
545 if ( @{ $regexes->{tbl_rows} } );
552 Sebastian Riedel, C<sri@cpan.org>
556 This program is free software, you can redistribute it and/or modify it under
557 the same terms as Perl itself.