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 ) = @_;
51 my @containers = $self->get_containers( $c->namespace );
53 foreach my $name (qw/begin auto end/) {
56 grep { exists $_->{$name} }
62 # Errors break the normal flow and the end action is instantly run
67 if ( my $begin = @{ $actions{begin} }[-1] ) {
69 $error++ if scalar @{ $c->error };
72 # Execute the auto chain
74 for my $auto ( @{ $actions{auto} } ) {
78 $error++ if scalar @{ $c->error };
79 last unless $c->state;
82 # Execute the action or last default
83 my $mkay = $autorun ? $c->state ? 1 : 0 : 1;
84 if ( ( my $action = $c->req->action ) && $mkay ) {
86 $c->action->execute($c);
87 $error++ if scalar @{ $c->error };
92 if ( my $end = @{ $actions{end} }[-1] ) {
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 $self->forward( $c, $command [, \@arguments ] )
117 $c->log->debug('Nothing to forward to') if $c->debug;
121 # Relative forwards from detach
122 my $caller = ( caller(1) )[0]->isa('Catalyst::Dispatcher')
123 && ( ( caller(2) )[3] =~ /::detach$/ ) ? caller(3) : caller(1);
125 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
129 my $command_copy = $command;
131 unless ( $command_copy =~ s/^\/// ) {
133 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} ) || '';
134 $command_copy = "${namespace}/${command}";
137 unless ( $command_copy =~ /\// ) {
138 $results = $c->get_action( $command_copy, '/' );
142 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
144 $results = $c->get_action( $tail, $1 );
147 push( @{$arguments}, @extra_args );
150 unshift( @extra_args, $tail );
154 unless ( @{$results} ) {
156 unless ( $c->components->{$command} ) {
158 qq/Couldn't forward to command "$command". Invalid action or component./;
160 $c->log->debug($error) if $c->debug;
164 my $class = $command;
165 my $method = shift || 'process';
167 if ( my $code = $c->components->{$class}->can($method) ) {
168 my $action = Catalyst::Action->new(
171 reverse => "$class->$method",
176 $results = [ [$action] ];
181 qq/Couldn't forward to "$class". Does not implement "$method"/;
183 $c->log->debug($error)
190 local $c->request->{arguments} = [ @{$arguments} ];
192 for my $result ( @{$results} ) {
193 $result->[0]->execute($c);
194 return if scalar @{ $c->error };
195 last unless $c->state;
201 =item $self->prepare_action($c)
206 my ( $self, $c ) = @_;
207 my $path = $c->req->path;
208 my @path = split /\//, $c->req->path;
209 $c->req->args( \my @args );
212 $path = join '/', @path;
213 if ( my $result = ${ $c->get_action($path) }[0] ) {
217 my $match = $result->[1];
218 my @snippets = @{ $result->[2] };
220 qq/Requested action is "$path" and matched "$match"/)
223 'Snippets are "' . join( ' ', @snippets ) . '"' )
224 if ( $c->debug && @snippets );
225 $c->req->action($match);
226 $c->req->snippets( \@snippets );
230 $c->req->action($path);
231 $c->log->debug(qq/Requested action is "$path"/) if $c->debug;
234 $c->req->match($path);
235 $c->action($result->[0]);
236 $c->namespace($result->[0]->prefix);
239 unshift @args, pop @path;
242 unless ( $c->req->action ) {
243 my $result = @{$c->get_action('default', $c->req->path, 1) || []}[-1];
245 $c->action( $result->[0] );
246 $c->namespace( $c->req->path );
247 $c->req->action('default');
252 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
253 if ( $c->debug && @args );
256 =item $self->get_action( $c, $action, $namespace, $inherit )
261 my ( $self, $c, $action, $namespace, $inherit ) = @_;
262 return [] unless $action;
268 my @match = $self->get_containers( $namespace );
272 foreach my $child ($inherit ? @match: $match[-1]) {
273 my $node = $child->actions;
274 push(@results, [ $node->{$action} ]) if defined $node->{$action};
279 elsif ( my $p = $self->actions->{plain}->{$action} ) { return [ [$p] ] }
280 elsif ( my $r = $self->actions->{regex}->{$action} ) { return [ [$r] ] }
284 for my $i ( 0 .. $#{ $self->actions->{compiled} } ) {
285 my $name = $self->actions->{compiled}->[$i]->[0];
286 my $regex = $self->actions->{compiled}->[$i]->[1];
288 if ( my @snippets = ( $action =~ $regex ) ) {
290 [ $self->actions->{regex}->{$name}, $name, \@snippets ] ];
298 =item $self->get_containers( $namespace )
303 my ( $self, $namespace ) = @_;
305 return ($self->tree->getNodeValue) if $namespace eq '/';
307 my $visitor = Tree::Simple::Visitor::FindByPath->new;
308 my @path = split('/', $namespace);
309 $visitor->setSearchPath( @path );
310 $self->tree->accept($visitor);
312 my @match = $visitor->getResults;
313 @match = ($self->tree) unless @match;
315 if (!defined $visitor->getResult) {
316 my $extra = $path[(scalar @match) - 1];
318 $visitor->setSearchPath($extra);
319 $match[-1]->accept($visitor);
320 push(@match, $visitor->getResult) if defined $visitor->getResult;
323 return map { $_->getNodeValue } @match;
326 =item $self->set_action( $c, $action, $code, $namespace, $attrs )
331 my ( $self, $c, $method, $code, $namespace, $attrs ) = @_;
334 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
338 for my $attr ( @{$attrs} ) {
339 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
340 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
341 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) {
342 push @{ $flags{path} }, $1;
344 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
345 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
346 push @{ $flags{regex} }, $2;
350 if ( $flags{private} && ( keys %flags > 1 ) ) {
351 $c->log->debug( 'Bad action definition "'
352 . join( ' ', @{$attrs} )
353 . qq/" for "$namespace->$method"/ )
357 return unless keys %flags;
359 my $parent = $self->tree;
360 my $visitor = Tree::Simple::Visitor::FindByPath->new;
363 for my $part ( split '/', $prefix ) {
364 $visitor->setSearchPath($part);
365 $parent->accept($visitor);
366 my $child = $visitor->getResult;
369 my $container = Catalyst::ActionContainer->new(
370 { part => $part, actions => {} });
371 $child = $parent->addChild( Tree::Simple->new($container) );
372 $visitor->setSearchPath($part);
373 $parent->accept($visitor);
374 $child = $visitor->getResult;
381 my $reverse = $prefix ? "$prefix/$method" : $method;
383 my $action = Catalyst::Action->new(
387 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.