1 package Catalyst::Dispatcher;
4 use base 'Class::Data::Inheritable';
8 use Tree::Simple::Visitor::FindByPath;
10 __PACKAGE__->mk_classdata($_) for qw/actions tree/;
14 Catalyst::Dispatcher - The Catalyst Dispatcher
28 Dispatch request to actions.
34 my $action = $c->req->action;
36 $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
37 if $action eq 'default';
40 if ( my $result = $c->get_action($action) ) {
42 Catalyst::Utils::class2prefix( $result->[0]->[0]->[0] );
46 my $default = $action eq 'default' ? $namespace : undef;
47 my $results = $c->get_action( $action, $default, $default ? 1 : 0 );
54 if ( my $begin = @{ $c->get_action( 'begin', $namespace, 1 ) }[-1] ) {
55 $c->execute( @{ $begin->[0] } );
56 return if scalar @{ $c->error };
59 # Execute the auto chain
61 for $auto ( @{ $c->get_action( 'auto', $namespace, 1 ) } ) {
62 $c->execute( @{ $auto->[0] } );
63 return if scalar @{ $c->error };
64 last unless $c->state;
67 # Execute the action or last default
68 my $mkay = defined $auto ? $c->state ? 1 : 0 : 1;
69 if ( ( my $action = $c->req->action ) && $mkay ) {
70 if ( my $result = @{ $c->get_action( $action, $default, 1 ) }[-1] )
72 $c->execute( @{ $result->[0] } );
77 if ( my $end = @{ $c->get_action( 'end', $namespace, 1 ) }[-1] ) {
78 $c->execute( @{ $end->[0] } );
79 return if scalar @{ $c->error };
84 my $path = $c->req->path;
86 ? qq/Unknown resource "$path"/
87 : "No default action defined";
88 $c->log->error($error) if $c->debug;
93 =item $c->forward($command)
95 Forward processing to a private action or a method from a class.
96 If you define a class without method it will default to process().
100 $c->forward(qw/MyApp::Model::CDBI::Foo do_stuff/);
101 $c->forward('MyApp::View::TT');
110 $c->log->debug('Nothing to forward to') if $c->debug;
114 my $caller = caller(0);
117 if ( $command =~ /^\// ) {
118 $command =~ /^\/(.*)\/(\w+)$/;
119 $namespace = $1 || '/';
120 $command = $2 || $command;
124 else { $namespace = Catalyst::Utils::class2prefix($caller) || '/' }
126 my $results = $c->get_action( $command, $namespace );
128 unless ( @{$results} ) {
129 my $class = $command || '';
130 my $path = $class . '.pm';
133 unless ( $INC{$path} ) {
135 qq/Couldn't forward to "$class". Invalid or not loaded./;
137 $c->log->debug($error) if $c->debug;
141 unless ( UNIVERSAL::isa( $class, 'Catalyst::Base' ) ) {
143 qq/Can't forward to "$class". Class is not a Catalyst component./;
145 $c->log->debug($error) if $c->debug;
149 my $method = shift || 'process';
151 if ( my $code = $class->can($method) ) {
152 $c->actions->{reverse}->{"$code"} = "$class->$method";
153 $results = [ [ [ $class, $code ] ] ];
158 qq/Couldn't forward to "$class". Does not implement "$method"/;
160 $c->log->debug($error)
167 for my $result ( @{$results} ) {
168 $c->execute( @{ $result->[0] } );
169 return if scalar @{ $c->error };
170 last unless $c->state;
176 =item $c->get_action( $action, $namespace, $inherit )
178 Get an action in a given namespace.
183 my ( $c, $action, $namespace, $inherit ) = @_;
184 return [] unless $action;
189 $namespace = '' if $namespace eq '/';
190 my $parent = $c->tree;
194 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
195 push @results, [$result] if $result;
196 my $visitor = Tree::Simple::Visitor::FindByPath->new;
198 for my $part ( split '/', $namespace ) {
199 $visitor->setSearchPath($part);
200 $parent->accept($visitor);
201 my $child = $visitor->getResult;
202 my $uid = $child->getUID if $child;
203 my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
204 push @results, [$match] if $match;
205 $parent = $child if $child;
213 my $visitor = Tree::Simple::Visitor::FindByPath->new;
214 $visitor->setSearchPath( split '/', $namespace );
215 $parent->accept($visitor);
216 my $child = $visitor->getResult;
217 my $uid = $child->getUID if $child;
218 my $match = $c->actions->{private}->{$uid}->{$action}
220 push @results, [$match] if $match;
225 $c->actions->{private}->{ $parent->getUID }->{$action};
226 push @results, [$result] if $result;
233 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
234 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
238 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
239 my $name = $c->actions->{compiled}->[$i]->[0];
240 my $regex = $c->actions->{compiled}->[$i]->[1];
242 if ( my @snippets = ( $action =~ $regex ) ) {
243 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
251 =item $c->set_action( $action, $code, $namespace, $attrs )
253 Set an action in a given namespace.
258 my ( $c, $method, $code, $namespace, $attrs ) = @_;
260 my $prefix = Catalyst::Utils::class2prefix($namespace) || '';
263 for my $attr ( @{$attrs} ) {
264 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
265 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
266 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) { $flags{path} = $1 }
267 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
268 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
273 if ( $flags{private} && ( keys %flags > 1 ) ) {
274 $c->log->debug( 'Bad action definition "'
275 . join( ' ', @{$attrs} )
276 . qq/" for "$namespace->$method"/ )
280 return unless keys %flags;
282 my $parent = $c->tree;
283 my $visitor = Tree::Simple::Visitor::FindByPath->new;
285 for my $part ( split '/', $prefix ) {
286 $visitor->setSearchPath($part);
287 $parent->accept($visitor);
288 my $child = $visitor->getResult;
291 $child = $parent->addChild( Tree::Simple->new($part) );
292 $visitor->setSearchPath($part);
293 $parent->accept($visitor);
294 $child = $visitor->getResult;
300 my $uid = $parent->getUID;
301 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
302 my $forward = $prefix ? "$prefix/$method" : $method;
304 if ( $flags{path} ) {
305 $flags{path} =~ s/^\w+//;
306 $flags{path} =~ s/\w+$//;
307 if ( $flags{path} =~ /^'(.*)'$/ ) { $flags{path} = $1 }
308 if ( $flags{path} =~ /^"(.*)"$/ ) { $flags{path} = $1 }
311 if ( $flags{regex} ) {
312 $flags{regex} =~ s/^\w+//;
313 $flags{regex} =~ s/\w+$//;
314 if ( $flags{regex} =~ /^'(.*)'$/ ) { $flags{regex} = $1 }
315 if ( $flags{regex} =~ /^"(.*)"$/ ) { $flags{regex} = $1 }
318 my $reverse = $prefix ? "$prefix/$method" : $method;
320 if ( $flags{local} || $flags{global} || $flags{path} ) {
321 my $path = $flags{path} || $method;
324 if ( $path =~ /^\/(.+)/ ) {
329 $absolute = 1 if $flags{global};
330 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
331 $c->actions->{plain}->{$name} = [ $namespace, $code ];
334 if ( my $regex = $flags{regex} ) {
335 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
336 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
339 $c->actions->{reverse}->{"$code"} = $reverse;
342 =item $class->setup_actions($component)
344 Setup actions for a component.
349 my ( $self, $comps ) = @_;
351 # These are the core structures
363 $self->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
365 for my $comp (@$comps) {
366 $comp = ref $comp || $comp;
368 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
369 my ( $code, $attrs ) = @{$action};
372 my @cache = ( $comp, @{"$comp\::ISA"} );
375 while ( my $namespace = shift @cache ) {
376 $namespaces{$namespace}++;
377 for my $isa ( @{"$comp\::ISA"} ) {
378 next if $namespaces{$isa};
384 for my $namespace ( keys %namespaces ) {
386 for my $sym ( values %{ $namespace . '::' } ) {
388 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
390 $name = *{$sym}{NAME};
391 $self->set_action( $name, $code, $comp, $attrs );
403 my $actions = $self->actions;
404 my $privates = Text::ASCIITable->new;
405 $privates->setCols( 'Private', 'Class' );
406 $privates->setColWidth( 'Private', 36, 1 );
407 $privates->setColWidth( 'Class', 37, 1 );
410 my ( $walker, $parent, $prefix ) = @_;
411 $prefix .= $parent->getNodeValue || '';
412 $prefix .= '/' unless $prefix =~ /\/$/;
413 my $uid = $parent->getUID;
415 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
416 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
417 $privates->addRow( "$prefix$action", $class );
420 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
423 $walker->( $walker, $self->tree, '' );
424 $self->log->debug( 'Loaded private actions', $privates->draw )
425 if ( @{ $privates->{tbl_rows} } && $self->debug );
427 my $publics = Text::ASCIITable->new;
428 $publics->setCols( 'Public', 'Private' );
429 $publics->setColWidth( 'Public', 36, 1 );
430 $publics->setColWidth( 'Private', 37, 1 );
432 for my $plain ( sort keys %{ $actions->{plain} } ) {
433 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
434 my $reverse = $self->actions->{reverse}->{$code};
435 $reverse = $reverse ? "/$reverse" : $code;
436 $publics->addRow( "/$plain", $reverse );
439 $self->log->debug( 'Loaded public actions', $publics->draw )
440 if ( @{ $publics->{tbl_rows} } && $self->debug );
442 my $regexes = Text::ASCIITable->new;
443 $regexes->setCols( 'Regex', 'Private' );
444 $regexes->setColWidth( 'Regex', 36, 1 );
445 $regexes->setColWidth( 'Private', 37, 1 );
447 for my $regex ( sort keys %{ $actions->{regex} } ) {
448 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
449 my $reverse = $self->actions->{reverse}->{$code};
450 $reverse = $reverse ? "/$reverse" : $code;
451 $regexes->addRow( $regex, $reverse );
454 $self->log->debug( 'Loaded regex actions', $regexes->draw )
455 if ( @{ $regexes->{tbl_rows} } && $self->debug );
462 Sebastian Riedel, C<sri@cpan.org>
466 This program is free software, you can redistribute it and/or modify it under
467 the same terms as Perl itself.