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/
23 # Preload these action types
24 our @PRELOAD = qw/Path Regex/;
26 # Postload these action types
27 our @POSTLOAD = qw/Index Default/;
31 Catalyst::Dispatcher - The Catalyst Dispatcher
43 =item $self->detach( $c, $command [, \@arguments ] )
48 my ( $self, $c, $command, @args ) = @_;
49 $c->forward( $command, @args ) if $command;
50 die $Catalyst::DETACH;
53 =item $self->dispatch($c)
58 my ( $self, $c ) = @_;
61 $c->forward( join( '/', '', $c->action->namespace, '_DISPATCH' ) );
65 my $path = $c->req->path;
67 ? qq/Unknown resource "$path"/
68 : "No default action defined";
69 $c->log->error($error) if $c->debug;
74 =item $self->forward( $c, $command [, \@arguments ] )
84 $c->log->debug('Nothing to forward to') if $c->debug;
89 my $arguments = $c->req->args;
90 if ( ref( $_[-1] ) eq 'ARRAY' ) {
97 unless ( ref $command ) {
98 my $command_copy = $command;
100 unless ( $command_copy =~ s/^\/// ) {
101 my $namespace = $c->stack->[-1]->namespace;
102 $command_copy = "${namespace}/${command}";
105 unless ( $command_copy =~ /\// ) {
106 $result = $c->get_action( $command_copy, '/' );
110 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
112 $result = $c->get_action( $tail, $1 );
116 unshift( @{$arguments}, @extra_args );
119 unshift( @extra_args, $tail );
126 my $class = ref($command) || ref( $c->component($command) );
127 my $method = shift || 'process';
131 qq/Couldn't forward to command "$command". Invalid action or component./;
133 $c->log->debug($error) if $c->debug;
137 if ( my $code = $class->can($method) ) {
138 my $action = $self->method_action_class->new(
142 reverse => "$class->$method",
144 namespace => Catalyst::Utils::class2prefix(
145 $class, $c->config->{case_sensitive}
154 qq/Couldn't forward to "$class". Does not implement "$method"/;
156 $c->log->debug($error)
164 local $c->request->{arguments} = [ @{$arguments} ];
165 $result->execute($c);
167 else { $result->execute($c) }
172 =item $self->prepare_action($c)
177 my ( $self, $c ) = @_;
178 my $path = $c->req->path;
179 my @path = split /\//, $c->req->path;
180 $c->req->args( \my @args );
182 push( @path, '/' ) unless @path; # Root action
184 DESCEND: while (@path) {
185 $path = join '/', @path;
187 $path = '' if $path eq '/'; # Root action
189 # Check out dispatch types to see if any will handle the path at
192 foreach my $type ( @{ $self->dispatch_types } ) {
193 last DESCEND if $type->match( $c, $path );
196 # If not, move the last part path to args
198 unshift @args, pop @path;
201 $c->log->debug( 'Path is "' . $c->req->match . '"' )
202 if ( $c->debug && $c->req->match );
204 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
205 if ( $c->debug && @args );
208 =item $self->get_action( $action, $namespace )
213 my ( $self, $name, $namespace ) = @_;
216 $namespace = '' if $namespace eq '/';
218 my @match = $self->get_containers($namespace);
220 return unless @match;
222 if ( my $action = $match[-1]->get_action($name) ) {
223 return $action if $action->namespace eq $namespace;
227 =item $self->get_actions( $c, $action, $namespace )
232 my ( $self, $c, $action, $namespace ) = @_;
233 return [] unless $action;
235 $namespace = '' if $namespace eq '/';
237 my @match = $self->get_containers($namespace);
239 return map { $_->get_action($action) } @match;
242 =item $self->get_containers( $namespace )
247 my ( $self, $namespace ) = @_;
249 # If the namespace is / just return the root ActionContainer
251 return ( $self->tree->getNodeValue )
252 if ( !$namespace || ( $namespace eq '/' ) );
254 # Use a visitor to recurse down the tree finding the ActionContainers
255 # for each namespace in the chain.
257 my $visitor = Tree::Simple::Visitor::FindByPath->new;
258 my @path = split( '/', $namespace );
259 $visitor->setSearchPath(@path);
260 $self->tree->accept($visitor);
262 my @match = $visitor->getResults;
263 @match = ( $self->tree ) unless @match;
265 if ( !defined $visitor->getResult ) {
267 # If we don't manage to match, the visitor doesn't return the last
268 # node is matched, so foo/bar/baz would only find the 'foo' node,
269 # not the foo and foo/bar nodes as it should. This does another
270 # single-level search to see if that's the case, and the 'last unless'
271 # should catch any failures - or short-circuit this if this *is* a
272 # bug in the visitor and gets fixed.
274 if ( my $extra = $path[ ( scalar @match ) - 1 ] ) {
275 $visitor->setSearchPath($extra);
276 $match[-1]->accept($visitor);
277 push( @match, $visitor->getResult ) if defined $visitor->getResult;
281 return map { $_->getNodeValue } @match;
284 =item $self->register( $c, $action )
289 my ( $self, $c, $action ) = @_;
291 my $registered = $self->registered_dispatch_types;
294 foreach my $key ( keys %{ $action->attributes } ) {
295 $priv++ if $key eq 'Private';
296 my $class = "Catalyst::DispatchType::$key";
297 unless ( $registered->{$class} ) {
298 eval "require $class";
299 push( @{ $self->dispatch_types }, $class->new ) unless $@;
300 $registered->{$class} = 1;
304 # Pass the action to our dispatch types so they can register it if reqd.
306 foreach my $type ( @{ $self->dispatch_types } ) {
307 $reg++ if $type->register( $c, $action );
310 return unless $reg + $priv;
312 my $namespace = $action->namespace;
313 my $parent = $self->tree;
314 my $visitor = Tree::Simple::Visitor::FindByPath->new;
317 for my $part ( split '/', $namespace ) {
318 $visitor->setSearchPath($part);
319 $parent->accept($visitor);
320 my $child = $visitor->getResult;
324 # Create a new tree node and an ActionContainer to form
328 Catalyst::ActionContainer->new(
329 { part => $part, actions => {} } );
330 $child = $parent->addChild( Tree::Simple->new($container) );
331 $visitor->setSearchPath($part);
332 $parent->accept($visitor);
333 $child = $visitor->getResult;
340 # Set the method value
341 $parent->getNodeValue->actions->{ $action->name } = $action;
344 =item $self->setup_actions( $class, $component )
349 my ( $self, $c ) = @_;
351 $self->dispatch_types( [] );
352 $self->registered_dispatch_types( {} );
353 $self->method_action_class('Catalyst::Action');
354 $self->action_container_class('Catalyst::ActionContainer');
356 # Preload action types
357 for my $type (@PRELOAD) {
358 my $class = "Catalyst::DispatchType::$type";
359 eval "require $class";
360 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
362 push @{ $self->dispatch_types }, $class->new;
363 $self->registered_dispatch_types->{$class} = 1;
368 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
369 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
371 foreach my $comp ( values %{ $c->components } ) {
372 $comp->register_actions($c) if $comp->can('register_actions');
375 # Postload action types
376 for my $type (@POSTLOAD) {
377 my $class = "Catalyst::DispatchType::$type";
378 eval "require $class";
379 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
381 push @{ $self->dispatch_types }, $class->new;
384 return unless $c->debug;
386 my $privates = Text::SimpleTable->new(
394 my ( $walker, $parent, $prefix ) = @_;
395 $prefix .= $parent->getNodeValue || '';
396 $prefix .= '/' unless $prefix =~ /\/$/;
397 my $node = $parent->getNodeValue->actions;
399 for my $action ( keys %{$node} ) {
400 my $action_obj = $node->{$action};
402 if ( ( $action =~ /^_.*/ )
403 && ( !$c->config->{show_internal_actions} ) );
404 $privates->row( "$prefix$action", $action_obj->class, $action );
408 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
411 $walker->( $walker, $self->tree, '' );
412 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
415 # List all public actions
416 $_->list($c) for @{ $self->dispatch_types };
423 Sebastian Riedel, C<sri@cpan.org>
424 Matt S Trout, C<mst@shadowcatsystems.co.uk>
428 This program is free software, you can redistribute it and/or modify it under
429 the same terms as Perl itself.