1 package Catalyst::Dispatcher;
4 use base 'Class::Accessor::Fast';
5 use Catalyst::Exception;
10 use Tree::Simple::Visitor::FindByPath;
13 use overload '""' => sub { return ref shift }, fallback => 1;
15 __PACKAGE__->mk_accessors(qw/actions tree/);
19 Catalyst::Dispatcher - The Catalyst Dispatcher
31 =item $self->detach( $c, $command [, \@arguments ] )
36 my ( $self, $c, $command, @args ) = @_;
37 $c->forward( $command, @args ) if $command;
38 die $Catalyst::DETACH;
41 =item $self->dispatch($c)
46 my ( $self, $c ) = @_;
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) ) {
55 Catalyst::Utils::class2prefix( $result->[0]->[0]->namespace,
56 $c->config->{case_sensitive} );
60 my $default = $action eq 'default' ? $namespace : undef;
61 my $results = $c->get_action( $action, $default, $default ? 1 : 0 );
66 # Errors break the normal flow and the end action is instantly run
71 if ( my $begin = @{ $c->get_action( 'begin', $namespace, 1 ) }[-1] ) {
72 $begin->[0]->execute($c);
73 $error++ if scalar @{ $c->error };
76 # Execute the auto chain
78 for my $auto ( @{ $c->get_action( 'auto', $namespace, 1 ) } ) {
81 $auto->[0]->execute($c);
82 $error++ if scalar @{ $c->error };
83 last unless $c->state;
86 # Execute the action or last default
87 my $mkay = $autorun ? $c->state ? 1 : 0 : 1;
88 if ( ( my $action = $c->req->action ) && $mkay ) {
91 @{ $c->get_action( $action, $default, 1 ) }[-1] )
93 $result->[0]->execute($c);
94 $error++ if scalar @{ $c->error };
100 if ( my $end = @{ $c->get_action( 'end', $namespace, 1 ) }[-1] ) {
101 $end->[0]->execute($c);
106 my $path = $c->req->path;
108 ? qq/Unknown resource "$path"/
109 : "No default action defined";
110 $c->log->error($error) if $c->debug;
115 =item $self->forward( $c, $command [, \@arguments ] )
125 $c->log->debug('Nothing to forward to') if $c->debug;
129 # Relative forwards from detach
130 my $caller = ( caller(1) )[0]->isa('Catalyst::Dispatcher')
131 && ( ( caller(2) )[3] =~ /::detach$/ ) ? caller(3) : caller(1);
133 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
137 my $command_copy = $command;
139 unless ( $command_copy =~ s/^\/// ) {
141 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} ) || '';
142 $command_copy = "${namespace}/${command}";
145 unless ( $command_copy =~ /\// ) {
146 $results = $c->get_action( $command_copy, '/' );
150 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
152 $results = $c->get_action( $tail, $1 );
155 push( @{$arguments}, @extra_args );
158 unshift( @extra_args, $tail );
162 unless ( @{$results} ) {
164 unless ( $c->components->{$command} ) {
166 qq/Couldn't forward to command "$command". Invalid action or component./;
168 $c->log->debug($error) if $c->debug;
172 my $class = $command;
173 my $method = shift || 'process';
175 if ( my $code = $c->components->{$class}->can($method) ) {
176 my $action = Catalyst::Action->new(
179 reverse => "$class->$method",
183 $results = [ [$action] ];
188 qq/Couldn't forward to "$class". Does not implement "$method"/;
190 $c->log->debug($error)
197 local $c->request->{arguments} = [ @{$arguments} ];
199 for my $result ( @{$results} ) {
200 $result->[0]->execute($c);
201 return if scalar @{ $c->error };
202 last unless $c->state;
208 =item $self->prepare_action($c)
213 my ( $self, $c ) = @_;
214 my $path = $c->req->path;
215 my @path = split /\//, $c->req->path;
216 $c->req->args( \my @args );
219 $path = join '/', @path;
220 if ( my $result = ${ $c->get_action($path) }[0] ) {
224 my $match = $result->[1];
225 my @snippets = @{ $result->[2] };
227 qq/Requested action is "$path" and matched "$match"/)
230 'Snippets are "' . join( ' ', @snippets ) . '"' )
231 if ( $c->debug && @snippets );
232 $c->req->action($match);
233 $c->req->snippets( \@snippets );
237 $c->req->action($path);
238 $c->log->debug(qq/Requested action is "$path"/) if $c->debug;
241 $c->req->match($path);
244 unshift @args, pop @path;
247 unless ( $c->req->action ) {
248 $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 $parent = $self->tree;
271 if ($namespace ne '/') {
273 my $visitor = Tree::Simple::Visitor::FindByPath->new;
274 my @path = split('/', $namespace);
275 $visitor->setSearchPath( @path );
276 $parent->accept($visitor);
280 @match = $visitor->getResults;
281 @match = ($parent) unless @match;
283 if (!defined $visitor->getResult) {
284 my $extra = $path[(scalar @match) - 1];
286 $visitor->setSearchPath($extra);
287 $match[-1]->accept($visitor);
288 push(@match, $visitor->getResult) if defined $visitor->getResult;
291 @match = ($visitor->getResult) if $visitor->getResult;
296 @match = ($parent) unless @match;
300 foreach my $child (@match) {
301 my $node = $self->actions->{private}->{$child->getUID};
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;
363 for my $part ( split '/', $prefix ) {
364 $visitor->setSearchPath($part);
365 $parent->accept($visitor);
366 my $child = $visitor->getResult;
369 $child = $parent->addChild( Tree::Simple->new($part) );
370 $visitor->setSearchPath($part);
371 $parent->accept($visitor);
372 $child = $visitor->getResult;
378 my $reverse = $prefix ? "$prefix/$method" : $method;
380 my $action = Catalyst::Action->new(
384 namespace => $namespace,
388 my $uid = $parent->getUID;
389 $self->actions->{private}->{$uid}->{$method} = $action;
392 for my $path ( @{ $flags{path} } ) {
395 if ( $path =~ /^\s*'(.*)'\s*$/ ) { $path = $1 }
396 if ( $path =~ /^\s*"(.*)"\s*$/ ) { $path = $1 }
399 $flags{path} = \@path;
402 for my $regex ( @{ $flags{regex} } ) {
405 if ( $regex =~ /^\s*'(.*)'\s*$/ ) { $regex = $1 }
406 if ( $regex =~ /^\s*"(.*)"\s*$/ ) { $regex = $1 }
409 $flags{regex} = \@regex;
411 if ( $flags{local} || $flags{global} ) {
412 push( @{ $flags{path} }, $prefix ? "/$prefix/$method" : "/$method" )
415 push( @{ $flags{path} }, "/$method" ) if $flags{global};
418 for my $path ( @{ $flags{path} } ) {
419 if ( $path =~ /^\// ) { $path =~ s/^\/// }
420 else { $path = $prefix ? "$prefix/$path" : $path }
421 $self->actions->{plain}->{$path} = $action;
424 for my $regex ( @{ $flags{regex} } ) {
425 push @{ $self->actions->{compiled} }, [ $regex, qr#$regex# ];
426 $self->actions->{regex}->{$regex} = $action;
430 =item $self->setup_actions( $class, $component )
435 my ( $self, $class ) = @_;
437 # These are the core structures
448 $self->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
450 for my $comp ( keys %{ $class->components } ) {
452 # We only setup components that inherit from Catalyst::Base
453 next unless $comp->isa('Catalyst::Base');
455 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
456 my ( $code, $attrs ) = @{$action};
459 my @cache = ( $comp, @{"$comp\::ISA"} );
462 while ( my $namespace = shift @cache ) {
463 $namespaces{$namespace}++;
464 for my $isa ( @{"$comp\::ISA"} ) {
465 next if $namespaces{$isa};
471 for my $namespace ( keys %namespaces ) {
473 for my $sym ( values %{ $namespace . '::' } ) {
475 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
477 $name = *{$sym}{NAME};
478 $class->set_action( $name, $code, $comp, $attrs );
490 return unless $class->debug;
492 my $actions = $self->actions;
493 my $privates = Text::ASCIITable->new;
494 $privates->setCols( 'Private', 'Class' );
495 $privates->setColWidth( 'Private', 36, 1 );
496 $privates->setColWidth( 'Class', 37, 1 );
499 my ( $walker, $parent, $prefix ) = @_;
500 $prefix .= $parent->getNodeValue || '';
501 $prefix .= '/' unless $prefix =~ /\/$/;
502 my $uid = $parent->getUID;
504 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
505 my $action_obj = $actions->{private}->{$uid}->{$action};
506 $privates->addRow( "$prefix$action", $action_obj->namespace );
509 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
512 $walker->( $walker, $self->tree, '' );
513 $class->log->debug( "Loaded private actions:\n" . $privates->draw )
514 if ( @{ $privates->{tbl_rows} } );
516 my $publics = Text::ASCIITable->new;
517 $publics->setCols( 'Public', 'Private' );
518 $publics->setColWidth( 'Public', 36, 1 );
519 $publics->setColWidth( 'Private', 37, 1 );
521 for my $plain ( sort keys %{ $actions->{plain} } ) {
522 my $action = $actions->{plain}->{$plain};
523 $publics->addRow( "/$plain", "/$action" );
526 $class->log->debug( "Loaded public actions:\n" . $publics->draw )
527 if ( @{ $publics->{tbl_rows} } );
529 my $regexes = Text::ASCIITable->new;
530 $regexes->setCols( 'Regex', 'Private' );
531 $regexes->setColWidth( 'Regex', 36, 1 );
532 $regexes->setColWidth( 'Private', 37, 1 );
534 for my $regex ( sort keys %{ $actions->{regex} } ) {
535 my $action = $actions->{regex}->{$regex};
536 $regexes->addRow( $regex, "/$action" );
539 $class->log->debug( "Loaded regex actions:\n" . $regexes->draw )
540 if ( @{ $regexes->{tbl_rows} } );
547 Sebastian Riedel, C<sri@cpan.org>
551 This program is free software, you can redistribute it and/or modify it under
552 the same terms as Perl itself.