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/Index Path Regex/;
26 # Postload these action types
27 our @POSTLOAD = qw/Default/;
31 Catalyst::Dispatcher - The Catalyst Dispatcher
41 =head2 $self->detach( $c, $command [, \@arguments ] )
46 my ( $self, $c, $command, @args ) = @_;
47 $c->forward( $command, @args ) if $command;
48 die $Catalyst::DETACH;
51 =head2 $self->dispatch($c)
56 my ( $self, $c ) = @_;
58 $c->forward( join( '/', '', $c->action->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 =head2 $self->forward( $c, $command [, \@arguments ] )
81 $c->log->debug('Nothing to forward to') if $c->debug;
86 my $arguments = $c->req->args;
87 if ( ref( $_[-1] ) eq 'ARRAY' ) {
94 unless ( ref $command ) {
95 my $command_copy = $command;
97 unless ( $command_copy =~ s/^\/// ) {
98 my $namespace = $c->stack->[-1]->namespace;
99 $command_copy = "${namespace}/${command}";
102 unless ( $command_copy =~ /\// ) {
103 $result = $c->get_action( $command_copy, '/' );
107 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
109 $result = $c->get_action( $tail, $1 );
113 unshift( @{$arguments}, @extra_args );
116 unshift( @extra_args, $tail );
123 my $class = ref($command)
124 || ref( $c->component($command) )
125 || $c->component($command);
126 my $method = shift || 'process';
130 qq/Couldn't forward to command "$command". Invalid action or component./;
132 $c->log->debug($error) if $c->debug;
136 if ( my $code = $class->can($method) ) {
137 my $action = $self->method_action_class->new(
141 reverse => "$class->$method",
143 namespace => Catalyst::Utils::class2prefix(
144 $class, $c->config->{case_sensitive}
153 qq/Couldn't forward to "$class". Does not implement "$method"/;
155 $c->log->debug($error)
163 local $c->request->{arguments} = [ @{$arguments} ];
164 $result->execute($c);
166 else { $result->execute($c) }
171 =head2 $self->prepare_action($c)
176 my ( $self, $c ) = @_;
177 my $path = $c->req->path;
178 my @path = split /\//, $c->req->path;
179 $c->req->args( \my @args );
181 unshift( @path, '' ); # Root action
183 DESCEND: while (@path) {
184 $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
197 unshift @args, pop @path;
200 $c->log->debug( 'Path is "' . $c->req->match . '"' )
201 if ( $c->debug && $c->req->match );
203 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
204 if ( $c->debug && @args );
207 =head2 $self->get_action( $action, $namespace )
212 my ( $self, $name, $namespace ) = @_;
215 $namespace = '' if $namespace eq '/';
217 my @match = $self->get_containers($namespace);
219 return unless @match;
221 if ( my $action = $match[-1]->get_action($name) ) {
222 return $action if $action->namespace eq $namespace;
226 =head2 $self->get_actions( $c, $action, $namespace )
231 my ( $self, $c, $action, $namespace ) = @_;
232 return [] unless $action;
234 $namespace = '' if $namespace eq '/';
236 my @match = $self->get_containers($namespace);
238 return map { $_->get_action($action) } @match;
241 =head2 $self->get_containers( $namespace )
246 my ( $self, $namespace ) = @_;
248 # If the namespace is / just return the root ActionContainer
250 return ( $self->tree->getNodeValue )
251 if ( !$namespace || ( $namespace eq '/' ) );
253 # Use a visitor to recurse down the tree finding the ActionContainers
254 # for each namespace in the chain.
256 my $visitor = Tree::Simple::Visitor::FindByPath->new;
257 my @path = split( '/', $namespace );
258 $visitor->setSearchPath(@path);
259 $self->tree->accept($visitor);
261 my @match = $visitor->getResults;
262 @match = ( $self->tree ) unless @match;
264 if ( !defined $visitor->getResult ) {
266 # If we don't manage to match, the visitor doesn't return the last
267 # node is matched, so foo/bar/baz would only find the 'foo' node,
268 # not the foo and foo/bar nodes as it should. This does another
269 # single-level search to see if that's the case, and the 'last unless'
270 # should catch any failures - or short-circuit this if this *is* a
271 # bug in the visitor and gets fixed.
273 if ( my $extra = $path[ ( scalar @match ) - 1 ] ) {
274 $visitor->setSearchPath($extra);
275 $match[-1]->accept($visitor);
276 push( @match, $visitor->getResult ) if defined $visitor->getResult;
280 return map { $_->getNodeValue } @match;
283 =head2 $self->register( $c, $action )
288 my ( $self, $c, $action ) = @_;
290 my $registered = $self->registered_dispatch_types;
293 foreach my $key ( keys %{ $action->attributes } ) {
294 $priv++ if $key eq 'Private';
295 my $class = "Catalyst::DispatchType::$key";
296 unless ( $registered->{$class} ) {
297 eval "require $class";
298 push( @{ $self->dispatch_types }, $class->new ) unless $@;
299 $registered->{$class} = 1;
303 # Pass the action to our dispatch types so they can register it if reqd.
305 foreach my $type ( @{ $self->dispatch_types } ) {
306 $reg++ if $type->register( $c, $action );
309 return unless $reg + $priv;
311 my $namespace = $action->namespace;
312 my $parent = $self->tree;
313 my $visitor = Tree::Simple::Visitor::FindByPath->new;
316 for my $part ( split '/', $namespace ) {
317 $visitor->setSearchPath($part);
318 $parent->accept($visitor);
319 my $child = $visitor->getResult;
323 # Create a new tree node and an ActionContainer to form
327 Catalyst::ActionContainer->new(
328 { part => $part, actions => {} } );
329 $child = $parent->addChild( Tree::Simple->new($container) );
330 $visitor->setSearchPath($part);
331 $parent->accept($visitor);
332 $child = $visitor->getResult;
339 # Set the method value
340 $parent->getNodeValue->actions->{ $action->name } = $action;
343 =head2 $self->setup_actions( $class, $component )
348 my ( $self, $c ) = @_;
350 $self->dispatch_types( [] );
351 $self->registered_dispatch_types( {} );
352 $self->method_action_class('Catalyst::Action');
353 $self->action_container_class('Catalyst::ActionContainer');
355 # Preload action types
356 for my $type (@PRELOAD) {
357 my $class = "Catalyst::DispatchType::$type";
358 eval "require $class";
359 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
361 push @{ $self->dispatch_types }, $class->new;
362 $self->registered_dispatch_types->{$class} = 1;
367 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
368 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
370 foreach my $comp ( values %{ $c->components } ) {
371 $comp->register_actions($c) if $comp->can('register_actions');
374 # Postload action types
375 for my $type (@POSTLOAD) {
376 my $class = "Catalyst::DispatchType::$type";
377 eval "require $class";
378 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
380 push @{ $self->dispatch_types }, $class->new;
383 return unless $c->debug;
385 my $privates = Text::SimpleTable->new(
393 my ( $walker, $parent, $prefix ) = @_;
394 $prefix .= $parent->getNodeValue || '';
395 $prefix .= '/' unless $prefix =~ /\/$/;
396 my $node = $parent->getNodeValue->actions;
398 for my $action ( keys %{$node} ) {
399 my $action_obj = $node->{$action};
401 if ( ( $action =~ /^_.*/ )
402 && ( !$c->config->{show_internal_actions} ) );
403 $privates->row( "$prefix$action", $action_obj->class, $action );
407 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
410 $walker->( $walker, $self->tree, '' );
411 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
414 # List all public actions
415 $_->list($c) for @{ $self->dispatch_types };
420 Sebastian Riedel, C<sri@cpan.org>
421 Matt S Trout, C<mst@shadowcatsystems.co.uk>
425 This program is free software, you can redistribute it and/or modify it under
426 the same terms as Perl itself.