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, $default ? 1 : 0 );
62 if ( my $begin = @{ $c->get_action( 'begin', $namespace, 1 ) }[-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, 1 ) } ) {
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 ) }[-1] )
80 $c->execute( @{ $result->[0] } );
85 if ( my $end = @{ $c->get_action( 'end', $namespace, 1 ) }[-1] ) {
86 $c->execute( @{ $end->[0] } );
87 return if scalar @{ $c->error };
92 my $path = $c->req->path;
94 ? qq/Unknown resource "$path"/
95 : "No default action defined";
96 $c->log->error($error) if $c->debug;
101 =item $c->forward($command)
103 Forward processing to a private action or a method from a class.
104 If you define a class without method it will default to process().
107 $c->forward('index');
108 $c->forward(qw/MyApp::Model::CDBI::Foo do_stuff/);
109 $c->forward('MyApp::View::TT');
118 $c->log->debug('Nothing to forward to') if $c->debug;
122 my $caller = caller(0);
125 if ( $command =~ /^\// ) {
126 $command =~ /^\/(.*)\/(\w+)$/;
127 $namespace = $1 || '/';
128 $command = $2 || $command;
132 else { $namespace = Catalyst::Utils::class2prefix($caller) || '/' }
134 my $results = $c->get_action( $command, $namespace );
136 unless ( @{$results} ) {
137 my $class = $command || '';
138 my $path = $class . '.pm';
141 unless ( $INC{$path} ) {
143 qq/Couldn't forward to "$class". Invalid or not loaded./;
145 $c->log->debug($error) if $c->debug;
149 unless ( UNIVERSAL::isa( $class, 'Catalyst::Base' ) ) {
151 qq/Can't forward to "$class". Class is not a Catalyst component./;
153 $c->log->debug($error) if $c->debug;
157 my $method = shift || 'process';
159 if ( my $code = $class->can($method) ) {
160 $c->actions->{reverse}->{"$code"} = "$class->$method";
161 $results = [ [ [ $class, $code ] ] ];
166 qq/Couldn't forward to "$class". Does not implement "$method"/;
168 $c->log->debug($error)
175 for my $result ( @{$results} ) {
176 $c->execute( @{ $result->[0] } );
177 return if scalar @{ $c->error };
178 last unless $c->state;
184 =item $c->get_action( $action, $namespace, $inherit )
186 Get an action in a given namespace.
191 my ( $c, $action, $namespace, $inherit ) = @_;
192 return [] unless $action;
197 $namespace = '' if $namespace eq '/';
198 my $parent = $c->tree;
202 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
203 push @results, [$result] if $result;
204 my $visitor = Tree::Simple::Visitor::FindByPath->new;
206 for my $part ( split '/', $namespace ) {
207 $visitor->setSearchPath($part);
208 $parent->accept($visitor);
209 my $child = $visitor->getResult;
210 my $uid = $child->getUID if $child;
211 my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
212 push @results, [$match] if $match;
213 $parent = $child if $child;
221 my $visitor = Tree::Simple::Visitor::FindByPath->new;
222 $visitor->setSearchPath( split '/', $namespace );
223 $parent->accept($visitor);
224 my $child = $visitor->getResult;
225 my $uid = $child->getUID if $child;
226 my $match = $c->actions->{private}->{$uid}->{$action}
228 push @results, [$match] if $match;
233 $c->actions->{private}->{ $parent->getUID }->{$action};
234 push @results, [$result] if $result;
241 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
242 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
246 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
247 my $name = $c->actions->{compiled}->[$i]->[0];
248 my $regex = $c->actions->{compiled}->[$i]->[1];
250 if ( my @snippets = ( $action =~ $regex ) ) {
251 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
259 =item $c->set_action( $action, $code, $namespace, $attrs )
261 Set an action in a given namespace.
266 my ( $c, $method, $code, $namespace, $attrs ) = @_;
268 my $prefix = Catalyst::Utils::class2prefix($namespace) || '';
271 for my $attr ( @{$attrs} ) {
272 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
273 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
274 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) { $flags{path} = $1 }
275 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
276 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
281 if ( $flags{private} && ( keys %flags > 1 ) ) {
282 $c->log->debug( 'Bad action definition "'
283 . join( ' ', @{$attrs} )
284 . qq/" for "$namespace->$method"/ )
288 return unless keys %flags;
290 my $parent = $c->tree;
291 my $visitor = Tree::Simple::Visitor::FindByPath->new;
293 for my $part ( split '/', $prefix ) {
294 $visitor->setSearchPath($part);
295 $parent->accept($visitor);
296 my $child = $visitor->getResult;
299 $child = $parent->addChild( Tree::Simple->new($part) );
300 $visitor->setSearchPath($part);
301 $parent->accept($visitor);
302 $child = $visitor->getResult;
308 my $uid = $parent->getUID;
309 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
310 my $forward = $prefix ? "$prefix/$method" : $method;
312 if ( $flags{path} ) {
313 $flags{path} =~ s/^\w+//;
314 $flags{path} =~ s/\w+$//;
315 if ( $flags{path} =~ /^'(.*)'$/ ) { $flags{path} = $1 }
316 if ( $flags{path} =~ /^"(.*)"$/ ) { $flags{path} = $1 }
319 if ( $flags{regex} ) {
320 $flags{regex} =~ s/^\w+//;
321 $flags{regex} =~ s/\w+$//;
322 if ( $flags{regex} =~ /^'(.*)'$/ ) { $flags{regex} = $1 }
323 if ( $flags{regex} =~ /^"(.*)"$/ ) { $flags{regex} = $1 }
326 my $reverse = $prefix ? "$prefix/$method" : $method;
328 if ( $flags{local} || $flags{global} || $flags{path} ) {
329 my $path = $flags{path} || $method;
332 if ( $path =~ /^\/(.+)/ ) {
337 $absolute = 1 if $flags{global};
338 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
339 $c->actions->{plain}->{$name} = [ $namespace, $code ];
342 if ( my $regex = $flags{regex} ) {
343 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
344 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
347 $c->actions->{reverse}->{"$code"} = $reverse;
350 =item $class->setup_actions($component)
352 Setup actions for a component.
357 my ( $self, $comps ) = @_;
359 for my $comp (@$comps) {
360 $comp = ref $comp || $comp;
362 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
363 my ( $code, $attrs ) = @{$action};
366 my @cache = ( $comp, @{"$comp\::ISA"} );
369 while ( my $namespace = shift @cache ) {
370 $namespaces{$namespace}++;
371 for my $isa ( @{"$comp\::ISA"} ) {
372 next if $namespaces{$isa};
378 for my $namespace ( keys %namespaces ) {
380 for my $sym ( values %{ $namespace . '::' } ) {
382 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
384 $name = *{$sym}{NAME};
385 $self->set_action( $name, $code, $comp, $attrs );
397 my $actions = $self->actions;
398 my $privates = Text::ASCIITable->new;
399 $privates->setCols( 'Private', 'Class' );
400 $privates->setColWidth( 'Private', 36, 1 );
401 $privates->setColWidth( 'Class', 37, 1 );
404 my ( $walker, $parent, $prefix ) = @_;
405 $prefix .= $parent->getNodeValue || '';
406 $prefix .= '/' unless $prefix =~ /\/$/;
407 my $uid = $parent->getUID;
409 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
410 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
411 $privates->addRow( "$prefix$action", $class );
414 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
417 $walker->( $walker, $self->tree, '' );
418 $self->log->debug( 'Loaded private actions', $privates->draw )
419 if ( @{ $privates->{tbl_rows} } && $self->debug );
421 my $publics = Text::ASCIITable->new;
422 $publics->setCols( 'Public', 'Private' );
423 $publics->setColWidth( 'Public', 36, 1 );
424 $publics->setColWidth( 'Private', 37, 1 );
426 for my $plain ( sort keys %{ $actions->{plain} } ) {
427 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
428 my $reverse = $self->actions->{reverse}->{$code};
429 $reverse = $reverse ? "/$reverse" : $code;
430 $publics->addRow( "/$plain", $reverse );
433 $self->log->debug( 'Loaded public actions', $publics->draw )
434 if ( @{ $publics->{tbl_rows} } && $self->debug );
436 my $regexes = Text::ASCIITable->new;
437 $regexes->setCols( 'Regex', 'Private' );
438 $regexes->setColWidth( 'Regex', 36, 1 );
439 $regexes->setColWidth( 'Private', 37, 1 );
441 for my $regex ( sort keys %{ $actions->{regex} } ) {
442 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
443 my $reverse = $self->actions->{reverse}->{$code};
444 $reverse = $reverse ? "/$reverse" : $code;
445 $regexes->addRow( $regex, $reverse );
448 $self->log->debug( 'Loaded regex actions', $regexes->draw )
449 if ( @{ $regexes->{tbl_rows} } && $self->debug );
456 Sebastian Riedel, C<sri@cpan.org>
460 This program is free software, you can redistribute it and/or modify it under
461 the same terms as Perl itself.