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;
133 else { $namespace = _class2prefix($caller) || '/' }
135 my $results = $c->get_action( $command, $namespace );
137 unless ( @{$results} ) {
138 my $class = $command || '';
140 if ( $class =~ /[^\w\:]/ ) {
141 my $error = qq/Couldn't forward to "$class"/;
143 $c->log->debug($error) if $c->debug;
147 my $method = shift || 'process';
149 if ( my $code = $class->can($method) ) {
150 $c->actions->{reverse}->{"$code"} = "$class->$method";
151 $results = [ [ [ $class, $code ] ] ];
155 my $error = qq/Couldn't forward to "$class"/;
157 $c->log->debug($error)
164 for my $result ( @{$results} ) {
165 $c->execute( @{ $result->[0] } );
166 return if scalar @{ $c->error };
167 last unless $c->state;
173 =item $c->get_action( $action, $namespace )
175 Get an action in a given namespace.
180 my ( $c, $action, $namespace ) = @_;
181 return [] unless $action;
185 $namespace = '' if $namespace eq '/';
186 my $parent = $c->tree;
188 my %allowed = ( begin => 1, auto => 1, default => 1, end => 1 );
190 if ( $allowed{$action} ) {
191 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
192 push @results, [$result] if $result;
193 my $visitor = Tree::Simple::Visitor::FindByPath->new;
195 for my $part ( split '/', $namespace ) {
196 $visitor->setSearchPath($part);
197 $parent->accept($visitor);
198 my $child = $visitor->getResult;
199 my $uid = $child->getUID if $child;
200 my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
201 push @results, [$match] if $match;
202 $parent = $child if $child;
210 my $visitor = Tree::Simple::Visitor::FindByPath->new;
211 $visitor->setSearchPath( split '/', $namespace );
212 $parent->accept($visitor);
213 my $child = $visitor->getResult;
214 my $uid = $child->getUID if $child;
215 my $match = $c->actions->{private}->{$uid}->{$action}
217 push @results, [$match] if $match;
222 $c->actions->{private}->{ $parent->getUID }->{$action};
223 push @results, [$result] if $result;
230 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
231 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
235 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
236 my $name = $c->actions->{compiled}->[$i]->[0];
237 my $regex = $c->actions->{compiled}->[$i]->[1];
239 if ( $action =~ $regex ) {
241 for my $i ( 1 .. 9 ) {
244 push @snippets, ${$i};
246 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
254 =item $c->set_action( $action, $code, $namespace, $attrs )
256 Set an action in a given namespace.
261 my ( $c, $method, $code, $namespace, $attrs ) = @_;
263 my $prefix = _class2prefix($namespace) || '';
266 for my $attr ( @{$attrs} ) {
267 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
268 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
269 elsif ( $attr =~ /^Path\((.+)\)$/i ) { $flags{path} = $1 }
270 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
271 elsif ( $attr =~ /^(Regex|Regexp)\((.+)\)$/i ) { $flags{regex} = $2 }
274 if ( $flags{private} && ( keys %flags > 1 ) ) {
275 $c->log->debug( 'Bad action definition "'
276 . join( ' ', @{$attrs} )
277 . qq/" for "$namespace->$method"/ )
281 return unless keys %flags;
283 my $parent = $c->tree;
284 my $visitor = Tree::Simple::Visitor::FindByPath->new;
286 for my $part ( split '/', $prefix ) {
287 $visitor->setSearchPath($part);
288 $parent->accept($visitor);
289 my $child = $visitor->getResult;
292 $child = $parent->addChild( Tree::Simple->new($part) );
293 $visitor->setSearchPath($part);
294 $parent->accept($visitor);
295 $child = $visitor->getResult;
301 my $uid = $parent->getUID;
302 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
303 my $forward = $prefix ? "$prefix/$method" : $method;
305 if ( $flags{path} ) {
306 $flags{path} =~ s/^\w+//;
307 $flags{path} =~ s/\w+$//;
308 if ( $flags{path} =~ /^'(.*)'$/ ) { $flags{path} = $1 }
309 if ( $flags{path} =~ /^"(.*)"$/ ) { $flags{path} = $1 }
312 if ( $flags{regex} ) {
313 $flags{regex} =~ s/^\w+//;
314 $flags{regex} =~ s/\w+$//;
315 if ( $flags{regex} =~ /^'(.*)'$/ ) { $flags{regex} = $1 }
316 if ( $flags{regex} =~ /^"(.*)"$/ ) { $flags{regex} = $1 }
319 my $reverse = $prefix ? "$prefix/$method" : $method;
321 if ( $flags{local} || $flags{global} || $flags{path} ) {
322 my $path = $flags{path} || $method;
325 if ( $path =~ /^\/(.+)/ ) {
330 $absolute = 1 if $flags{global};
331 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
332 $c->actions->{plain}->{$name} = [ $namespace, $code ];
335 if ( my $regex = $flags{regex} ) {
336 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
337 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
340 $c->actions->{reverse}->{"$code"} = $reverse;
343 =item $class->setup_actions($component)
345 Setup actions for a component.
350 my ( $self, $comps ) = @_;
352 for my $comp (@$comps) {
353 $comp = ref $comp || $comp;
355 for my $action ( @{ $comp->_cache } ) {
356 my ( $code, $attrs ) = @{$action};
359 my @cache = ( $comp, @{"$comp\::ISA"} );
362 while ( my $namespace = shift @cache ) {
363 $namespaces{$namespace}++;
364 for my $isa ( @{"$comp\::ISA"} ) {
365 next if $namespaces{$isa};
371 for my $namespace ( keys %namespaces ) {
373 for my $sym ( values %{ $namespace . '::' } ) {
375 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
377 $name = *{$sym}{NAME};
378 $self->set_action( $name, $code, $comp, $attrs );
390 my $actions = $self->actions;
391 my $privates = Text::ASCIITable->new;
392 $privates->setCols( 'Private', 'Class' );
393 $privates->setColWidth( 'Private', 36, 1 );
394 $privates->setColWidth( 'Class', 37, 1 );
397 my ( $walker, $parent, $prefix ) = @_;
398 $prefix .= $parent->getNodeValue || '';
399 $prefix .= '/' unless $prefix =~ /\/$/;
400 my $uid = $parent->getUID;
402 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
403 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
404 $privates->addRow( "$prefix$action", $class );
407 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
410 $walker->( $walker, $self->tree, '' );
411 $self->log->debug( 'Loaded private actions', $privates->draw )
412 if ( @{ $privates->{tbl_rows} } && $self->debug );
414 my $publics = Text::ASCIITable->new;
415 $publics->setCols( 'Public', 'Private' );
416 $publics->setColWidth( 'Public', 36, 1 );
417 $publics->setColWidth( 'Private', 37, 1 );
419 for my $plain ( sort keys %{ $actions->{plain} } ) {
420 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
421 my $reverse = $self->actions->{reverse}->{$code};
422 $reverse = $reverse ? "/$reverse" : $code;
423 $publics->addRow( "/$plain", $reverse );
426 $self->log->debug( 'Loaded public actions', $publics->draw )
427 if ( @{ $publics->{tbl_rows} } && $self->debug );
429 my $regexes = Text::ASCIITable->new;
430 $regexes->setCols( 'Regex', 'Private' );
431 $regexes->setColWidth( 'Regex', 36, 1 );
432 $regexes->setColWidth( 'Private', 37, 1 );
434 for my $regex ( sort keys %{ $actions->{regex} } ) {
435 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
436 my $reverse = $self->actions->{reverse}->{$code};
437 $reverse = $reverse ? "/$reverse" : $code;
438 $regexes->addRow( $regex, $reverse );
441 $self->log->debug( 'Loaded regex actions', $regexes->draw )
442 if ( @{ $regexes->{tbl_rows} } && $self->debug );
446 my ( $class, $name ) = @_;
447 my $prefix = _class2prefix($class);
448 $name = "$prefix/$name" if $prefix;
453 my $class = shift || '';
455 if ( $class =~ /^.*::([MVC]|Model|View|Controller)?::(.*)$/ ) {
457 $prefix =~ s/\:\:/\//g;
466 Sebastian Riedel, C<sri@cpan.org>
470 This program is free software, you can redistribute it and/or modify it under
471 the same terms as Perl itself.