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 ) = @_;
48 my $action = $c->req->action;
50 $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
51 if $action eq 'default';
54 if ( my $result = $c->get_action($action) ) {
55 $namespace = $result->[0]->[0]->prefix;
59 my $default = $action eq 'default' ? $namespace : undef;
60 my $results = $c->get_action( $action, $default, $default ? 1 : 0 );
63 my @containers = $self->get_containers( $namespace );
65 foreach my $name (qw/begin auto end/) {
68 grep { exists $_->{$name} }
76 # Errors break the normal flow and the end action is instantly run
81 if ( my $begin = @{ $actions{begin} }[-1] ) {
83 $error++ if scalar @{ $c->error };
86 # Execute the auto chain
88 for my $auto ( @{ $actions{auto} } ) {
92 $error++ if scalar @{ $c->error };
93 last unless $c->state;
96 # Execute the action or last default
97 my $mkay = $autorun ? $c->state ? 1 : 0 : 1;
98 if ( ( my $action = $c->req->action ) && $mkay ) {
101 @{ $c->get_action( $action, $default, 1 ) }[-1] )
103 $result->[0]->execute($c);
104 $error++ if scalar @{ $c->error };
110 if ( my $end = @{ $actions{end} }[-1] ) {
116 my $path = $c->req->path;
118 ? qq/Unknown resource "$path"/
119 : "No default action defined";
120 $c->log->error($error) if $c->debug;
125 =item $self->forward( $c, $command [, \@arguments ] )
135 $c->log->debug('Nothing to forward to') if $c->debug;
139 # Relative forwards from detach
140 my $caller = ( caller(1) )[0]->isa('Catalyst::Dispatcher')
141 && ( ( caller(2) )[3] =~ /::detach$/ ) ? caller(3) : caller(1);
143 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
147 my $command_copy = $command;
149 unless ( $command_copy =~ s/^\/// ) {
151 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} ) || '';
152 $command_copy = "${namespace}/${command}";
155 unless ( $command_copy =~ /\// ) {
156 $results = $c->get_action( $command_copy, '/' );
160 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
162 $results = $c->get_action( $tail, $1 );
165 push( @{$arguments}, @extra_args );
168 unshift( @extra_args, $tail );
172 unless ( @{$results} ) {
174 unless ( $c->components->{$command} ) {
176 qq/Couldn't forward to command "$command". Invalid action or component./;
178 $c->log->debug($error) if $c->debug;
182 my $class = $command;
183 my $method = shift || 'process';
185 if ( my $code = $c->components->{$class}->can($method) ) {
186 my $action = Catalyst::Action->new(
189 reverse => "$class->$method",
194 $results = [ [$action] ];
199 qq/Couldn't forward to "$class". Does not implement "$method"/;
201 $c->log->debug($error)
208 local $c->request->{arguments} = [ @{$arguments} ];
210 for my $result ( @{$results} ) {
211 $result->[0]->execute($c);
212 return if scalar @{ $c->error };
213 last unless $c->state;
219 =item $self->prepare_action($c)
224 my ( $self, $c ) = @_;
225 my $path = $c->req->path;
226 my @path = split /\//, $c->req->path;
227 $c->req->args( \my @args );
230 $path = join '/', @path;
231 if ( my $result = ${ $c->get_action($path) }[0] ) {
235 my $match = $result->[1];
236 my @snippets = @{ $result->[2] };
238 qq/Requested action is "$path" and matched "$match"/)
241 'Snippets are "' . join( ' ', @snippets ) . '"' )
242 if ( $c->debug && @snippets );
243 $c->req->action($match);
244 $c->req->snippets( \@snippets );
248 $c->req->action($path);
249 $c->log->debug(qq/Requested action is "$path"/) if $c->debug;
252 $c->req->match($path);
255 unshift @args, pop @path;
258 unless ( $c->req->action ) {
259 $c->req->action('default');
263 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
264 if ( $c->debug && @args );
267 =item $self->get_action( $c, $action, $namespace, $inherit )
272 my ( $self, $c, $action, $namespace, $inherit ) = @_;
273 return [] unless $action;
279 my @match = $self->get_containers( $namespace );
283 foreach my $child ($inherit ? @match: $match[-1]) {
284 my $node = $child->actions;
285 push(@results, [ $node->{$action} ]) if defined $node->{$action};
290 elsif ( my $p = $self->actions->{plain}->{$action} ) { return [ [$p] ] }
291 elsif ( my $r = $self->actions->{regex}->{$action} ) { return [ [$r] ] }
295 for my $i ( 0 .. $#{ $self->actions->{compiled} } ) {
296 my $name = $self->actions->{compiled}->[$i]->[0];
297 my $regex = $self->actions->{compiled}->[$i]->[1];
299 if ( my @snippets = ( $action =~ $regex ) ) {
301 [ $self->actions->{regex}->{$name}, $name, \@snippets ] ];
309 =item $self->get_containers( $namespace )
314 my ( $self, $namespace ) = @_;
316 return ($self->tree->getNodeValue) if $namespace eq '/';
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) {
327 my $extra = $path[(scalar @match) - 1];
329 $visitor->setSearchPath($extra);
330 $match[-1]->accept($visitor);
331 push(@match, $visitor->getResult) if defined $visitor->getResult;
334 return map { $_->getNodeValue } @match;
337 =item $self->set_action( $c, $action, $code, $namespace, $attrs )
342 my ( $self, $c, $method, $code, $namespace, $attrs ) = @_;
345 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
349 for my $attr ( @{$attrs} ) {
350 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
351 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
352 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) {
353 push @{ $flags{path} }, $1;
355 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
356 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
357 push @{ $flags{regex} }, $2;
361 if ( $flags{private} && ( keys %flags > 1 ) ) {
362 $c->log->debug( 'Bad action definition "'
363 . join( ' ', @{$attrs} )
364 . qq/" for "$namespace->$method"/ )
368 return unless keys %flags;
370 my $parent = $self->tree;
371 my $visitor = Tree::Simple::Visitor::FindByPath->new;
374 for my $part ( split '/', $prefix ) {
375 $visitor->setSearchPath($part);
376 $parent->accept($visitor);
377 my $child = $visitor->getResult;
380 my $container = Catalyst::ActionContainer->new(
381 { part => $part, actions => {} });
382 $child = $parent->addChild( Tree::Simple->new($container) );
383 $visitor->setSearchPath($part);
384 $parent->accept($visitor);
385 $child = $visitor->getResult;
392 my $reverse = $prefix ? "$prefix/$method" : $method;
394 my $action = Catalyst::Action->new(
398 namespace => $namespace,
403 $parent->getNodeValue->actions->{$method} = $action;
406 for my $path ( @{ $flags{path} } ) {
409 if ( $path =~ /^\s*'(.*)'\s*$/ ) { $path = $1 }
410 if ( $path =~ /^\s*"(.*)"\s*$/ ) { $path = $1 }
413 $flags{path} = \@path;
416 for my $regex ( @{ $flags{regex} } ) {
419 if ( $regex =~ /^\s*'(.*)'\s*$/ ) { $regex = $1 }
420 if ( $regex =~ /^\s*"(.*)"\s*$/ ) { $regex = $1 }
423 $flags{regex} = \@regex;
425 if ( $flags{local} || $flags{global} ) {
426 push( @{ $flags{path} }, $prefix ? "/$prefix/$method" : "/$method" )
429 push( @{ $flags{path} }, "/$method" ) if $flags{global};
432 for my $path ( @{ $flags{path} } ) {
433 if ( $path =~ /^\// ) { $path =~ s/^\/// }
434 else { $path = $prefix ? "$prefix/$path" : $path }
435 $self->actions->{plain}->{$path} = $action;
438 for my $regex ( @{ $flags{regex} } ) {
439 push @{ $self->actions->{compiled} }, [ $regex, qr#$regex# ];
440 $self->actions->{regex}->{$regex} = $action;
444 =item $self->setup_actions( $class, $component )
449 my ( $self, $class ) = @_;
451 # These are the core structures
462 my $container = Catalyst::ActionContainer->new(
463 { part => '/', actions => {} } );
464 $self->tree( Tree::Simple->new( $container, 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 $node = $parent->getNodeValue->actions;
520 for my $action ( keys %{ $node } ) {
521 my $action_obj = $node->{$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.