1 package Catalyst::Dispatcher;
4 use base 'Class::Data::Inheritable';
8 use Tree::Simple::Visitor::FindByPath;
10 __PACKAGE__->mk_classdata($_) for qw/actions tree/;
12 # These are the core structures
14 { plain => {}, private => {}, regex => {}, compiled => [], reverse => {} }
18 __PACKAGE__->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
22 Catalyst::Dispatcher - The Catalyst Dispatcher
36 Dispatch request to actions.
42 my $action = $c->req->action;
44 $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
45 if $action eq 'default';
48 if ( my $result = $c->get_action($action) ) {
50 Catalyst::Utils::class2prefix( $result->[0]->[0]->[0] );
54 my $default = $action eq 'default' ? $namespace : undef;
55 my $results = $c->get_action( $action, $default );
62 if ( my $begin = @{ $c->get_action( 'begin', $namespace ) }[-1] ) {
63 $c->execute( @{ $begin->[0] } );
64 return if scalar @{ $c->error };
67 # Execute the auto chain
69 for $auto ( @{ $c->get_action( 'auto', $namespace ) } ) {
70 $c->execute( @{ $auto->[0] } );
71 return if scalar @{ $c->error };
72 last unless $c->state;
75 # Execute the action or last default
76 my $mkay = defined $auto ? $c->state ? 1 : 0 : 1;
77 if ( ( my $action = $c->req->action ) && $mkay ) {
78 if ( my $result = @{ $c->get_action( $action, $default ) }[-1] ) {
79 $c->execute( @{ $result->[0] } );
84 if ( my $end = @{ $c->get_action( 'end', $namespace ) }[-1] ) {
85 $c->execute( @{ $end->[0] } );
86 return if scalar @{ $c->error };
91 my $path = $c->req->path;
93 ? qq/Unknown resource "$path"/
94 : "No default action defined";
95 $c->log->error($error) if $c->debug;
100 =item $c->forward($command)
102 Forward processing to a private action or a method from a class.
103 If you define a class without method it will default to process().
106 $c->forward('index');
107 $c->forward(qw/MyApp::Model::CDBI::Foo do_stuff/);
108 $c->forward('MyApp::View::TT');
117 $c->log->debug('Nothing to forward to') if $c->debug;
121 my $caller = caller(0);
124 if ( $command =~ /^\// ) {
125 $command =~ /^\/(.*)\/(\w+)$/;
126 $namespace = $1 || '/';
127 $command = $2 || $command;
131 else { $namespace = Catalyst::Utils::class2prefix($caller) || '/' }
133 my $results = $c->get_action( $command, $namespace );
135 unless ( @{$results} ) {
136 my $class = $command || '';
137 my $path = $class . '.pm';
140 unless ( $INC{$path} ) {
142 qq/Couldn't forward to "$class". Invalid or not loaded./;
144 $c->log->debug($error) if $c->debug;
148 unless ( UNIVERSAL::isa( $class, 'Catalyst::Base' ) ) {
150 qq/Can't forward to "$class". Class is not a Catalyst component./;
152 $c->log->debug($error) if $c->debug;
156 my $method = shift || 'process';
158 if ( my $code = $class->can($method) ) {
159 $c->actions->{reverse}->{"$code"} = "$class->$method";
160 $results = [ [ [ $class, $code ] ] ];
165 qq/Couldn't forward to "$class". Does not implement "$method"/;
167 $c->log->debug($error)
174 for my $result ( @{$results} ) {
175 $c->execute( @{ $result->[0] } );
176 return if scalar @{ $c->error };
177 last unless $c->state;
183 =item $c->get_action( $action, $namespace )
185 Get an action in a given namespace.
190 my ( $c, $action, $namespace ) = @_;
191 return [] unless $action;
195 $namespace = '' if $namespace eq '/';
196 my $parent = $c->tree;
198 my %allowed = ( begin => 1, auto => 1, default => 1, end => 1 );
200 if ( $allowed{$action} ) {
201 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
202 push @results, [$result] if $result;
203 my $visitor = Tree::Simple::Visitor::FindByPath->new;
205 for my $part ( split '/', $namespace ) {
206 $visitor->setSearchPath($part);
207 $parent->accept($visitor);
208 my $child = $visitor->getResult;
209 my $uid = $child->getUID if $child;
210 my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
211 push @results, [$match] if $match;
212 $parent = $child if $child;
220 my $visitor = Tree::Simple::Visitor::FindByPath->new;
221 $visitor->setSearchPath( split '/', $namespace );
222 $parent->accept($visitor);
223 my $child = $visitor->getResult;
224 my $uid = $child->getUID if $child;
225 my $match = $c->actions->{private}->{$uid}->{$action}
227 push @results, [$match] if $match;
232 $c->actions->{private}->{ $parent->getUID }->{$action};
233 push @results, [$result] if $result;
240 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
241 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
245 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
246 my $name = $c->actions->{compiled}->[$i]->[0];
247 my $regex = $c->actions->{compiled}->[$i]->[1];
249 if ( my @snippets = ( $action =~ $regex ) ) {
250 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
258 =item $c->set_action( $action, $code, $namespace, $attrs )
260 Set an action in a given namespace.
265 my ( $c, $method, $code, $namespace, $attrs ) = @_;
267 my $prefix = Catalyst::Utils::class2prefix($namespace) || '';
270 for my $attr ( @{$attrs} ) {
271 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
272 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
273 elsif ( $attr =~ /^Path\((.+)\)$/i ) { $flags{path} = $1 }
274 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
275 elsif ( $attr =~ /^(Regex|Regexp)\((.+)\)$/i ) { $flags{regex} = $2 }
278 if ( $flags{private} && ( keys %flags > 1 ) ) {
279 $c->log->debug( 'Bad action definition "'
280 . join( ' ', @{$attrs} )
281 . qq/" for "$namespace->$method"/ )
285 return unless keys %flags;
287 my $parent = $c->tree;
288 my $visitor = Tree::Simple::Visitor::FindByPath->new;
290 for my $part ( split '/', $prefix ) {
291 $visitor->setSearchPath($part);
292 $parent->accept($visitor);
293 my $child = $visitor->getResult;
296 $child = $parent->addChild( Tree::Simple->new($part) );
297 $visitor->setSearchPath($part);
298 $parent->accept($visitor);
299 $child = $visitor->getResult;
305 my $uid = $parent->getUID;
306 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
307 my $forward = $prefix ? "$prefix/$method" : $method;
309 if ( $flags{path} ) {
310 $flags{path} =~ s/^\w+//;
311 $flags{path} =~ s/\w+$//;
312 if ( $flags{path} =~ /^'(.*)'$/ ) { $flags{path} = $1 }
313 if ( $flags{path} =~ /^"(.*)"$/ ) { $flags{path} = $1 }
316 if ( $flags{regex} ) {
317 $flags{regex} =~ s/^\w+//;
318 $flags{regex} =~ s/\w+$//;
319 if ( $flags{regex} =~ /^'(.*)'$/ ) { $flags{regex} = $1 }
320 if ( $flags{regex} =~ /^"(.*)"$/ ) { $flags{regex} = $1 }
323 my $reverse = $prefix ? "$prefix/$method" : $method;
325 if ( $flags{local} || $flags{global} || $flags{path} ) {
326 my $path = $flags{path} || $method;
329 if ( $path =~ /^\/(.+)/ ) {
334 $absolute = 1 if $flags{global};
335 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
336 $c->actions->{plain}->{$name} = [ $namespace, $code ];
339 if ( my $regex = $flags{regex} ) {
340 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
341 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
344 $c->actions->{reverse}->{"$code"} = $reverse;
347 =item $class->setup_actions($component)
349 Setup actions for a component.
354 my ( $self, $comps ) = @_;
356 for my $comp (@$comps) {
357 $comp = ref $comp || $comp;
359 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
360 my ( $code, $attrs ) = @{$action};
363 my @cache = ( $comp, @{"$comp\::ISA"} );
366 while ( my $namespace = shift @cache ) {
367 $namespaces{$namespace}++;
368 for my $isa ( @{"$comp\::ISA"} ) {
369 next if $namespaces{$isa};
375 for my $namespace ( keys %namespaces ) {
377 for my $sym ( values %{ $namespace . '::' } ) {
379 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
381 $name = *{$sym}{NAME};
382 $self->set_action( $name, $code, $comp, $attrs );
394 my $actions = $self->actions;
395 my $privates = Text::ASCIITable->new;
396 $privates->setCols( 'Private', 'Class' );
397 $privates->setColWidth( 'Private', 36, 1 );
398 $privates->setColWidth( 'Class', 37, 1 );
401 my ( $walker, $parent, $prefix ) = @_;
402 $prefix .= $parent->getNodeValue || '';
403 $prefix .= '/' unless $prefix =~ /\/$/;
404 my $uid = $parent->getUID;
406 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
407 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
408 $privates->addRow( "$prefix$action", $class );
411 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
414 $walker->( $walker, $self->tree, '' );
415 $self->log->debug( 'Loaded private actions', $privates->draw )
416 if ( @{ $privates->{tbl_rows} } && $self->debug );
418 my $publics = Text::ASCIITable->new;
419 $publics->setCols( 'Public', 'Private' );
420 $publics->setColWidth( 'Public', 36, 1 );
421 $publics->setColWidth( 'Private', 37, 1 );
423 for my $plain ( sort keys %{ $actions->{plain} } ) {
424 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
425 my $reverse = $self->actions->{reverse}->{$code};
426 $reverse = $reverse ? "/$reverse" : $code;
427 $publics->addRow( "/$plain", $reverse );
430 $self->log->debug( 'Loaded public actions', $publics->draw )
431 if ( @{ $publics->{tbl_rows} } && $self->debug );
433 my $regexes = Text::ASCIITable->new;
434 $regexes->setCols( 'Regex', 'Private' );
435 $regexes->setColWidth( 'Regex', 36, 1 );
436 $regexes->setColWidth( 'Private', 37, 1 );
438 for my $regex ( sort keys %{ $actions->{regex} } ) {
439 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
440 my $reverse = $self->actions->{reverse}->{$code};
441 $reverse = $reverse ? "/$reverse" : $code;
442 $regexes->addRow( $regex, $reverse );
445 $self->log->debug( 'Loaded regex actions', $regexes->draw )
446 if ( @{ $regexes->{tbl_rows} } && $self->debug );
453 Sebastian Riedel, C<sri@cpan.org>
457 This program is free software, you can redistribute it and/or modify it under
458 the same terms as Perl itself.