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
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) || ref( $c->component($command) );
124 my $method = shift || 'process';
128 qq/Couldn't forward to command "$command". Invalid action or component./;
130 $c->log->debug($error) if $c->debug;
134 if ( my $code = $class->can($method) ) {
135 my $action = $self->method_action_class->new(
139 reverse => "$class->$method",
141 namespace => Catalyst::Utils::class2prefix(
142 $class, $c->config->{case_sensitive}
151 qq/Couldn't forward to "$class". Does not implement "$method"/;
153 $c->log->debug($error)
161 local $c->request->{arguments} = [ @{$arguments} ];
162 $result->execute($c);
164 else { $result->execute($c) }
169 =head2 $self->prepare_action($c)
174 my ( $self, $c ) = @_;
175 my $path = $c->req->path;
176 my @path = split /\//, $c->req->path;
177 $c->req->args( \my @args );
179 push( @path, '/' ) unless @path; # Root action
181 DESCEND: while (@path) {
182 $path = join '/', @path;
184 $path = '' if $path eq '/'; # Root action
186 # Check out dispatch types to see if any will handle the path at
189 foreach my $type ( @{ $self->dispatch_types } ) {
190 last DESCEND if $type->match( $c, $path );
193 # If not, move the last part path to args
194 unshift @args, pop @path;
197 $c->log->debug( 'Path is "' . $c->req->match . '"' )
198 if ( $c->debug && $c->req->match );
200 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
201 if ( $c->debug && @args );
204 =head2 $self->get_action( $action, $namespace )
209 my ( $self, $name, $namespace ) = @_;
212 $namespace = '' if $namespace eq '/';
214 my @match = $self->get_containers($namespace);
216 return unless @match;
218 if ( my $action = $match[-1]->get_action($name) ) {
219 return $action if $action->namespace eq $namespace;
223 =head2 $self->get_actions( $c, $action, $namespace )
228 my ( $self, $c, $action, $namespace ) = @_;
229 return [] unless $action;
231 $namespace = '' if $namespace eq '/';
233 my @match = $self->get_containers($namespace);
235 return map { $_->get_action($action) } @match;
238 =head2 $self->get_containers( $namespace )
243 my ( $self, $namespace ) = @_;
245 # If the namespace is / just return the root ActionContainer
247 return ( $self->tree->getNodeValue )
248 if ( !$namespace || ( $namespace eq '/' ) );
250 # Use a visitor to recurse down the tree finding the ActionContainers
251 # for each namespace in the chain.
253 my $visitor = Tree::Simple::Visitor::FindByPath->new;
254 my @path = split( '/', $namespace );
255 $visitor->setSearchPath(@path);
256 $self->tree->accept($visitor);
258 my @match = $visitor->getResults;
259 @match = ( $self->tree ) unless @match;
261 if ( !defined $visitor->getResult ) {
263 # If we don't manage to match, the visitor doesn't return the last
264 # node is matched, so foo/bar/baz would only find the 'foo' node,
265 # not the foo and foo/bar nodes as it should. This does another
266 # single-level search to see if that's the case, and the 'last unless'
267 # should catch any failures - or short-circuit this if this *is* a
268 # bug in the visitor and gets fixed.
270 if ( my $extra = $path[ ( scalar @match ) - 1 ] ) {
271 $visitor->setSearchPath($extra);
272 $match[-1]->accept($visitor);
273 push( @match, $visitor->getResult ) if defined $visitor->getResult;
277 return map { $_->getNodeValue } @match;
280 =head2 $self->register( $c, $action )
285 my ( $self, $c, $action ) = @_;
287 my $registered = $self->registered_dispatch_types;
290 foreach my $key ( keys %{ $action->attributes } ) {
291 $priv++ if $key eq 'Private';
292 my $class = "Catalyst::DispatchType::$key";
293 unless ( $registered->{$class} ) {
294 eval "require $class";
295 push( @{ $self->dispatch_types }, $class->new ) unless $@;
296 $registered->{$class} = 1;
300 # Pass the action to our dispatch types so they can register it if reqd.
302 foreach my $type ( @{ $self->dispatch_types } ) {
303 $reg++ if $type->register( $c, $action );
306 return unless $reg + $priv;
308 my $namespace = $action->namespace;
309 my $parent = $self->tree;
310 my $visitor = Tree::Simple::Visitor::FindByPath->new;
313 for my $part ( split '/', $namespace ) {
314 $visitor->setSearchPath($part);
315 $parent->accept($visitor);
316 my $child = $visitor->getResult;
320 # Create a new tree node and an ActionContainer to form
324 Catalyst::ActionContainer->new(
325 { part => $part, actions => {} } );
326 $child = $parent->addChild( Tree::Simple->new($container) );
327 $visitor->setSearchPath($part);
328 $parent->accept($visitor);
329 $child = $visitor->getResult;
336 # Set the method value
337 $parent->getNodeValue->actions->{ $action->name } = $action;
340 =head2 $self->setup_actions( $class, $component )
345 my ( $self, $c ) = @_;
347 $self->dispatch_types( [] );
348 $self->registered_dispatch_types( {} );
349 $self->method_action_class('Catalyst::Action');
350 $self->action_container_class('Catalyst::ActionContainer');
352 # Preload action types
353 for my $type (@PRELOAD) {
354 my $class = "Catalyst::DispatchType::$type";
355 eval "require $class";
356 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
358 push @{ $self->dispatch_types }, $class->new;
359 $self->registered_dispatch_types->{$class} = 1;
364 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
365 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
367 foreach my $comp ( values %{ $c->components } ) {
368 $comp->register_actions($c) if $comp->can('register_actions');
371 # Postload action types
372 for my $type (@POSTLOAD) {
373 my $class = "Catalyst::DispatchType::$type";
374 eval "require $class";
375 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
377 push @{ $self->dispatch_types }, $class->new;
380 return unless $c->debug;
382 my $privates = Text::SimpleTable->new(
390 my ( $walker, $parent, $prefix ) = @_;
391 $prefix .= $parent->getNodeValue || '';
392 $prefix .= '/' unless $prefix =~ /\/$/;
393 my $node = $parent->getNodeValue->actions;
395 for my $action ( keys %{$node} ) {
396 my $action_obj = $node->{$action};
398 if ( ( $action =~ /^_.*/ )
399 && ( !$c->config->{show_internal_actions} ) );
400 $privates->row( "$prefix$action", $action_obj->class, $action );
404 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
407 $walker->( $walker, $self->tree, '' );
408 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
411 # List all public actions
412 $_->list($c) for @{ $self->dispatch_types };
417 Sebastian Riedel, C<sri@cpan.org>
418 Matt S Trout, C<mst@shadowcatsystems.co.uk>
422 This program is free software, you can redistribute it and/or modify it under
423 the same terms as Perl itself.