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;
267 $namespace = '' if $namespace eq '/';
268 my $parent = $self->tree;
273 # $self->actions->{private}->{ $parent->getUID }->{$action};
274 #push @results, [$result] if $result;
275 my $visitor = Tree::Simple::Visitor::FindByPath->new;
277 #warn "${namespace} ${action}";
278 my @path = split(/\//, $namespace);
279 $visitor->setSearchPath( @path );
280 $parent->accept($visitor);
282 #warn join(', ', map { $_->getNodeValue } $visitor->getResults);
284 my @match = $visitor->getResults;
285 @match = ($parent) unless @match;
287 if (!defined $visitor->getResult) {
288 my $extra = $path[(scalar @match) - 1];
290 $visitor->setSearchPath($extra);
291 $match[-1]->accept($visitor);
292 push(@match, $visitor->getResult) if defined $visitor->getResult;
295 foreach my $child (@match) {
296 my $node = $self->actions->{private}->{$child->getUID};
298 push(@results, [ $node->{$action} ]) if defined $node->{$action};
302 # for my $part ( split '/', $namespace ) {
303 # $visitor->setSearchPath($part);
304 # $parent->accept($visitor);
305 # my $child = $visitor->getResult;
306 # my $uid = $child->getUID if $child;
307 # my $match = $self->actions->{private}->{$uid}->{$action}
309 # push @results, [$match] if $match;
323 my $visitor = Tree::Simple::Visitor::FindByPath->new;
324 $visitor->setSearchPath( split '/', $namespace );
325 $parent->accept($visitor);
326 my $child = $visitor->getResult;
327 my $uid = $child->getUID if $child;
328 my $match = $self->actions->{private}->{$uid}->{$action}
330 push @results, [$match] if $match;
335 $self->actions->{private}->{ $parent->getUID }->{$action};
336 push @results, [$result] if $result;
343 elsif ( my $p = $self->actions->{plain}->{$action} ) { return [ [$p] ] }
344 elsif ( my $r = $self->actions->{regex}->{$action} ) { return [ [$r] ] }
348 for my $i ( 0 .. $#{ $self->actions->{compiled} } ) {
349 my $name = $self->actions->{compiled}->[$i]->[0];
350 my $regex = $self->actions->{compiled}->[$i]->[1];
352 if ( my @snippets = ( $action =~ $regex ) ) {
354 [ $self->actions->{regex}->{$name}, $name, \@snippets ] ];
362 =item $self->set_action( $c, $action, $code, $namespace, $attrs )
367 my ( $self, $c, $method, $code, $namespace, $attrs ) = @_;
370 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
374 for my $attr ( @{$attrs} ) {
375 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
376 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
377 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) {
378 push @{ $flags{path} }, $1;
380 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
381 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
382 push @{ $flags{regex} }, $2;
386 if ( $flags{private} && ( keys %flags > 1 ) ) {
387 $c->log->debug( 'Bad action definition "'
388 . join( ' ', @{$attrs} )
389 . qq/" for "$namespace->$method"/ )
393 return unless keys %flags;
395 my $parent = $self->tree;
396 my $visitor = Tree::Simple::Visitor::FindByPath->new;
398 for my $part ( split '/', $prefix ) {
399 $visitor->setSearchPath($part);
400 $parent->accept($visitor);
401 my $child = $visitor->getResult;
404 $child = $parent->addChild( Tree::Simple->new($part) );
405 $visitor->setSearchPath($part);
406 $parent->accept($visitor);
407 $child = $visitor->getResult;
413 my $reverse = $prefix ? "$prefix/$method" : $method;
415 my $action = Catalyst::Action->new(
419 namespace => $namespace,
423 my $uid = $parent->getUID;
424 $self->actions->{private}->{$uid}->{$method} = $action;
427 for my $path ( @{ $flags{path} } ) {
430 if ( $path =~ /^\s*'(.*)'\s*$/ ) { $path = $1 }
431 if ( $path =~ /^\s*"(.*)"\s*$/ ) { $path = $1 }
434 $flags{path} = \@path;
437 for my $regex ( @{ $flags{regex} } ) {
440 if ( $regex =~ /^\s*'(.*)'\s*$/ ) { $regex = $1 }
441 if ( $regex =~ /^\s*"(.*)"\s*$/ ) { $regex = $1 }
444 $flags{regex} = \@regex;
446 if ( $flags{local} || $flags{global} ) {
447 push( @{ $flags{path} }, $prefix ? "/$prefix/$method" : "/$method" )
450 push( @{ $flags{path} }, "/$method" ) if $flags{global};
453 for my $path ( @{ $flags{path} } ) {
454 if ( $path =~ /^\// ) { $path =~ s/^\/// }
455 else { $path = $prefix ? "$prefix/$path" : $path }
456 $self->actions->{plain}->{$path} = $action;
459 for my $regex ( @{ $flags{regex} } ) {
460 push @{ $self->actions->{compiled} }, [ $regex, qr#$regex# ];
461 $self->actions->{regex}->{$regex} = $action;
465 =item $self->setup_actions( $class, $component )
470 my ( $self, $class ) = @_;
472 # These are the core structures
483 $self->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
485 for my $comp ( keys %{ $class->components } ) {
487 # We only setup components that inherit from Catalyst::Base
488 next unless $comp->isa('Catalyst::Base');
490 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
491 my ( $code, $attrs ) = @{$action};
494 my @cache = ( $comp, @{"$comp\::ISA"} );
497 while ( my $namespace = shift @cache ) {
498 $namespaces{$namespace}++;
499 for my $isa ( @{"$comp\::ISA"} ) {
500 next if $namespaces{$isa};
506 for my $namespace ( keys %namespaces ) {
508 for my $sym ( values %{ $namespace . '::' } ) {
510 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
512 $name = *{$sym}{NAME};
513 $class->set_action( $name, $code, $comp, $attrs );
525 return unless $class->debug;
527 my $actions = $self->actions;
528 my $privates = Text::ASCIITable->new;
529 $privates->setCols( 'Private', 'Class' );
530 $privates->setColWidth( 'Private', 36, 1 );
531 $privates->setColWidth( 'Class', 37, 1 );
534 my ( $walker, $parent, $prefix ) = @_;
535 $prefix .= $parent->getNodeValue || '';
536 $prefix .= '/' unless $prefix =~ /\/$/;
537 my $uid = $parent->getUID;
539 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
540 my $action_obj = $actions->{private}->{$uid}->{$action};
541 $privates->addRow( "$prefix$action", $action_obj->namespace );
544 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
547 $walker->( $walker, $self->tree, '' );
548 $class->log->debug( "Loaded private actions:\n" . $privates->draw )
549 if ( @{ $privates->{tbl_rows} } );
551 my $publics = Text::ASCIITable->new;
552 $publics->setCols( 'Public', 'Private' );
553 $publics->setColWidth( 'Public', 36, 1 );
554 $publics->setColWidth( 'Private', 37, 1 );
556 for my $plain ( sort keys %{ $actions->{plain} } ) {
557 my $action = $actions->{plain}->{$plain};
558 $publics->addRow( "/$plain", "/$action" );
561 $class->log->debug( "Loaded public actions:\n" . $publics->draw )
562 if ( @{ $publics->{tbl_rows} } );
564 my $regexes = Text::ASCIITable->new;
565 $regexes->setCols( 'Regex', 'Private' );
566 $regexes->setColWidth( 'Regex', 36, 1 );
567 $regexes->setColWidth( 'Private', 37, 1 );
569 for my $regex ( sort keys %{ $actions->{regex} } ) {
570 my $action = $actions->{regex}->{$regex};
571 $regexes->addRow( $regex, "/$action" );
574 $class->log->debug( "Loaded regex actions:\n" . $regexes->draw )
575 if ( @{ $regexes->{tbl_rows} } );
582 Sebastian Riedel, C<sri@cpan.org>
586 This program is free software, you can redistribute it and/or modify it under
587 the same terms as Perl itself.