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/) {
55 # Go down the container list representing each part of the
56 # current namespace inheritance tree, grabbing the actions hash
57 # of the ActionContainer object and looking for actions of the
58 # appropriate name registered to the namespace
62 grep { exists $_->{$name} }
68 # Errors break the normal flow and the end action is instantly run
73 if ( my $begin = @{ $actions{begin} }[-1] ) {
75 $error++ if scalar @{ $c->error };
78 # Execute the auto chain
80 for my $auto ( @{ $actions{auto} } ) {
84 $error++ if scalar @{ $c->error };
85 last unless $c->state;
88 # Execute the action or last default
89 my $mkay = $autorun ? $c->state ? 1 : 0 : 1;
90 if ( ( my $action = $c->req->action ) && $mkay ) {
92 $c->action->execute($c);
93 $error++ if scalar @{ $c->error };
98 if ( my $end = @{ $actions{end} }[-1] ) {
104 my $path = $c->req->path;
106 ? qq/Unknown resource "$path"/
107 : "No default action defined";
108 $c->log->error($error) if $c->debug;
113 =item $self->forward( $c, $command [, \@arguments ] )
123 $c->log->debug('Nothing to forward to') if $c->debug;
127 # Relative forwards from detach
128 my $caller = ( caller(1) )[0]->isa('Catalyst::Dispatcher')
129 && ( ( caller(2) )[3] =~ /::detach$/ ) ? caller(3) : caller(1);
131 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
135 my $command_copy = $command;
137 unless ( $command_copy =~ s/^\/// ) {
139 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} ) || '';
140 $command_copy = "${namespace}/${command}";
143 unless ( $command_copy =~ /\// ) {
144 $results = $c->get_action( $command_copy, '/' );
148 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
150 $results = $c->get_action( $tail, $1 );
153 push( @{$arguments}, @extra_args );
156 unshift( @extra_args, $tail );
160 unless ( @{$results} ) {
162 unless ( $c->components->{$command} ) {
164 qq/Couldn't forward to command "$command". Invalid action or component./;
166 $c->log->debug($error) if $c->debug;
170 my $class = $command;
171 my $method = shift || 'process';
173 if ( my $code = $c->components->{$class}->can($method) ) {
174 my $action = Catalyst::Action->new(
177 reverse => "$class->$method",
182 $results = [ [$action] ];
187 qq/Couldn't forward to "$class". Does not implement "$method"/;
189 $c->log->debug($error)
196 local $c->request->{arguments} = [ @{$arguments} ];
198 for my $result ( @{$results} ) {
199 $result->[0]->execute($c);
200 return if scalar @{ $c->error };
201 last unless $c->state;
207 =item $self->prepare_action($c)
212 my ( $self, $c ) = @_;
213 my $path = $c->req->path;
214 my @path = split /\//, $c->req->path;
215 $c->req->args( \my @args );
218 $path = join '/', @path;
219 if ( my $result = ${ $c->get_action($path) }[0] ) {
223 my $match = $result->[1];
224 my @snippets = @{ $result->[2] };
226 qq/Requested action is "$path" and matched "$match"/)
229 'Snippets are "' . join( ' ', @snippets ) . '"' )
230 if ( $c->debug && @snippets );
231 $c->req->action($match);
232 $c->req->snippets( \@snippets );
236 $c->req->action($path);
237 $c->log->debug(qq/Requested action is "$path"/) if $c->debug;
240 $c->req->match($path);
241 $c->action($result->[0]);
242 $c->namespace($result->[0]->prefix);
245 unshift @args, pop @path;
248 unless ( $c->req->action ) {
249 my $result = @{$c->get_action('default', $c->req->path, 1) || []}[-1];
251 $c->action( $result->[0] );
252 $c->namespace( $c->req->path );
253 $c->req->action('default');
258 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
259 if ( $c->debug && @args );
262 =item $self->get_action( $c, $action, $namespace, $inherit )
267 my ( $self, $c, $action, $namespace, $inherit ) = @_;
268 return [] unless $action;
274 my @match = $self->get_containers( $namespace );
278 foreach my $child ($inherit ? @match: $match[-1]) {
279 my $node = $child->actions;
280 push(@results, [ $node->{$action} ]) if defined $node->{$action};
285 elsif ( my $p = $self->actions->{plain}->{$action} ) { return [ [$p] ] }
286 elsif ( my $r = $self->actions->{regex}->{$action} ) { return [ [$r] ] }
290 for my $i ( 0 .. $#{ $self->actions->{compiled} } ) {
291 my $name = $self->actions->{compiled}->[$i]->[0];
292 my $regex = $self->actions->{compiled}->[$i]->[1];
294 if ( my @snippets = ( $action =~ $regex ) ) {
296 [ $self->actions->{regex}->{$name}, $name, \@snippets ] ];
304 =item $self->get_containers( $namespace )
309 my ( $self, $namespace ) = @_;
311 # If the namespace is / just return the root ActionContainer
313 return ($self->tree->getNodeValue) if $namespace eq '/';
315 # Use a visitor to recurse down the tree finding the ActionContainers
316 # for each namespace in the chain.
318 my $visitor = Tree::Simple::Visitor::FindByPath->new;
319 my @path = split('/', $namespace);
320 $visitor->setSearchPath( @path );
321 $self->tree->accept($visitor);
323 my @match = $visitor->getResults;
324 @match = ($self->tree) unless @match;
326 if (!defined $visitor->getResult) {
328 # If we don't manage to match, the visitor doesn't return the last
329 # node is matched, so foo/bar/baz would only find the 'foo' node,
330 # not the foo and foo/bar nodes as it should. This does another
331 # single-level search to see if that's the case, and the 'last unless'
332 # should catch any failures - or short-circuit this if this *is* a
333 # bug in the visitor and gets fixed.
335 my $extra = $path[(scalar @match) - 1];
337 $visitor->setSearchPath($extra);
338 $match[-1]->accept($visitor);
339 push(@match, $visitor->getResult) if defined $visitor->getResult;
342 return map { $_->getNodeValue } @match;
345 =item $self->set_action( $c, $action, $code, $namespace, $attrs )
350 my ( $self, $c, $method, $code, $namespace, $attrs ) = @_;
353 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
357 for my $attr ( @{$attrs} ) {
358 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
359 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
360 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) {
361 push @{ $flags{path} }, $1;
363 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
364 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
365 push @{ $flags{regex} }, $2;
369 if ( $flags{private} && ( keys %flags > 1 ) ) {
370 $c->log->debug( 'Bad action definition "'
371 . join( ' ', @{$attrs} )
372 . qq/" for "$namespace->$method"/ )
376 return unless keys %flags;
378 my $parent = $self->tree;
379 my $visitor = Tree::Simple::Visitor::FindByPath->new;
382 for my $part ( split '/', $prefix ) {
383 $visitor->setSearchPath($part);
384 $parent->accept($visitor);
385 my $child = $visitor->getResult;
389 # Create a new tree node and an ActionContainer to form
392 my $container = Catalyst::ActionContainer->new(
393 { part => $part, actions => {} });
394 $child = $parent->addChild( Tree::Simple->new($container) );
395 $visitor->setSearchPath($part);
396 $parent->accept($visitor);
397 $child = $visitor->getResult;
404 my $reverse = $prefix ? "$prefix/$method" : $method;
406 my $action = Catalyst::Action->new(
410 namespace => $namespace,
415 # Set the method value
416 $parent->getNodeValue->actions->{$method} = $action;
419 for my $path ( @{ $flags{path} } ) {
422 if ( $path =~ /^\s*'(.*)'\s*$/ ) { $path = $1 }
423 if ( $path =~ /^\s*"(.*)"\s*$/ ) { $path = $1 }
426 $flags{path} = \@path;
429 for my $regex ( @{ $flags{regex} } ) {
432 if ( $regex =~ /^\s*'(.*)'\s*$/ ) { $regex = $1 }
433 if ( $regex =~ /^\s*"(.*)"\s*$/ ) { $regex = $1 }
436 $flags{regex} = \@regex;
438 if ( $flags{local} || $flags{global} ) {
439 push( @{ $flags{path} }, $prefix ? "/$prefix/$method" : "/$method" )
442 push( @{ $flags{path} }, "/$method" ) if $flags{global};
445 for my $path ( @{ $flags{path} } ) {
446 if ( $path =~ /^\// ) { $path =~ s/^\/// }
447 else { $path = $prefix ? "$prefix/$path" : $path }
448 $self->actions->{plain}->{$path} = $action;
451 for my $regex ( @{ $flags{regex} } ) {
452 push @{ $self->actions->{compiled} }, [ $regex, qr#$regex# ];
453 $self->actions->{regex}->{$regex} = $action;
457 =item $self->setup_actions( $class, $component )
462 my ( $self, $class ) = @_;
464 # These are the core structures
475 my $container = Catalyst::ActionContainer->new(
476 { part => '/', actions => {} } );
477 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
479 for my $comp ( keys %{ $class->components } ) {
481 # We only setup components that inherit from Catalyst::Base
482 next unless $comp->isa('Catalyst::Base');
484 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
485 my ( $code, $attrs ) = @{$action};
488 my @cache = ( $comp, @{"$comp\::ISA"} );
491 while ( my $namespace = shift @cache ) {
492 $namespaces{$namespace}++;
493 for my $isa ( @{"$comp\::ISA"} ) {
494 next if $namespaces{$isa};
500 for my $namespace ( keys %namespaces ) {
502 for my $sym ( values %{ $namespace . '::' } ) {
504 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
506 $name = *{$sym}{NAME};
507 $class->set_action( $name, $code, $comp, $attrs );
519 return unless $class->debug;
521 my $actions = $self->actions;
522 my $privates = Text::ASCIITable->new;
523 $privates->setCols( 'Private', 'Class' );
524 $privates->setColWidth( 'Private', 36, 1 );
525 $privates->setColWidth( 'Class', 37, 1 );
528 my ( $walker, $parent, $prefix ) = @_;
529 $prefix .= $parent->getNodeValue || '';
530 $prefix .= '/' unless $prefix =~ /\/$/;
531 my $node = $parent->getNodeValue->actions;
533 for my $action ( keys %{ $node } ) {
534 my $action_obj = $node->{$action};
535 $privates->addRow( "$prefix$action", $action_obj->namespace );
538 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
541 $walker->( $walker, $self->tree, '' );
542 $class->log->debug( "Loaded private actions:\n" . $privates->draw )
543 if ( @{ $privates->{tbl_rows} } );
545 my $publics = Text::ASCIITable->new;
546 $publics->setCols( 'Public', 'Private' );
547 $publics->setColWidth( 'Public', 36, 1 );
548 $publics->setColWidth( 'Private', 37, 1 );
550 for my $plain ( sort keys %{ $actions->{plain} } ) {
551 my $action = $actions->{plain}->{$plain};
552 $publics->addRow( "/$plain", "/$action" );
555 $class->log->debug( "Loaded public actions:\n" . $publics->draw )
556 if ( @{ $publics->{tbl_rows} } );
558 my $regexes = Text::ASCIITable->new;
559 $regexes->setCols( 'Regex', 'Private' );
560 $regexes->setColWidth( 'Regex', 36, 1 );
561 $regexes->setColWidth( 'Private', 37, 1 );
563 for my $regex ( sort keys %{ $actions->{regex} } ) {
564 my $action = $actions->{regex}->{$regex};
565 $regexes->addRow( $regex, "/$action" );
568 $class->log->debug( "Loaded regex actions:\n" . $regexes->draw )
569 if ( @{ $regexes->{tbl_rows} } );
576 Sebastian Riedel, C<sri@cpan.org>
580 This program is free software, you can redistribute it and/or modify it under
581 the same terms as Perl itself.