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 if ( $results = $c->get_action( $tail, $1 ) ) {
149 $namespace = $command_copy;
150 push( @{$arguments}, @extra_args );
153 unshift( @extra_args, $tail );
161 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} )
163 $results = $c->get_action( $command, $namespace );
166 unless ( @{$results} ) {
168 unless ( $c->components->{$command} ) {
170 qq/Couldn't forward to command "$command". Invalid action or component./;
172 $c->log->debug($error) if $c->debug;
176 my $class = $command;
177 my $method = shift || 'process';
179 if ( my $code = $c->components->{$class}->can($method) ) {
180 my $action = Catalyst::Action->new(
183 reverse => "$class->$method",
187 $results = [ [$action] ];
192 qq/Couldn't forward to "$class". Does not implement "$method"/;
194 $c->log->debug($error)
201 local $c->request->{arguments} = [ @{$arguments} ];
203 for my $result ( @{$results} ) {
204 $result->[0]->execute($c);
205 return if scalar @{ $c->error };
206 last unless $c->state;
212 =item $self->prepare_action($c)
217 my ( $self, $c ) = @_;
218 my $path = $c->req->path;
219 my @path = split /\//, $c->req->path;
220 $c->req->args( \my @args );
223 $path = join '/', @path;
224 if ( my $result = ${ $c->get_action($path) }[0] ) {
228 my $match = $result->[1];
229 my @snippets = @{ $result->[2] };
231 qq/Requested action is "$path" and matched "$match"/)
234 'Snippets are "' . join( ' ', @snippets ) . '"' )
235 if ( $c->debug && @snippets );
236 $c->req->action($match);
237 $c->req->snippets( \@snippets );
241 $c->req->action($path);
242 $c->log->debug(qq/Requested action is "$path"/) if $c->debug;
245 $c->req->match($path);
248 unshift @args, pop @path;
251 unless ( $c->req->action ) {
252 $c->req->action('default');
256 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
257 if ( $c->debug && @args );
260 =item $self->get_action( $c, $action, $namespace, $inherit )
265 my ( $self, $c, $action, $namespace, $inherit ) = @_;
266 return [] unless $action;
271 $namespace = '' if $namespace eq '/';
272 my $parent = $self->tree;
277 $self->actions->{private}->{ $parent->getUID }->{$action};
278 push @results, [$result] if $result;
279 my $visitor = Tree::Simple::Visitor::FindByPath->new;
282 for my $part ( split '/', $namespace ) {
283 $visitor->setSearchPath($part);
284 $parent->accept($visitor);
285 my $child = $visitor->getResult;
286 my $uid = $child->getUID if $child;
287 my $match = $self->actions->{private}->{$uid}->{$action}
289 push @results, [$match] if $match;
303 my $visitor = Tree::Simple::Visitor::FindByPath->new;
304 $visitor->setSearchPath( split '/', $namespace );
305 $parent->accept($visitor);
306 my $child = $visitor->getResult;
307 my $uid = $child->getUID if $child;
308 my $match = $self->actions->{private}->{$uid}->{$action}
310 push @results, [$match] if $match;
315 $self->actions->{private}->{ $parent->getUID }->{$action};
316 push @results, [$result] if $result;
323 elsif ( my $p = $self->actions->{plain}->{$action} ) { return [ [$p] ] }
324 elsif ( my $r = $self->actions->{regex}->{$action} ) { return [ [$r] ] }
328 for my $i ( 0 .. $#{ $self->actions->{compiled} } ) {
329 my $name = $self->actions->{compiled}->[$i]->[0];
330 my $regex = $self->actions->{compiled}->[$i]->[1];
332 if ( my @snippets = ( $action =~ $regex ) ) {
334 [ $self->actions->{regex}->{$name}, $name, \@snippets ] ];
342 =item $self->set_action( $c, $action, $code, $namespace, $attrs )
347 my ( $self, $c, $method, $code, $namespace, $attrs ) = @_;
350 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
354 for my $attr ( @{$attrs} ) {
355 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
356 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
357 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) {
358 push @{ $flags{path} }, $1;
360 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
361 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
362 push @{ $flags{regex} }, $2;
366 if ( $flags{private} && ( keys %flags > 1 ) ) {
367 $c->log->debug( 'Bad action definition "'
368 . join( ' ', @{$attrs} )
369 . qq/" for "$namespace->$method"/ )
373 return unless keys %flags;
375 my $parent = $self->tree;
376 my $visitor = Tree::Simple::Visitor::FindByPath->new;
378 for my $part ( split '/', $prefix ) {
379 $visitor->setSearchPath($part);
380 $parent->accept($visitor);
381 my $child = $visitor->getResult;
384 $child = $parent->addChild( Tree::Simple->new($part) );
385 $visitor->setSearchPath($part);
386 $parent->accept($visitor);
387 $child = $visitor->getResult;
393 my $reverse = $prefix ? "$prefix/$method" : $method;
395 my $action = Catalyst::Action->new(
399 namespace => $namespace,
403 my $uid = $parent->getUID;
404 $self->actions->{private}->{$uid}->{$method} = $action;
407 for my $path ( @{ $flags{path} } ) {
410 if ( $path =~ /^\s*'(.*)'\s*$/ ) { $path = $1 }
411 if ( $path =~ /^\s*"(.*)"\s*$/ ) { $path = $1 }
414 $flags{path} = \@path;
417 for my $regex ( @{ $flags{regex} } ) {
420 if ( $regex =~ /^\s*'(.*)'\s*$/ ) { $regex = $1 }
421 if ( $regex =~ /^\s*"(.*)"\s*$/ ) { $regex = $1 }
424 $flags{regex} = \@regex;
426 if ( $flags{local} || $flags{global} ) {
427 push( @{ $flags{path} }, $prefix ? "/$prefix/$method" : "/$method" )
430 push( @{ $flags{path} }, "/$method" ) if $flags{global};
433 for my $path ( @{ $flags{path} } ) {
434 if ( $path =~ /^\// ) { $path =~ s/^\/// }
435 else { $path = $prefix ? "$prefix/$path" : $path }
436 $self->actions->{plain}->{$path} = $action;
439 for my $regex ( @{ $flags{regex} } ) {
440 push @{ $self->actions->{compiled} }, [ $regex, qr#$regex# ];
441 $self->actions->{regex}->{$regex} = $action;
445 =item $self->setup_actions( $class, $component )
450 my ( $self, $class ) = @_;
452 # These are the core structures
463 $self->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
465 for my $comp ( keys %{ $class->components } ) {
467 # We only setup components that inherit from Catalyst::Base
468 next unless $comp->isa('Catalyst::Base');
470 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
471 my ( $code, $attrs ) = @{$action};
474 my @cache = ( $comp, @{"$comp\::ISA"} );
477 while ( my $namespace = shift @cache ) {
478 $namespaces{$namespace}++;
479 for my $isa ( @{"$comp\::ISA"} ) {
480 next if $namespaces{$isa};
486 for my $namespace ( keys %namespaces ) {
488 for my $sym ( values %{ $namespace . '::' } ) {
490 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
492 $name = *{$sym}{NAME};
493 $class->set_action( $name, $code, $comp, $attrs );
505 return unless $class->debug;
507 my $actions = $self->actions;
508 my $privates = Text::ASCIITable->new;
509 $privates->setCols( 'Private', 'Class' );
510 $privates->setColWidth( 'Private', 36, 1 );
511 $privates->setColWidth( 'Class', 37, 1 );
514 my ( $walker, $parent, $prefix ) = @_;
515 $prefix .= $parent->getNodeValue || '';
516 $prefix .= '/' unless $prefix =~ /\/$/;
517 my $uid = $parent->getUID;
519 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
520 my $action_obj = $actions->{private}->{$uid}->{$action};
521 $privates->addRow( "$prefix$action", $action_obj->namespace );
524 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
527 $walker->( $walker, $self->tree, '' );
528 $class->log->debug( "Loaded private actions:\n" . $privates->draw )
529 if ( @{ $privates->{tbl_rows} } );
531 my $publics = Text::ASCIITable->new;
532 $publics->setCols( 'Public', 'Private' );
533 $publics->setColWidth( 'Public', 36, 1 );
534 $publics->setColWidth( 'Private', 37, 1 );
536 for my $plain ( sort keys %{ $actions->{plain} } ) {
537 my $action = $actions->{plain}->{$plain};
538 $publics->addRow( "/$plain", "/$action" );
541 $class->log->debug( "Loaded public actions:\n" . $publics->draw )
542 if ( @{ $publics->{tbl_rows} } );
544 my $regexes = Text::ASCIITable->new;
545 $regexes->setCols( 'Regex', 'Private' );
546 $regexes->setColWidth( 'Regex', 36, 1 );
547 $regexes->setColWidth( 'Private', 37, 1 );
549 for my $regex ( sort keys %{ $actions->{regex} } ) {
550 my $action = $actions->{regex}->{$regex};
551 $regexes->addRow( $regex, "/$action" );
554 $class->log->debug( "Loaded regex actions:\n" . $regexes->draw )
555 if ( @{ $regexes->{tbl_rows} } );
562 Sebastian Riedel, C<sri@cpan.org>
566 This program is free software, you can redistribute it and/or modify it under
567 the same terms as Perl itself.