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->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 ] )
83 $command = ref $command if ref $command;
86 $c->log->debug('Nothing to forward to') if $c->debug;
90 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
94 my $command_copy = $command;
96 unless ( $command_copy =~ s/^\/// ) {
97 my $namespace = $c->namespace;
98 $command_copy = "${namespace}/${command}";
101 unless ( $command_copy =~ /\// ) {
102 $result = $c->get_action( $command_copy, '/' );
106 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
108 $result = $c->get_action( $tail, $1 );
111 push( @{$arguments}, @extra_args );
114 unshift( @extra_args, $tail );
122 unless ( $comp = $c->component($command) ) {
124 qq/Couldn't forward to command "$command". Invalid action or component./;
126 $c->log->debug($error) if $c->debug;
130 my $class = ref $comp;
131 my $method = shift || 'process';
133 if ( my $code = $class->can($method) ) {
134 my $action = $self->method_action_class->new(
138 reverse => "$class->$method",
148 qq/Couldn't forward to "$class". Does not implement "$method"/;
150 $c->log->debug($error)
157 local $c->request->{arguments} = [ @{$arguments} ];
159 $result->execute($c);
164 =item $self->prepare_action($c)
169 my ( $self, $c ) = @_;
170 my $path = $c->req->path;
171 my @path = split /\//, $c->req->path;
172 $c->req->args( \my @args );
174 push( @path, '/' ) unless @path; # Root action
176 DESCEND: while (@path) {
177 $path = join '/', @path;
179 $path = '' if $path eq '/'; # Root action
181 # Check out dispatch types to see if any will handle the path at
184 foreach my $type ( @{ $self->dispatch_types } ) {
185 last DESCEND if $type->match( $c, $path );
188 # If not, move the last part path to args
190 unshift @args, pop @path;
193 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
194 if ( $c->debug && @args );
197 =item $self->get_action( $c, $action, $namespace )
202 my ( $self, $c, $name, $namespace ) = @_;
205 $namespace = '' if $namespace eq '/';
207 my @match = $self->get_containers($namespace);
209 return unless @match;
211 if ( my $action = $match[-1]->get_action( $c, $name ) ) {
212 return $action if $action->namespace eq $namespace;
216 =item $self->get_actions( $c, $action, $namespace )
221 my ( $self, $c, $action, $namespace ) = @_;
222 return [] unless $action;
224 $namespace = '' if $namespace eq '/';
226 my @match = $self->get_containers($namespace);
228 return map { $_->get_action( $c, $action ) } @match;
231 =item $self->get_containers( $namespace )
236 my ( $self, $namespace ) = @_;
238 # If the namespace is / just return the root ActionContainer
240 return ( $self->tree->getNodeValue )
241 if ( !$namespace || ( $namespace eq '/' ) );
243 # Use a visitor to recurse down the tree finding the ActionContainers
244 # for each namespace in the chain.
246 my $visitor = Tree::Simple::Visitor::FindByPath->new;
247 my @path = split( '/', $namespace );
248 $visitor->setSearchPath(@path);
249 $self->tree->accept($visitor);
251 my @match = $visitor->getResults;
252 @match = ( $self->tree ) unless @match;
254 if ( !defined $visitor->getResult ) {
256 # If we don't manage to match, the visitor doesn't return the last
257 # node is matched, so foo/bar/baz would only find the 'foo' node,
258 # not the foo and foo/bar nodes as it should. This does another
259 # single-level search to see if that's the case, and the 'last unless'
260 # should catch any failures - or short-circuit this if this *is* a
261 # bug in the visitor and gets fixed.
263 my $extra = $path[ ( scalar @match ) - 1 ];
265 $visitor->setSearchPath($extra);
266 $match[-1]->accept($visitor);
267 push( @match, $visitor->getResult ) if defined $visitor->getResult;
270 return map { $_->getNodeValue } @match;
274 my ( $self, $c, $action ) = @_;
276 my $namespace = $action->namespace;
277 my $parent = $self->tree;
278 my $visitor = Tree::Simple::Visitor::FindByPath->new;
281 for my $part ( split '/', $namespace ) {
282 $visitor->setSearchPath($part);
283 $parent->accept($visitor);
284 my $child = $visitor->getResult;
288 # Create a new tree node and an ActionContainer to form
292 Catalyst::ActionContainer->new(
293 { part => $part, actions => {} } );
294 $child = $parent->addChild( Tree::Simple->new($container) );
295 $visitor->setSearchPath($part);
296 $parent->accept($visitor);
297 $child = $visitor->getResult;
304 # Set the method value
305 $parent->getNodeValue->actions->{ $action->name } = $action;
307 my $registered = $self->registered_dispatch_types;
309 foreach my $key ( keys %{ $action->attributes } ) {
310 my $class = "Catalyst::DispatchType::$key";
311 unless ( $registered->{$class} ) {
312 eval "require $class";
313 push( @{ $self->dispatch_types }, $class->new ) unless $@;
314 $registered->{$class} = 1;
318 # Pass the action to our dispatch types so they can register it if reqd.
319 foreach my $type ( @{ $self->dispatch_types } ) {
320 $type->register( $c, $action );
324 =item $self->setup_actions( $class, $component )
329 my ( $self, $c ) = @_;
331 $self->dispatch_types( [] );
332 $self->registered_dispatch_types( {} );
333 $self->method_action_class('Catalyst::Action');
334 $self->action_container_class('Catalyst::ActionContainer');
336 # Preload action types
337 for my $type (@PRELOAD) {
338 my $class = "Catalyst::DispatchType::$type";
339 eval "require $class";
340 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
342 push @{ $self->dispatch_types }, $class->new;
343 $self->registered_dispatch_types->{$class} = 1;
348 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
349 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
351 $c->register_actions($c);
353 foreach my $comp ( values %{ $c->components } ) {
354 $comp->register_actions($c) if $comp->can('register_actions');
357 # Postload action types
358 for my $type (@POSTLOAD) {
359 my $class = "Catalyst::DispatchType::$type";
360 eval "require $class";
361 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
363 push @{ $self->dispatch_types }, $class->new;
366 return unless $c->debug;
368 my $privates = Text::SimpleTable->new( [ 36, 'Private' ], [ 37, 'Class' ] );
372 my ( $walker, $parent, $prefix ) = @_;
373 $prefix .= $parent->getNodeValue || '';
374 $prefix .= '/' unless $prefix =~ /\/$/;
375 my $node = $parent->getNodeValue->actions;
377 for my $action ( keys %{$node} ) {
378 my $action_obj = $node->{$action};
380 if ( ( $action =~ /^_.*/ )
381 && ( !$c->config->{show_internal_actions} ) );
382 $privates->row( "$prefix$action", $action_obj->class );
386 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
389 $walker->( $walker, $self->tree, '' );
390 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
393 # List all public actions
394 $_->list($c) for @{ $self->dispatch_types };
401 Sebastian Riedel, C<sri@cpan.org>
402 Matt S Trout, C<mst@shadowcatsystems.co.uk>
406 This program is free software, you can redistribute it and/or modify it under
407 the same terms as Perl itself.