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( $c, $action, $namespace )
200 my ( $self, $c, $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( $c, $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( $c, $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 $namespace = $action->namespace;
275 my $parent = $self->tree;
276 my $visitor = Tree::Simple::Visitor::FindByPath->new;
279 for my $part ( split '/', $namespace ) {
280 $visitor->setSearchPath($part);
281 $parent->accept($visitor);
282 my $child = $visitor->getResult;
286 # Create a new tree node and an ActionContainer to form
290 Catalyst::ActionContainer->new(
291 { part => $part, actions => {} } );
292 $child = $parent->addChild( Tree::Simple->new($container) );
293 $visitor->setSearchPath($part);
294 $parent->accept($visitor);
295 $child = $visitor->getResult;
302 # Set the method value
303 $parent->getNodeValue->actions->{ $action->name } = $action;
305 my $registered = $self->registered_dispatch_types;
307 foreach my $key ( keys %{ $action->attributes } ) {
308 my $class = "Catalyst::DispatchType::$key";
309 unless ( $registered->{$class} ) {
310 eval "require $class";
311 push( @{ $self->dispatch_types }, $class->new ) unless $@;
312 $registered->{$class} = 1;
316 # Pass the action to our dispatch types so they can register it if reqd.
317 foreach my $type ( @{ $self->dispatch_types } ) {
318 $type->register( $c, $action );
322 =item $self->setup_actions( $class, $component )
327 my ( $self, $c ) = @_;
329 $self->dispatch_types( [] );
330 $self->registered_dispatch_types( {} );
331 $self->method_action_class('Catalyst::Action');
332 $self->action_container_class('Catalyst::ActionContainer');
334 # Preload action types
335 for my $type (@PRELOAD) {
336 my $class = "Catalyst::DispatchType::$type";
337 eval "require $class";
338 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
340 push @{ $self->dispatch_types }, $class->new;
341 $self->registered_dispatch_types->{$class} = 1;
346 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
347 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
349 foreach my $comp ( values %{ $c->components } ) {
350 $comp->register_actions($c) if $comp->can('register_actions');
353 # Postload action types
354 for my $type (@POSTLOAD) {
355 my $class = "Catalyst::DispatchType::$type";
356 eval "require $class";
357 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
359 push @{ $self->dispatch_types }, $class->new;
362 return unless $c->debug;
364 my $privates = Text::SimpleTable->new( [ 36, 'Private' ], [ 37, 'Class' ] );
368 my ( $walker, $parent, $prefix ) = @_;
369 $prefix .= $parent->getNodeValue || '';
370 $prefix .= '/' unless $prefix =~ /\/$/;
371 my $node = $parent->getNodeValue->actions;
373 for my $action ( keys %{$node} ) {
374 my $action_obj = $node->{$action};
376 if ( ( $action =~ /^_.*/ )
377 && ( !$c->config->{show_internal_actions} ) );
378 $privates->row( "$prefix$action", $action_obj->class );
382 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
385 $walker->( $walker, $self->tree, '' );
386 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
389 # List all public actions
390 $_->list($c) for @{ $self->dispatch_types };
397 Sebastian Riedel, C<sri@cpan.org>
398 Matt S Trout, C<mst@shadowcatsystems.co.uk>
402 This program is free software, you can redistribute it and/or modify it under
403 the same terms as Perl itself.