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(qw/tree dispatch_types registered_dispatch_types/);
20 # Preload these action types
21 our @PRELOAD = qw/Path Regex/;
23 # Postload these action types
24 our @POSTLOAD = qw/Index Default/;
28 Catalyst::Dispatcher - The Catalyst Dispatcher
40 =item $self->detach( $c, $command [, \@arguments ] )
45 my ( $self, $c, $command, @args ) = @_;
46 $c->forward( $command, @args ) if $command;
47 die $Catalyst::DETACH;
50 =item $self->dispatch($c)
55 my ( $self, $c ) = @_;
58 $c->forward( join( '/', '', $c->namespace, '_DISPATCH' ) );
62 my $path = $c->req->path;
64 ? qq/Unknown resource "$path"/
65 : "No default action defined";
66 $c->log->error($error) if $c->debug;
71 =item $self->forward( $c, $command [, \@arguments ] )
81 $c->log->debug('Nothing to forward to') if $c->debug;
85 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
89 my $command_copy = $command;
91 unless ( $command_copy =~ s/^\/// ) {
92 my $namespace = $c->namespace;
93 $command_copy = "${namespace}/${command}";
96 unless ( $command_copy =~ /\// ) {
97 $result = $c->get_action( $command_copy, '/' );
101 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
103 $result = $c->get_action( $tail, $1 );
106 push( @{$arguments}, @extra_args );
109 unshift( @extra_args, $tail );
117 unless ( $comp = $c->component($command) ) {
119 qq/Couldn't forward to command "$command". Invalid action or component./;
121 $c->log->debug($error) if $c->debug;
125 my $class = ref $comp;
126 my $method = shift || 'process';
128 if ( my $code = $class->can($method) ) {
129 my $action = Catalyst::Action->new(
133 reverse => "$class->$method",
143 qq/Couldn't forward to "$class". Does not implement "$method"/;
145 $c->log->debug($error)
152 local $c->request->{arguments} = [ @{$arguments} ];
154 $result->execute($c);
159 =item $self->prepare_action($c)
164 my ( $self, $c ) = @_;
165 my $path = $c->req->path;
166 my @path = split /\//, $c->req->path;
167 $c->req->args( \my @args );
169 push( @path, '/' ) unless @path; # Root action
171 DESCEND: while (@path) {
172 $path = join '/', @path;
174 $path = '' if $path eq '/'; # Root action
176 # Check out dispatch types to see if any will handle the path at
179 foreach my $type ( @{ $self->dispatch_types } ) {
180 last DESCEND if $type->match( $c, $path );
183 # If not, move the last part path to args
185 unshift @args, pop @path;
188 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
189 if ( $c->debug && @args );
192 =item $self->get_action( $c, $action, $namespace )
197 my ( $self, $c, $name, $namespace ) = @_;
200 $namespace = '' if $namespace eq '/';
202 my @match = $self->get_containers($namespace);
204 return unless @match;
206 if ( my $action = $match[-1]->get_action( $c, $name ) )
208 return $action if $action->namespace eq $namespace;
212 =item $self->get_actions( $c, $action, $namespace )
217 my ( $self, $c, $action, $namespace ) = @_;
218 return [] unless $action;
220 $namespace = '' if $namespace eq '/';
222 my @match = $self->get_containers($namespace);
224 return map { $_->get_action($c, $action) } @match;
227 =item $self->get_containers( $namespace )
232 my ( $self, $namespace ) = @_;
234 # If the namespace is / just return the root ActionContainer
236 return ( $self->tree->getNodeValue )
237 if ( !$namespace || ( $namespace eq '/' ) );
239 # Use a visitor to recurse down the tree finding the ActionContainers
240 # for each namespace in the chain.
242 my $visitor = Tree::Simple::Visitor::FindByPath->new;
243 my @path = split( '/', $namespace );
244 $visitor->setSearchPath(@path);
245 $self->tree->accept($visitor);
247 my @match = $visitor->getResults;
248 @match = ( $self->tree ) unless @match;
250 if ( !defined $visitor->getResult ) {
252 # If we don't manage to match, the visitor doesn't return the last
253 # node is matched, so foo/bar/baz would only find the 'foo' node,
254 # not the foo and foo/bar nodes as it should. This does another
255 # single-level search to see if that's the case, and the 'last unless'
256 # should catch any failures - or short-circuit this if this *is* a
257 # bug in the visitor and gets fixed.
259 my $extra = $path[ ( scalar @match ) - 1 ];
261 $visitor->setSearchPath($extra);
262 $match[-1]->accept($visitor);
263 push( @match, $visitor->getResult ) if defined $visitor->getResult;
266 return map { $_->getNodeValue } @match;
270 my ( $self, $c, $action ) = @_;
272 my $namespace = $action->namespace;
273 my $parent = $self->tree;
274 my $visitor = Tree::Simple::Visitor::FindByPath->new;
277 for my $part ( split '/', $namespace ) {
278 $visitor->setSearchPath($part);
279 $parent->accept($visitor);
280 my $child = $visitor->getResult;
284 # Create a new tree node and an ActionContainer to form
288 Catalyst::ActionContainer->new(
289 { part => $part, actions => {} } );
290 $child = $parent->addChild( Tree::Simple->new($container) );
291 $visitor->setSearchPath($part);
292 $parent->accept($visitor);
293 $child = $visitor->getResult;
300 # Set the method value
301 $parent->getNodeValue->actions->{$action->name} = $action;
303 my $registered = $self->registered_dispatch_types;
305 foreach my $key (keys %{$action->attributes}) {
306 my $class = "Catalyst::DispatchType::$key";
307 unless ( $registered->{$class} ) {
308 eval "require $class";
309 push( @{ $self->dispatch_types }, $class->new ) unless $@;
310 $registered->{$class} = 1;
314 # Pass the action to our dispatch types so they can register it if reqd.
315 foreach my $type ( @{ $self->dispatch_types } ) {
316 $type->register( $c, $action );
320 =item $self->setup_actions( $class, $component )
325 my ( $self, $c ) = @_;
327 $self->dispatch_types( [] );
328 $self->registered_dispatch_types( {} );
330 # Preload action types
331 for my $type (@PRELOAD) {
332 my $class = "Catalyst::DispatchType::$type";
333 eval "require $class";
334 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
336 push @{ $self->dispatch_types }, $class->new;
337 $self->registered_dispatch_types->{$class} = 1;
342 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
343 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
345 $c->register_actions( $c );
347 foreach my $comp ( values %{$c->components} ) {
348 $comp->register_actions( $c ) if $comp->can('register_actions');
351 # Postload action types
352 for my $type (@POSTLOAD) {
353 my $class = "Catalyst::DispatchType::$type";
354 eval "require $class";
355 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
357 push @{ $self->dispatch_types }, $class->new;
360 return unless $c->debug;
363 = Text::SimpleTable->new( [ 36, 'Private' ], [ 37, 'Class' ] );
367 my ( $walker, $parent, $prefix ) = @_;
368 $prefix .= $parent->getNodeValue || '';
369 $prefix .= '/' unless $prefix =~ /\/$/;
370 my $node = $parent->getNodeValue->actions;
372 for my $action ( keys %{$node} ) {
373 my $action_obj = $node->{$action};
375 if ( ( $action =~ /^_.*/ )
376 && ( !$c->config->{show_internal_actions} ) );
377 $privates->row( "$prefix$action", $action_obj->class );
381 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
384 $walker->( $walker, $self->tree, '' );
385 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
388 # List all public actions
389 $_->list($c) for @{ $self->dispatch_types };
396 Sebastian Riedel, C<sri@cpan.org>
397 Matt S Trout, C<mst@shadowcatsystems.co.uk>
401 This program is free software, you can redistribute it and/or modify it under
402 the same terms as Perl itself.