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 ) );
20 memoize('_class2prefix');
24 Catalyst::Dispatch - The Catalyst Dispatcher
38 Dispatch request to actions.
44 my $action = $c->req->action;
46 $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
47 if $action eq 'default';
50 if ( my $result = $c->get_action($action) ) {
51 $namespace = _class2prefix( $result->[0]->[0]->[0] );
55 my $default = $action eq 'default' ? $namespace : undef;
56 my $results = $c->get_action( $action, $default );
63 if ( my $begin = @{ $c->get_action( 'begin', $namespace ) }[-1] ) {
64 $c->execute( @{ $begin->[0] } );
65 return if scalar @{ $c->error };
68 # Execute the auto chain
70 for my $auto ( @{ $c->get_action( 'auto', $namespace ) } ) {
71 $c->execute( @{ $auto->[0] } );
72 return if scalar @{ $c->error };
73 last unless $c->state;
77 # Execute the action or last default
78 my $mkay = $auto ? $c->state ? 1 : 0 : 1;
79 if ( ( my $action = $c->req->action ) && $mkay ) {
80 if ( my $result = @{ $c->get_action( $action, $default ) }[-1] ) {
81 $c->execute( @{ $result->[0] } );
86 if ( my $end = @{ $c->get_action( 'end', $namespace ) }[-1] ) {
87 $c->execute( @{ $end->[0] } );
88 return if scalar @{ $c->error };
93 my $path = $c->req->path;
95 ? qq/Unknown resource "$path"/
96 : "No default action defined";
97 $c->log->error($error) if $c->debug;
102 =item $c->forward($command)
104 Forward processing to a private action or a method from a class.
105 If you define a class without method it will default to process().
108 $c->forward('index');
109 $c->forward(qw/MyApp::Model::CDBI::Foo do_stuff/);
110 $c->forward('MyApp::View::TT');
119 $c->log->debug('Nothing to forward to') if $c->debug;
123 my $caller = caller(0);
126 if ( $command =~ /^\// ) {
127 $command =~ /^\/(.*)\/(\w+)$/;
128 $namespace = $1 || '/';
129 $command = $2 || $command;
131 warn "NAMESPACE: $namespace COMMAND: $command";
134 else { $namespace = _class2prefix($caller) || '/' }
136 my $results = $c->get_action( $command, $namespace );
138 unless ( @{$results} ) {
139 my $class = $command || '';
141 if ( $class =~ /[^\w\:]/ ) {
142 my $error = qq/Couldn't forward to "$class"/;
144 $c->log->debug($error) if $c->debug;
148 my $method = shift || 'process';
150 if ( my $code = $class->can($method) ) {
151 $c->actions->{reverse}->{"$code"} = "$class->$method";
152 $results = [ [ [ $class, $code ] ] ];
156 my $error = qq/Couldn't forward to "$class"/;
158 $c->log->debug($error)
165 for my $result ( @{$results} ) {
166 $c->execute( @{ $result->[0] } );
167 return if scalar @{ $c->error };
168 last unless $c->state;
174 =item $c->get_action( $action, $namespace )
176 Get an action in a given namespace.
181 my ( $c, $action, $namespace ) = @_;
182 return [] unless $action;
186 $namespace = '' if $namespace eq '/';
187 my $parent = $c->tree;
189 my %allowed = ( begin => 1, auto => 1, default => 1, end => 1 );
191 if ( $allowed{$action} ) {
192 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
193 push @results, [$result] if $result;
194 my $visitor = Tree::Simple::Visitor::FindByPath->new;
196 for my $part ( split '/', $namespace ) {
197 $visitor->setSearchPath($part);
198 $parent->accept($visitor);
199 my $child = $visitor->getResult;
200 my $uid = $child->getUID if $child;
201 my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
202 push @results, [$match] if $match;
203 $parent = $child if $child;
211 my $visitor = Tree::Simple::Visitor::FindByPath->new;
212 $visitor->setSearchPath( split '/', $namespace );
213 $parent->accept($visitor);
214 my $child = $visitor->getResult;
215 my $uid = $child->getUID if $child;
216 my $match = $c->actions->{private}->{$uid}->{$action}
218 push @results, [$match] if $match;
223 $c->actions->{private}->{ $parent->getUID }->{$action};
224 push @results, [$result] if $result;
231 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
232 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
236 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
237 my $name = $c->actions->{compiled}->[$i]->[0];
238 my $regex = $c->actions->{compiled}->[$i]->[1];
240 if ( $action =~ $regex ) {
242 for my $i ( 1 .. 9 ) {
245 push @snippets, ${$i};
247 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
255 =item $c->set_action( $action, $code, $namespace, $attrs )
257 Set an action in a given namespace.
262 my ( $c, $method, $code, $namespace, $attrs ) = @_;
264 my $prefix = _class2prefix($namespace) || '';
267 for my $attr ( @{$attrs} ) {
268 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
269 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
270 elsif ( $attr =~ /^Path\((.+)\)$/i ) { $flags{path} = $1 }
271 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
272 elsif ( $attr =~ /^(Regex|Regexp)\((.+)\)$/i ) { $flags{regex} = $2 }
275 if ( $flags{private} && ( keys %flags > 1 ) ) {
276 $c->log->debug( 'Bad action definition "'
277 . join( ' ', @{$attrs} )
278 . qq/" for "$namespace->$method"/ )
282 return unless keys %flags;
284 my $parent = $c->tree;
285 my $visitor = Tree::Simple::Visitor::FindByPath->new;
287 for my $part ( split '/', $prefix ) {
288 $visitor->setSearchPath($part);
289 $parent->accept($visitor);
290 my $child = $visitor->getResult;
293 $child = $parent->addChild( Tree::Simple->new($part) );
294 $visitor->setSearchPath($part);
295 $parent->accept($visitor);
296 $child = $visitor->getResult;
302 my $uid = $parent->getUID;
303 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
304 my $forward = $prefix ? "$prefix/$method" : $method;
306 if ( $flags{path} ) {
307 $flags{path} =~ s/^\w+//;
308 $flags{path} =~ s/\w+$//;
309 if ( $flags{path} =~ /^'(.*)'$/ ) { $flags{path} = $1 }
310 if ( $flags{path} =~ /^"(.*)"$/ ) { $flags{path} = $1 }
313 if ( $flags{regex} ) {
314 $flags{regex} =~ s/^\w+//;
315 $flags{regex} =~ s/\w+$//;
316 if ( $flags{regex} =~ /^'(.*)'$/ ) { $flags{regex} = $1 }
317 if ( $flags{regex} =~ /^"(.*)"$/ ) { $flags{regex} = $1 }
320 my $reverse = $prefix ? "$prefix/$method" : $method;
322 if ( $flags{local} || $flags{global} || $flags{path} ) {
323 my $path = $flags{path} || $method;
326 if ( $path =~ /^\/(.+)/ ) {
331 $absolute = 1 if $flags{global};
332 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
333 $c->actions->{plain}->{$name} = [ $namespace, $code ];
336 if ( my $regex = $flags{regex} ) {
337 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
338 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
341 $c->actions->{reverse}->{"$code"} = $reverse;
344 =item $class->setup_actions($component)
346 Setup actions for a component.
351 my ( $self, $comps ) = @_;
353 for my $comp (@$comps) {
354 $comp = ref $comp || $comp;
356 for my $action ( @{ $comp->_cache } ) {
357 my ( $code, $attrs ) = @{$action};
360 my @cache = ( $comp, @{"$comp\::ISA"} );
363 while ( my $namespace = shift @cache ) {
364 $namespaces{$namespace}++;
365 for my $isa ( @{"$comp\::ISA"} ) {
366 next if $namespaces{$isa};
372 for my $namespace ( keys %namespaces ) {
374 for my $sym ( values %{ $namespace . '::' } ) {
376 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
378 $name = *{$sym}{NAME};
379 $self->set_action( $name, $code, $comp, $attrs );
391 my $actions = $self->actions;
392 my $privates = Text::ASCIITable->new;
393 $privates->setCols( 'Private', 'Class' );
394 $privates->setColWidth( 'Private', 36, 1 );
395 $privates->setColWidth( 'Class', 37, 1 );
398 my ( $walker, $parent, $prefix ) = @_;
399 $prefix .= $parent->getNodeValue || '';
400 $prefix .= '/' unless $prefix =~ /\/$/;
401 my $uid = $parent->getUID;
403 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
404 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
405 $privates->addRow( "$prefix$action", $class );
408 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
411 $walker->( $walker, $self->tree, '' );
412 $self->log->debug( 'Loaded private actions', $privates->draw )
413 if ( @{ $privates->{tbl_rows} } && $self->debug );
415 my $publics = Text::ASCIITable->new;
416 $publics->setCols( 'Public', 'Private' );
417 $publics->setColWidth( 'Public', 36, 1 );
418 $publics->setColWidth( 'Private', 37, 1 );
420 for my $plain ( sort keys %{ $actions->{plain} } ) {
421 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
422 my $reverse = $self->actions->{reverse}->{$code};
423 $reverse = $reverse ? "/$reverse" : $code;
424 $publics->addRow( "/$plain", $reverse );
427 $self->log->debug( 'Loaded public actions', $publics->draw )
428 if ( @{ $publics->{tbl_rows} } && $self->debug );
430 my $regexes = Text::ASCIITable->new;
431 $regexes->setCols( 'Regex', 'Private' );
432 $regexes->setColWidth( 'Regex', 36, 1 );
433 $regexes->setColWidth( 'Private', 37, 1 );
435 for my $regex ( sort keys %{ $actions->{regex} } ) {
436 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
437 my $reverse = $self->actions->{reverse}->{$code};
438 $reverse = $reverse ? "/$reverse" : $code;
439 $regexes->addRow( $regex, $reverse );
442 $self->log->debug( 'Loaded regex actions', $regexes->draw )
443 if ( @{ $regexes->{tbl_rows} } && $self->debug );
447 my ( $class, $name ) = @_;
448 my $prefix = _class2prefix($class);
449 $name = "$prefix/$name" if $prefix;
454 my $class = shift || '';
456 if ( $class =~ /^.*::([MVC]|Model|View|Controller)?::(.*)$/ ) {
458 $prefix =~ s/\:\:/\//g;
467 Sebastian Riedel, C<sri@cpan.org>
471 This program is free software, you can redistribute it and/or modify it under
472 the same terms as Perl itself.