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 ] )
84 $c->log->debug('Nothing to forward to') if $c->debug;
88 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
92 unless ( ref $command ) {
93 my $command_copy = $command;
95 unless ( $command_copy =~ s/^\/// ) {
96 my $namespace = $c->namespace;
97 $command_copy = "${namespace}/${command}";
100 unless ( $command_copy =~ /\// ) {
101 $result = $c->get_action( $command_copy, '/' );
105 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
107 $result = $c->get_action( $tail, $1 );
110 push( @{$arguments}, @extra_args );
113 unshift( @extra_args, $tail );
125 unless ( $comp = $c->component($command) ) {
127 qq/Couldn't forward to command "$command". Invalid action or component./;
129 $c->log->debug($error) if $c->debug;
133 my $class = ref $comp;
134 my $method = shift || 'process';
136 if ( my $code = $class->can($method) ) {
137 my $action = $self->method_action_class->new(
141 reverse => "$class->$method",
151 qq/Couldn't forward to "$class". Does not implement "$method"/;
153 $c->log->debug($error)
160 local $c->request->{arguments} = [ @{$arguments} ];
162 $result->execute($c);
167 =item $self->prepare_action($c)
172 my ( $self, $c ) = @_;
173 my $path = $c->req->path;
174 my @path = split /\//, $c->req->path;
175 $c->req->args( \my @args );
177 push( @path, '/' ) unless @path; # Root action
179 DESCEND: while (@path) {
180 $path = join '/', @path;
182 $path = '' if $path eq '/'; # Root action
184 # Check out dispatch types to see if any will handle the path at
187 foreach my $type ( @{ $self->dispatch_types } ) {
188 last DESCEND if $type->match( $c, $path );
191 # If not, move the last part path to args
193 unshift @args, pop @path;
196 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
197 if ( $c->debug && @args );
200 =item $self->get_action( $c, $action, $namespace )
205 my ( $self, $c, $name, $namespace ) = @_;
208 $namespace = '' if $namespace eq '/';
210 my @match = $self->get_containers($namespace);
212 return unless @match;
214 if ( my $action = $match[-1]->get_action( $c, $name ) ) {
215 return $action if $action->namespace eq $namespace;
219 =item $self->get_actions( $c, $action, $namespace )
224 my ( $self, $c, $action, $namespace ) = @_;
225 return [] unless $action;
227 $namespace = '' if $namespace eq '/';
229 my @match = $self->get_containers($namespace);
231 return map { $_->get_action( $c, $action ) } @match;
234 =item $self->get_containers( $namespace )
239 my ( $self, $namespace ) = @_;
241 # If the namespace is / just return the root ActionContainer
243 return ( $self->tree->getNodeValue )
244 if ( !$namespace || ( $namespace eq '/' ) );
246 # Use a visitor to recurse down the tree finding the ActionContainers
247 # for each namespace in the chain.
249 my $visitor = Tree::Simple::Visitor::FindByPath->new;
250 my @path = split( '/', $namespace );
251 $visitor->setSearchPath(@path);
252 $self->tree->accept($visitor);
254 my @match = $visitor->getResults;
255 @match = ( $self->tree ) unless @match;
257 if ( !defined $visitor->getResult ) {
259 # If we don't manage to match, the visitor doesn't return the last
260 # node is matched, so foo/bar/baz would only find the 'foo' node,
261 # not the foo and foo/bar nodes as it should. This does another
262 # single-level search to see if that's the case, and the 'last unless'
263 # should catch any failures - or short-circuit this if this *is* a
264 # bug in the visitor and gets fixed.
266 my $extra = $path[ ( scalar @match ) - 1 ];
268 $visitor->setSearchPath($extra);
269 $match[-1]->accept($visitor);
270 push( @match, $visitor->getResult ) if defined $visitor->getResult;
273 return map { $_->getNodeValue } @match;
277 my ( $self, $c, $action ) = @_;
279 my $namespace = $action->namespace;
280 my $parent = $self->tree;
281 my $visitor = Tree::Simple::Visitor::FindByPath->new;
284 for my $part ( split '/', $namespace ) {
285 $visitor->setSearchPath($part);
286 $parent->accept($visitor);
287 my $child = $visitor->getResult;
291 # Create a new tree node and an ActionContainer to form
295 Catalyst::ActionContainer->new(
296 { part => $part, actions => {} } );
297 $child = $parent->addChild( Tree::Simple->new($container) );
298 $visitor->setSearchPath($part);
299 $parent->accept($visitor);
300 $child = $visitor->getResult;
307 # Set the method value
308 $parent->getNodeValue->actions->{ $action->name } = $action;
310 my $registered = $self->registered_dispatch_types;
312 foreach my $key ( keys %{ $action->attributes } ) {
313 my $class = "Catalyst::DispatchType::$key";
314 unless ( $registered->{$class} ) {
315 eval "require $class";
316 push( @{ $self->dispatch_types }, $class->new ) unless $@;
317 $registered->{$class} = 1;
321 # Pass the action to our dispatch types so they can register it if reqd.
322 foreach my $type ( @{ $self->dispatch_types } ) {
323 $type->register( $c, $action );
327 =item $self->setup_actions( $class, $component )
332 my ( $self, $c ) = @_;
334 $self->dispatch_types( [] );
335 $self->registered_dispatch_types( {} );
336 $self->method_action_class('Catalyst::Action');
337 $self->action_container_class('Catalyst::ActionContainer');
339 # Preload action types
340 for my $type (@PRELOAD) {
341 my $class = "Catalyst::DispatchType::$type";
342 eval "require $class";
343 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
345 push @{ $self->dispatch_types }, $class->new;
346 $self->registered_dispatch_types->{$class} = 1;
351 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
352 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
354 $c->register_actions($c);
356 foreach my $comp ( values %{ $c->components } ) {
357 $comp->register_actions($c) if $comp->can('register_actions');
360 # Postload action types
361 for my $type (@POSTLOAD) {
362 my $class = "Catalyst::DispatchType::$type";
363 eval "require $class";
364 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
366 push @{ $self->dispatch_types }, $class->new;
369 return unless $c->debug;
371 my $privates = Text::SimpleTable->new( [ 36, 'Private' ], [ 37, 'Class' ] );
375 my ( $walker, $parent, $prefix ) = @_;
376 $prefix .= $parent->getNodeValue || '';
377 $prefix .= '/' unless $prefix =~ /\/$/;
378 my $node = $parent->getNodeValue->actions;
380 for my $action ( keys %{$node} ) {
381 my $action_obj = $node->{$action};
383 if ( ( $action =~ /^_.*/ )
384 && ( !$c->config->{show_internal_actions} ) );
385 $privates->row( "$prefix$action", $action_obj->class );
389 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
392 $walker->( $walker, $self->tree, '' );
393 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
396 # List all public actions
397 $_->list($c) for @{ $self->dispatch_types };
404 Sebastian Riedel, C<sri@cpan.org>
405 Matt S Trout, C<mst@shadowcatsystems.co.uk>
409 This program is free software, you can redistribute it and/or modify it under
410 the same terms as Perl itself.