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 );
120 my $class = ref($command) || ref( $c->component($command) );
121 my $method = shift || 'process';
125 qq/Couldn't forward to command "$command". Invalid action or component./;
127 $c->log->debug($error) if $c->debug;
131 if ( my $code = $class->can($method) ) {
132 my $action = $self->method_action_class->new(
136 reverse => "$class->$method",
146 qq/Couldn't forward to "$class". Does not implement "$method"/;
148 $c->log->debug($error)
155 local $c->request->{arguments} = [ @{$arguments} ];
157 $result->execute($c);
162 =item $self->prepare_action($c)
167 my ( $self, $c ) = @_;
168 my $path = $c->req->path;
169 my @path = split /\//, $c->req->path;
170 $c->req->args( \my @args );
172 push( @path, '/' ) unless @path; # Root action
174 DESCEND: while (@path) {
175 $path = join '/', @path;
177 $path = '' if $path eq '/'; # Root action
179 # Check out dispatch types to see if any will handle the path at
182 foreach my $type ( @{ $self->dispatch_types } ) {
183 last DESCEND if $type->match( $c, $path );
186 # If not, move the last part path to args
188 unshift @args, pop @path;
191 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
192 if ( $c->debug && @args );
195 =item $self->get_action( $action, $namespace )
200 my ( $self, $name, $namespace ) = @_;
203 $namespace = '' if $namespace eq '/';
205 my @match = $self->get_containers($namespace);
207 return unless @match;
209 if ( my $action = $match[-1]->get_action($name) ) {
210 return $action if $action->namespace eq $namespace;
214 =item $self->get_actions( $c, $action, $namespace )
219 my ( $self, $c, $action, $namespace ) = @_;
220 return [] unless $action;
222 $namespace = '' if $namespace eq '/';
224 my @match = $self->get_containers($namespace);
226 return map { $_->get_action($action) } @match;
229 =item $self->get_containers( $namespace )
234 my ( $self, $namespace ) = @_;
236 # If the namespace is / just return the root ActionContainer
238 return ( $self->tree->getNodeValue )
239 if ( !$namespace || ( $namespace eq '/' ) );
241 # Use a visitor to recurse down the tree finding the ActionContainers
242 # for each namespace in the chain.
244 my $visitor = Tree::Simple::Visitor::FindByPath->new;
245 my @path = split( '/', $namespace );
246 $visitor->setSearchPath(@path);
247 $self->tree->accept($visitor);
249 my @match = $visitor->getResults;
250 @match = ( $self->tree ) unless @match;
252 if ( !defined $visitor->getResult ) {
254 # If we don't manage to match, the visitor doesn't return the last
255 # node is matched, so foo/bar/baz would only find the 'foo' node,
256 # not the foo and foo/bar nodes as it should. This does another
257 # single-level search to see if that's the case, and the 'last unless'
258 # should catch any failures - or short-circuit this if this *is* a
259 # bug in the visitor and gets fixed.
261 if ( my $extra = $path[ ( scalar @match ) - 1 ] ) {
262 $visitor->setSearchPath($extra);
263 $match[-1]->accept($visitor);
264 push( @match, $visitor->getResult ) if defined $visitor->getResult;
268 return map { $_->getNodeValue } @match;
272 my ( $self, $c, $action ) = @_;
274 my $registered = $self->registered_dispatch_types;
277 foreach my $key ( keys %{ $action->attributes } ) {
278 $priv++ if $key eq 'Private';
279 my $class = "Catalyst::DispatchType::$key";
280 unless ( $registered->{$class} ) {
281 eval "require $class";
282 push( @{ $self->dispatch_types }, $class->new ) unless $@;
283 $registered->{$class} = 1;
287 # Pass the action to our dispatch types so they can register it if reqd.
289 foreach my $type ( @{ $self->dispatch_types } ) {
290 $reg++ if $type->register( $c, $action );
293 return unless $reg + $priv;
295 my $namespace = $action->namespace;
296 my $parent = $self->tree;
297 my $visitor = Tree::Simple::Visitor::FindByPath->new;
300 for my $part ( split '/', $namespace ) {
301 $visitor->setSearchPath($part);
302 $parent->accept($visitor);
303 my $child = $visitor->getResult;
307 # Create a new tree node and an ActionContainer to form
311 Catalyst::ActionContainer->new(
312 { part => $part, actions => {} } );
313 $child = $parent->addChild( Tree::Simple->new($container) );
314 $visitor->setSearchPath($part);
315 $parent->accept($visitor);
316 $child = $visitor->getResult;
323 # Set the method value
324 $parent->getNodeValue->actions->{ $action->name } = $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 foreach my $comp ( values %{ $c->components } ) {
355 $comp->register_actions($c) if $comp->can('register_actions');
358 # Postload action types
359 for my $type (@POSTLOAD) {
360 my $class = "Catalyst::DispatchType::$type";
361 eval "require $class";
362 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
364 push @{ $self->dispatch_types }, $class->new;
367 return unless $c->debug;
369 my $privates = Text::SimpleTable->new(
377 my ( $walker, $parent, $prefix ) = @_;
378 $prefix .= $parent->getNodeValue || '';
379 $prefix .= '/' unless $prefix =~ /\/$/;
380 my $node = $parent->getNodeValue->actions;
382 for my $action ( keys %{$node} ) {
383 my $action_obj = $node->{$action};
385 if ( ( $action =~ /^_.*/ )
386 && ( !$c->config->{show_internal_actions} ) );
387 $privates->row( "$prefix$action", $action_obj->class, $action );
391 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
394 $walker->( $walker, $self->tree, '' );
395 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
398 # List all public actions
399 $_->list($c) for @{ $self->dispatch_types };
406 Sebastian Riedel, C<sri@cpan.org>
407 Matt S Trout, C<mst@shadowcatsystems.co.uk>
411 This program is free software, you can redistribute it and/or modify it under
412 the same terms as Perl itself.