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);
134 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
138 if ( $command =~ /^\// ) {
139 if ( $command =~ /^\/(\w+)$/ ) {
140 $results = $c->get_action( $1, $namespace );
143 my $command_copy = $command;
145 DESCEND: while ( $command_copy =~ s/^\/(.*)\/(\w+)$/\/$1/ ) {
147 $results = $c->get_action( $tail, $1 );
150 $namespace = $command_copy;
151 push( @{$arguments}, @extra_args );
154 unshift( @extra_args, $tail );
162 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} )
164 $results = $c->get_action( $command, $namespace );
167 unless ( @{$results} ) {
169 unless ( $c->components->{$command} ) {
171 qq/Couldn't forward to command "$command". Invalid action or component./;
173 $c->log->debug($error) if $c->debug;
177 my $class = $command;
178 my $method = shift || 'process';
180 if ( my $code = $c->components->{$class}->can($method) ) {
181 my $action = Catalyst::Action->new(
184 reverse => "$class->$method",
188 $results = [ [$action] ];
193 qq/Couldn't forward to "$class". Does not implement "$method"/;
195 $c->log->debug($error)
202 local $c->request->{arguments} = [ @{$arguments} ];
204 for my $result ( @{$results} ) {
205 $result->[0]->execute($c);
206 return if scalar @{ $c->error };
207 last unless $c->state;
213 =item $self->prepare_action($c)
218 my ( $self, $c ) = @_;
219 my $path = $c->req->path;
220 my @path = split /\//, $c->req->path;
221 $c->req->args( \my @args );
224 $path = join '/', @path;
225 if ( my $result = ${ $c->get_action($path) }[0] ) {
229 my $match = $result->[1];
230 my @snippets = @{ $result->[2] };
232 qq/Requested action is "$path" and matched "$match"/)
235 'Snippets are "' . join( ' ', @snippets ) . '"' )
236 if ( $c->debug && @snippets );
237 $c->req->action($match);
238 $c->req->snippets( \@snippets );
242 $c->req->action($path);
243 $c->log->debug(qq/Requested action is "$path"/) if $c->debug;
246 $c->req->match($path);
249 unshift @args, pop @path;
252 unless ( $c->req->action ) {
253 $c->req->action('default');
257 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
258 if ( $c->debug && @args );
261 =item $self->get_action( $c, $action, $namespace, $inherit )
266 my ( $self, $c, $action, $namespace, $inherit ) = @_;
267 return [] unless $action;
272 $namespace = '' if $namespace eq '/';
273 my $parent = $self->tree;
278 $self->actions->{private}->{ $parent->getUID }->{$action};
279 push @results, [$result] if $result;
280 my $visitor = Tree::Simple::Visitor::FindByPath->new;
283 for my $part ( split '/', $namespace ) {
284 $visitor->setSearchPath($part);
285 $parent->accept($visitor);
286 my $child = $visitor->getResult;
287 my $uid = $child->getUID if $child;
288 my $match = $self->actions->{private}->{$uid}->{$action}
290 push @results, [$match] if $match;
304 my $visitor = Tree::Simple::Visitor::FindByPath->new;
305 $visitor->setSearchPath( split '/', $namespace );
306 $parent->accept($visitor);
307 my $child = $visitor->getResult;
308 my $uid = $child->getUID if $child;
309 my $match = $self->actions->{private}->{$uid}->{$action}
311 push @results, [$match] if $match;
316 $self->actions->{private}->{ $parent->getUID }->{$action};
317 push @results, [$result] if $result;
324 elsif ( my $p = $self->actions->{plain}->{$action} ) { return [ [$p] ] }
325 elsif ( my $r = $self->actions->{regex}->{$action} ) { return [ [$r] ] }
329 for my $i ( 0 .. $#{ $self->actions->{compiled} } ) {
330 my $name = $self->actions->{compiled}->[$i]->[0];
331 my $regex = $self->actions->{compiled}->[$i]->[1];
333 if ( my @snippets = ( $action =~ $regex ) ) {
335 [ $self->actions->{regex}->{$name}, $name, \@snippets ] ];
343 =item $self->set_action( $c, $action, $code, $namespace, $attrs )
348 my ( $self, $c, $method, $code, $namespace, $attrs ) = @_;
351 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
355 for my $attr ( @{$attrs} ) {
356 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
357 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
358 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) {
359 push @{ $flags{path} }, $1;
361 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
362 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
363 push @{ $flags{regex} }, $2;
367 if ( $flags{private} && ( keys %flags > 1 ) ) {
368 $c->log->debug( 'Bad action definition "'
369 . join( ' ', @{$attrs} )
370 . qq/" for "$namespace->$method"/ )
374 return unless keys %flags;
376 my $parent = $self->tree;
377 my $visitor = Tree::Simple::Visitor::FindByPath->new;
379 for my $part ( split '/', $prefix ) {
380 $visitor->setSearchPath($part);
381 $parent->accept($visitor);
382 my $child = $visitor->getResult;
385 $child = $parent->addChild( Tree::Simple->new($part) );
386 $visitor->setSearchPath($part);
387 $parent->accept($visitor);
388 $child = $visitor->getResult;
394 my $reverse = $prefix ? "$prefix/$method" : $method;
396 my $action = Catalyst::Action->new(
400 namespace => $namespace,
404 my $uid = $parent->getUID;
405 $self->actions->{private}->{$uid}->{$method} = $action;
408 for my $path ( @{ $flags{path} } ) {
411 if ( $path =~ /^\s*'(.*)'\s*$/ ) { $path = $1 }
412 if ( $path =~ /^\s*"(.*)"\s*$/ ) { $path = $1 }
415 $flags{path} = \@path;
418 for my $regex ( @{ $flags{regex} } ) {
421 if ( $regex =~ /^\s*'(.*)'\s*$/ ) { $regex = $1 }
422 if ( $regex =~ /^\s*"(.*)"\s*$/ ) { $regex = $1 }
425 $flags{regex} = \@regex;
427 if ( $flags{local} || $flags{global} ) {
428 push( @{ $flags{path} }, $prefix ? "/$prefix/$method" : "/$method" )
431 push( @{ $flags{path} }, "/$method" ) if $flags{global};
434 for my $path ( @{ $flags{path} } ) {
435 if ( $path =~ /^\// ) { $path =~ s/^\/// }
436 else { $path = $prefix ? "$prefix/$path" : $path }
437 $self->actions->{plain}->{$path} = $action;
440 for my $regex ( @{ $flags{regex} } ) {
441 push @{ $self->actions->{compiled} }, [ $regex, qr#$regex# ];
442 $self->actions->{regex}->{$regex} = $action;
446 =item $self->setup_actions( $class, $component )
451 my ( $self, $class ) = @_;
453 # These are the core structures
464 $self->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
466 for my $comp ( keys %{ $class->components } ) {
468 # We only setup components that inherit from Catalyst::Base
469 next unless $comp->isa('Catalyst::Base');
471 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
472 my ( $code, $attrs ) = @{$action};
475 my @cache = ( $comp, @{"$comp\::ISA"} );
478 while ( my $namespace = shift @cache ) {
479 $namespaces{$namespace}++;
480 for my $isa ( @{"$comp\::ISA"} ) {
481 next if $namespaces{$isa};
487 for my $namespace ( keys %namespaces ) {
489 for my $sym ( values %{ $namespace . '::' } ) {
491 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
493 $name = *{$sym}{NAME};
494 $class->set_action( $name, $code, $comp, $attrs );
506 return unless $class->debug;
508 my $actions = $self->actions;
509 my $privates = Text::ASCIITable->new;
510 $privates->setCols( 'Private', 'Class' );
511 $privates->setColWidth( 'Private', 36, 1 );
512 $privates->setColWidth( 'Class', 37, 1 );
515 my ( $walker, $parent, $prefix ) = @_;
516 $prefix .= $parent->getNodeValue || '';
517 $prefix .= '/' unless $prefix =~ /\/$/;
518 my $uid = $parent->getUID;
520 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
521 my $action_obj = $actions->{private}->{$uid}->{$action};
522 $privates->addRow( "$prefix$action", $action_obj->namespace );
525 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
528 $walker->( $walker, $self->tree, '' );
529 $class->log->debug( "Loaded private actions:\n" . $privates->draw )
530 if ( @{ $privates->{tbl_rows} } );
532 my $publics = Text::ASCIITable->new;
533 $publics->setCols( 'Public', 'Private' );
534 $publics->setColWidth( 'Public', 36, 1 );
535 $publics->setColWidth( 'Private', 37, 1 );
537 for my $plain ( sort keys %{ $actions->{plain} } ) {
538 my $action = $actions->{plain}->{$plain};
539 $publics->addRow( "/$plain", "/$action" );
542 $class->log->debug( "Loaded public actions:\n" . $publics->draw )
543 if ( @{ $publics->{tbl_rows} } );
545 my $regexes = Text::ASCIITable->new;
546 $regexes->setCols( 'Regex', 'Private' );
547 $regexes->setColWidth( 'Regex', 36, 1 );
548 $regexes->setColWidth( 'Private', 37, 1 );
550 for my $regex ( sort keys %{ $actions->{regex} } ) {
551 my $action = $actions->{regex}->{$regex};
552 $regexes->addRow( $regex, "/$action" );
555 $class->log->debug( "Loaded regex actions:\n" . $regexes->draw )
556 if ( @{ $regexes->{tbl_rows} } );
563 Sebastian Riedel, C<sri@cpan.org>
567 This program is free software, you can redistribute it and/or modify it under
568 the same terms as Perl itself.