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 my $auto ( @{ $c->get_action( 'auto', $namespace ) } ) {
70 $c->execute( @{ $auto->[0] } );
71 return if scalar @{ $c->error };
72 last unless $c->state;
76 # Execute the action or last default
77 my $mkay = $auto ? $c->state ? 1 : 0 : 1;
78 if ( ( my $action = $c->req->action ) && $mkay ) {
79 if ( my $result = @{ $c->get_action( $action, $default ) }[-1] ) {
80 $c->execute( @{ $result->[0] } );
85 if ( my $end = @{ $c->get_action( 'end', $namespace ) }[-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 || '';
139 if ( $class =~ /[^\w\:]/ ) {
140 my $error = qq/Couldn't forward to "$class"/;
142 $c->log->debug($error) if $c->debug;
146 my $method = shift || 'process';
148 if ( my $code = $class->can($method) ) {
149 $c->actions->{reverse}->{"$code"} = "$class->$method";
150 $results = [ [ [ $class, $code ] ] ];
154 my $error = qq/Couldn't forward to "$class"/;
156 $c->log->debug($error)
163 for my $result ( @{$results} ) {
164 $c->execute( @{ $result->[0] } );
165 return if scalar @{ $c->error };
166 last unless $c->state;
172 =item $c->get_action( $action, $namespace )
174 Get an action in a given namespace.
179 my ( $c, $action, $namespace ) = @_;
180 return [] unless $action;
184 $namespace = '' if $namespace eq '/';
185 my $parent = $c->tree;
187 my %allowed = ( begin => 1, auto => 1, default => 1, end => 1 );
189 if ( $allowed{$action} ) {
190 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
191 push @results, [$result] if $result;
192 my $visitor = Tree::Simple::Visitor::FindByPath->new;
194 for my $part ( split '/', $namespace ) {
195 $visitor->setSearchPath($part);
196 $parent->accept($visitor);
197 my $child = $visitor->getResult;
198 my $uid = $child->getUID if $child;
199 my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
200 push @results, [$match] if $match;
201 $parent = $child if $child;
209 my $visitor = Tree::Simple::Visitor::FindByPath->new;
210 $visitor->setSearchPath( split '/', $namespace );
211 $parent->accept($visitor);
212 my $child = $visitor->getResult;
213 my $uid = $child->getUID if $child;
214 my $match = $c->actions->{private}->{$uid}->{$action}
216 push @results, [$match] if $match;
221 $c->actions->{private}->{ $parent->getUID }->{$action};
222 push @results, [$result] if $result;
229 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
230 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
234 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
235 my $name = $c->actions->{compiled}->[$i]->[0];
236 my $regex = $c->actions->{compiled}->[$i]->[1];
238 if ( my @snippets = ( $action =~ $regex ) ) {
239 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
247 =item $c->set_action( $action, $code, $namespace, $attrs )
249 Set an action in a given namespace.
254 my ( $c, $method, $code, $namespace, $attrs ) = @_;
256 my $prefix = Catalyst::Utils::class2prefix($namespace) || '';
259 for my $attr ( @{$attrs} ) {
260 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
261 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
262 elsif ( $attr =~ /^Path\((.+)\)$/i ) { $flags{path} = $1 }
263 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
264 elsif ( $attr =~ /^(Regex|Regexp)\((.+)\)$/i ) { $flags{regex} = $2 }
267 if ( $flags{private} && ( keys %flags > 1 ) ) {
268 $c->log->debug( 'Bad action definition "'
269 . join( ' ', @{$attrs} )
270 . qq/" for "$namespace->$method"/ )
274 return unless keys %flags;
276 my $parent = $c->tree;
277 my $visitor = Tree::Simple::Visitor::FindByPath->new;
279 for my $part ( split '/', $prefix ) {
280 $visitor->setSearchPath($part);
281 $parent->accept($visitor);
282 my $child = $visitor->getResult;
285 $child = $parent->addChild( Tree::Simple->new($part) );
286 $visitor->setSearchPath($part);
287 $parent->accept($visitor);
288 $child = $visitor->getResult;
294 my $uid = $parent->getUID;
295 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
296 my $forward = $prefix ? "$prefix/$method" : $method;
298 if ( $flags{path} ) {
299 $flags{path} =~ s/^\w+//;
300 $flags{path} =~ s/\w+$//;
301 if ( $flags{path} =~ /^'(.*)'$/ ) { $flags{path} = $1 }
302 if ( $flags{path} =~ /^"(.*)"$/ ) { $flags{path} = $1 }
305 if ( $flags{regex} ) {
306 $flags{regex} =~ s/^\w+//;
307 $flags{regex} =~ s/\w+$//;
308 if ( $flags{regex} =~ /^'(.*)'$/ ) { $flags{regex} = $1 }
309 if ( $flags{regex} =~ /^"(.*)"$/ ) { $flags{regex} = $1 }
312 my $reverse = $prefix ? "$prefix/$method" : $method;
314 if ( $flags{local} || $flags{global} || $flags{path} ) {
315 my $path = $flags{path} || $method;
318 if ( $path =~ /^\/(.+)/ ) {
323 $absolute = 1 if $flags{global};
324 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
325 $c->actions->{plain}->{$name} = [ $namespace, $code ];
328 if ( my $regex = $flags{regex} ) {
329 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
330 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
333 $c->actions->{reverse}->{"$code"} = $reverse;
336 =item $class->setup_actions($component)
338 Setup actions for a component.
343 my ( $self, $comps ) = @_;
345 for my $comp (@$comps) {
346 $comp = ref $comp || $comp;
348 for my $action ( @{ $comp->_cache } ) {
349 my ( $code, $attrs ) = @{$action};
352 my @cache = ( $comp, @{"$comp\::ISA"} );
355 while ( my $namespace = shift @cache ) {
356 $namespaces{$namespace}++;
357 for my $isa ( @{"$comp\::ISA"} ) {
358 next if $namespaces{$isa};
364 for my $namespace ( keys %namespaces ) {
366 for my $sym ( values %{ $namespace . '::' } ) {
368 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
370 $name = *{$sym}{NAME};
371 $self->set_action( $name, $code, $comp, $attrs );
383 my $actions = $self->actions;
384 my $privates = Text::ASCIITable->new;
385 $privates->setCols( 'Private', 'Class' );
386 $privates->setColWidth( 'Private', 36, 1 );
387 $privates->setColWidth( 'Class', 37, 1 );
390 my ( $walker, $parent, $prefix ) = @_;
391 $prefix .= $parent->getNodeValue || '';
392 $prefix .= '/' unless $prefix =~ /\/$/;
393 my $uid = $parent->getUID;
395 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
396 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
397 $privates->addRow( "$prefix$action", $class );
400 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
403 $walker->( $walker, $self->tree, '' );
404 $self->log->debug( 'Loaded private actions', $privates->draw )
405 if ( @{ $privates->{tbl_rows} } && $self->debug );
407 my $publics = Text::ASCIITable->new;
408 $publics->setCols( 'Public', 'Private' );
409 $publics->setColWidth( 'Public', 36, 1 );
410 $publics->setColWidth( 'Private', 37, 1 );
412 for my $plain ( sort keys %{ $actions->{plain} } ) {
413 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
414 my $reverse = $self->actions->{reverse}->{$code};
415 $reverse = $reverse ? "/$reverse" : $code;
416 $publics->addRow( "/$plain", $reverse );
419 $self->log->debug( 'Loaded public actions', $publics->draw )
420 if ( @{ $publics->{tbl_rows} } && $self->debug );
422 my $regexes = Text::ASCIITable->new;
423 $regexes->setCols( 'Regex', 'Private' );
424 $regexes->setColWidth( 'Regex', 36, 1 );
425 $regexes->setColWidth( 'Private', 37, 1 );
427 for my $regex ( sort keys %{ $actions->{regex} } ) {
428 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
429 my $reverse = $self->actions->{reverse}->{$code};
430 $reverse = $reverse ? "/$reverse" : $code;
431 $regexes->addRow( $regex, $reverse );
434 $self->log->debug( 'Loaded regex actions', $regexes->draw )
435 if ( @{ $regexes->{tbl_rows} } && $self->debug );
442 Sebastian Riedel, C<sri@cpan.org>
446 This program is free software, you can redistribute it and/or modify it under
447 the same terms as Perl itself.