1 package Catalyst::Dispatcher;
4 use base 'Class::Accessor::Fast';
5 use Catalyst::Exception;
8 use Catalyst::ActionContainer;
9 use Catalyst::DispatchType::Default;
10 use Catalyst::DispatchType::Index;
11 use Text::SimpleTable;
13 use Tree::Simple::Visitor::FindByPath;
16 use overload '""' => sub { return ref shift }, fallback => 1;
18 __PACKAGE__->mk_accessors(
19 qw/tree dispatch_types registered_dispatch_types
20 method_action_class action_container_class reserved_actions/
23 # Preload these action types
24 our @PRELOAD = qw/Path Regex/;
26 # Postload these action types
27 our @POSTLOAD = qw/Index Default/;
29 # Reserved action names
30 our @RESERVED = qw/begin auto default index end/;
34 Catalyst::Dispatcher - The Catalyst Dispatcher
46 =item $self->detach( $c, $command [, \@arguments ] )
51 my ( $self, $c, $command, @args ) = @_;
52 $c->forward( $command, @args ) if $command;
53 die $Catalyst::DETACH;
56 =item $self->dispatch($c)
61 my ( $self, $c ) = @_;
64 $c->forward( join( '/', '', $c->action->namespace, '_DISPATCH' ) );
68 my $path = $c->req->path;
70 ? qq/Unknown resource "$path"/
71 : "No default action defined";
72 $c->log->error($error) if $c->debug;
77 =item $self->forward( $c, $command [, \@arguments ] )
87 $c->log->debug('Nothing to forward to') if $c->debug;
92 my $arguments = $c->req->args;
93 if ( ref( $_[-1] ) eq 'ARRAY' ) {
100 unless ( ref $command ) {
101 my $command_copy = $command;
103 unless ( $command_copy =~ s/^\/// ) {
104 my $namespace = $c->stack->[-1]->namespace;
105 $command_copy = "${namespace}/${command}";
108 unless ( $command_copy =~ /\// ) {
109 $result = $c->get_action( $command_copy, '/' );
113 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
115 $result = $c->get_action( $tail, $1 );
119 unshift( @{$arguments}, @extra_args );
122 unshift( @extra_args, $tail );
129 my $class = ref($command) || ref( $c->component($command) );
130 my $method = shift || 'process';
134 qq/Couldn't forward to command "$command". Invalid action or component./;
136 $c->log->debug($error) if $c->debug;
140 if ( my $code = $class->can($method) ) {
141 my $action = $self->method_action_class->new(
145 reverse => "$class->$method",
147 namespace => Catalyst::Utils::class2prefix(
148 $class, $c->config->{case_sensitive}
157 qq/Couldn't forward to "$class". Does not implement "$method"/;
159 $c->log->debug($error)
167 local $c->request->{arguments} = [ @{$arguments} ];
168 $result->execute($c);
170 else { $result->execute($c) }
175 =item $self->prepare_action($c)
180 my ( $self, $c ) = @_;
181 my $path = $c->req->path;
182 my @path = split /\//, $c->req->path;
183 $c->req->args( \my @args );
185 push( @path, '/' ) unless @path; # Root action
187 DESCEND: while (@path) {
188 $path = join '/', @path;
190 $path = '' if $path eq '/'; # Root action
192 # Check out dispatch types to see if any will handle the path at
195 foreach my $type ( @{ $self->dispatch_types } ) {
196 last DESCEND if $type->match( $c, $path );
199 # If not, move the last part path to args
200 unshift @args, pop @path;
203 $c->log->debug( 'Path is "' . $c->req->match . '"' )
204 if ( $c->debug && $c->req->match );
206 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
207 if ( $c->debug && @args );
210 =item $self->get_action( $action, $namespace )
215 my ( $self, $name, $namespace ) = @_;
218 $namespace = '' if $namespace eq '/';
220 my @match = $self->get_containers($namespace);
222 return unless @match;
224 if ( my $action = $match[-1]->get_action($name) ) {
225 return $action if $action->namespace eq $namespace;
229 =item $self->get_actions( $c, $action, $namespace )
234 my ( $self, $c, $action, $namespace ) = @_;
235 return [] unless $action;
237 $namespace = '' if $namespace eq '/';
239 my @match = $self->get_containers($namespace);
241 return map { $_->get_action($action) } @match;
244 =item $self->get_containers( $namespace )
249 my ( $self, $namespace ) = @_;
251 # If the namespace is / just return the root ActionContainer
253 return ( $self->tree->getNodeValue )
254 if ( !$namespace || ( $namespace eq '/' ) );
256 # Use a visitor to recurse down the tree finding the ActionContainers
257 # for each namespace in the chain.
259 my $visitor = Tree::Simple::Visitor::FindByPath->new;
260 my @path = split( '/', $namespace );
261 $visitor->setSearchPath(@path);
262 $self->tree->accept($visitor);
264 my @match = $visitor->getResults;
265 @match = ( $self->tree ) unless @match;
267 if ( !defined $visitor->getResult ) {
269 # If we don't manage to match, the visitor doesn't return the last
270 # node is matched, so foo/bar/baz would only find the 'foo' node,
271 # not the foo and foo/bar nodes as it should. This does another
272 # single-level search to see if that's the case, and the 'last unless'
273 # should catch any failures - or short-circuit this if this *is* a
274 # bug in the visitor and gets fixed.
276 if ( my $extra = $path[ ( scalar @match ) - 1 ] ) {
277 $visitor->setSearchPath($extra);
278 $match[-1]->accept($visitor);
279 push( @match, $visitor->getResult ) if defined $visitor->getResult;
283 return map { $_->getNodeValue } @match;
286 =item $self->register( $c, $action )
291 my ( $self, $c, $action ) = @_;
293 my $registered = $self->registered_dispatch_types;
296 foreach my $key ( keys %{ $action->attributes } ) {
297 $priv++ if $key eq 'Private';
298 my $class = "Catalyst::DispatchType::$key";
299 unless ( $registered->{$class} ) {
300 eval "require $class";
301 push( @{ $self->dispatch_types }, $class->new ) unless $@;
302 $registered->{$class} = 1;
306 # Check if action name is reserved
308 for my $name ( @{ $self->reserved_actions } ) {
309 $reserved++ if $action->name eq $name;
312 # Pass the action to our dispatch types so they can register it if reqd.
315 foreach my $type ( @{ $self->dispatch_types } ) {
316 $reg++ if $type->register( $c, $action );
320 return unless $reg + $priv;
322 my $namespace = $action->namespace;
323 my $parent = $self->tree;
324 my $visitor = Tree::Simple::Visitor::FindByPath->new;
327 for my $part ( split '/', $namespace ) {
328 $visitor->setSearchPath($part);
329 $parent->accept($visitor);
330 my $child = $visitor->getResult;
334 # Create a new tree node and an ActionContainer to form
338 Catalyst::ActionContainer->new(
339 { part => $part, actions => {} } );
340 $child = $parent->addChild( Tree::Simple->new($container) );
341 $visitor->setSearchPath($part);
342 $parent->accept($visitor);
343 $child = $visitor->getResult;
350 # Set the method value
351 $parent->getNodeValue->actions->{ $action->name } = $action;
354 =item $self->setup_actions( $class, $component )
359 my ( $self, $c ) = @_;
361 $self->dispatch_types( [] );
362 $self->registered_dispatch_types( {} );
363 $self->reserved_actions( \@RESERVED );
364 $self->method_action_class('Catalyst::Action');
365 $self->action_container_class('Catalyst::ActionContainer');
367 # Preload action types
368 for my $type (@PRELOAD) {
369 my $class = "Catalyst::DispatchType::$type";
370 eval "require $class";
371 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
373 push @{ $self->dispatch_types }, $class->new;
374 $self->registered_dispatch_types->{$class} = 1;
379 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
380 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
382 foreach my $comp ( values %{ $c->components } ) {
383 $comp->register_actions($c) if $comp->can('register_actions');
386 # Postload action types
387 for my $type (@POSTLOAD) {
388 my $class = "Catalyst::DispatchType::$type";
389 eval "require $class";
390 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
392 push @{ $self->dispatch_types }, $class->new;
395 return unless $c->debug;
397 my $privates = Text::SimpleTable->new(
405 my ( $walker, $parent, $prefix ) = @_;
406 $prefix .= $parent->getNodeValue || '';
407 $prefix .= '/' unless $prefix =~ /\/$/;
408 my $node = $parent->getNodeValue->actions;
410 for my $action ( keys %{$node} ) {
411 my $action_obj = $node->{$action};
413 if ( ( $action =~ /^_.*/ )
414 && ( !$c->config->{show_internal_actions} ) );
415 $privates->row( "$prefix$action", $action_obj->class, $action );
419 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
422 $walker->( $walker, $self->tree, '' );
423 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
426 # List all public actions
427 $_->list($c) for @{ $self->dispatch_types };
434 Sebastian Riedel, C<sri@cpan.org>
435 Matt S Trout, C<mst@shadowcatsystems.co.uk>
439 This program is free software, you can redistribute it and/or modify it under
440 the same terms as Perl itself.