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(qw/tree dispatch_types registered_dispatch_types
19 method_action_class action_container_class/);
21 # Preload these action types
22 our @PRELOAD = qw/Path Regex/;
24 # Postload these action types
25 our @POSTLOAD = qw/Index Default/;
29 Catalyst::Dispatcher - The Catalyst Dispatcher
41 =item $self->detach( $c, $command [, \@arguments ] )
46 my ( $self, $c, $command, @args ) = @_;
47 $c->forward( $command, @args ) if $command;
48 die $Catalyst::DETACH;
51 =item $self->dispatch($c)
56 my ( $self, $c ) = @_;
59 $c->forward( join( '/', '', $c->namespace, '_DISPATCH' ) );
63 my $path = $c->req->path;
65 ? qq/Unknown resource "$path"/
66 : "No default action defined";
67 $c->log->error($error) if $c->debug;
72 =item $self->forward( $c, $command [, \@arguments ] )
82 $c->log->debug('Nothing to forward to') if $c->debug;
86 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
90 my $command_copy = $command;
92 unless ( $command_copy =~ s/^\/// ) {
93 my $namespace = $c->namespace;
94 $command_copy = "${namespace}/${command}";
97 unless ( $command_copy =~ /\// ) {
98 $result = $c->get_action( $command_copy, '/' );
102 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
104 $result = $c->get_action( $tail, $1 );
107 push( @{$arguments}, @extra_args );
110 unshift( @extra_args, $tail );
118 unless ( $comp = $c->component($command) ) {
120 qq/Couldn't forward to command "$command". Invalid action or component./;
122 $c->log->debug($error) if $c->debug;
126 my $class = ref $comp;
127 my $method = shift || 'process';
129 if ( my $code = $class->can($method) ) {
130 my $action = $self->method_action_class->new(
134 reverse => "$class->$method",
144 qq/Couldn't forward to "$class". Does not implement "$method"/;
146 $c->log->debug($error)
153 local $c->request->{arguments} = [ @{$arguments} ];
155 $result->execute($c);
160 =item $self->prepare_action($c)
165 my ( $self, $c ) = @_;
166 my $path = $c->req->path;
167 my @path = split /\//, $c->req->path;
168 $c->req->args( \my @args );
170 push( @path, '/' ) unless @path; # Root action
172 DESCEND: while (@path) {
173 $path = join '/', @path;
175 $path = '' if $path eq '/'; # Root action
177 # Check out dispatch types to see if any will handle the path at
180 foreach my $type ( @{ $self->dispatch_types } ) {
181 last DESCEND if $type->match( $c, $path );
184 # If not, move the last part path to args
186 unshift @args, pop @path;
189 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
190 if ( $c->debug && @args );
193 =item $self->get_action( $c, $action, $namespace )
198 my ( $self, $c, $name, $namespace ) = @_;
201 $namespace = '' if $namespace eq '/';
203 my @match = $self->get_containers($namespace);
205 return unless @match;
207 if ( my $action = $match[-1]->get_action( $c, $name ) )
209 return $action if $action->namespace eq $namespace;
213 =item $self->get_actions( $c, $action, $namespace )
218 my ( $self, $c, $action, $namespace ) = @_;
219 return [] unless $action;
221 $namespace = '' if $namespace eq '/';
223 my @match = $self->get_containers($namespace);
225 return map { $_->get_action($c, $action) } @match;
228 =item $self->get_containers( $namespace )
233 my ( $self, $namespace ) = @_;
235 # If the namespace is / just return the root ActionContainer
237 return ( $self->tree->getNodeValue )
238 if ( !$namespace || ( $namespace eq '/' ) );
240 # Use a visitor to recurse down the tree finding the ActionContainers
241 # for each namespace in the chain.
243 my $visitor = Tree::Simple::Visitor::FindByPath->new;
244 my @path = split( '/', $namespace );
245 $visitor->setSearchPath(@path);
246 $self->tree->accept($visitor);
248 my @match = $visitor->getResults;
249 @match = ( $self->tree ) unless @match;
251 if ( !defined $visitor->getResult ) {
253 # If we don't manage to match, the visitor doesn't return the last
254 # node is matched, so foo/bar/baz would only find the 'foo' node,
255 # not the foo and foo/bar nodes as it should. This does another
256 # single-level search to see if that's the case, and the 'last unless'
257 # should catch any failures - or short-circuit this if this *is* a
258 # bug in the visitor and gets fixed.
260 my $extra = $path[ ( scalar @match ) - 1 ];
262 $visitor->setSearchPath($extra);
263 $match[-1]->accept($visitor);
264 push( @match, $visitor->getResult ) if defined $visitor->getResult;
267 return map { $_->getNodeValue } @match;
271 my ( $self, $c, $action ) = @_;
273 my $namespace = $action->namespace;
274 my $parent = $self->tree;
275 my $visitor = Tree::Simple::Visitor::FindByPath->new;
278 for my $part ( split '/', $namespace ) {
279 $visitor->setSearchPath($part);
280 $parent->accept($visitor);
281 my $child = $visitor->getResult;
285 # Create a new tree node and an ActionContainer to form
289 Catalyst::ActionContainer->new(
290 { part => $part, actions => {} } );
291 $child = $parent->addChild( Tree::Simple->new($container) );
292 $visitor->setSearchPath($part);
293 $parent->accept($visitor);
294 $child = $visitor->getResult;
301 # Set the method value
302 $parent->getNodeValue->actions->{$action->name} = $action;
304 my $registered = $self->registered_dispatch_types;
306 foreach my $key (keys %{$action->attributes}) {
307 my $class = "Catalyst::DispatchType::$key";
308 unless ( $registered->{$class} ) {
309 eval "require $class";
310 push( @{ $self->dispatch_types }, $class->new ) unless $@;
311 $registered->{$class} = 1;
315 # Pass the action to our dispatch types so they can register it if reqd.
316 foreach my $type ( @{ $self->dispatch_types } ) {
317 $type->register( $c, $action );
321 =item $self->setup_actions( $class, $component )
326 my ( $self, $c ) = @_;
328 $self->dispatch_types( [] );
329 $self->registered_dispatch_types( {} );
330 $self->method_action_class( 'Catalyst::Action' );
331 $self->action_container_class( 'Catalyst::ActionContainer' );
333 # Preload action types
334 for my $type (@PRELOAD) {
335 my $class = "Catalyst::DispatchType::$type";
336 eval "require $class";
337 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
339 push @{ $self->dispatch_types }, $class->new;
340 $self->registered_dispatch_types->{$class} = 1;
345 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
346 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
348 $c->register_actions( $c );
350 foreach my $comp ( values %{$c->components} ) {
351 $comp->register_actions( $c ) if $comp->can('register_actions');
354 # Postload action types
355 for my $type (@POSTLOAD) {
356 my $class = "Catalyst::DispatchType::$type";
357 eval "require $class";
358 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
360 push @{ $self->dispatch_types }, $class->new;
363 return unless $c->debug;
366 = Text::SimpleTable->new( [ 36, 'Private' ], [ 37, 'Class' ] );
370 my ( $walker, $parent, $prefix ) = @_;
371 $prefix .= $parent->getNodeValue || '';
372 $prefix .= '/' unless $prefix =~ /\/$/;
373 my $node = $parent->getNodeValue->actions;
375 for my $action ( keys %{$node} ) {
376 my $action_obj = $node->{$action};
378 if ( ( $action =~ /^_.*/ )
379 && ( !$c->config->{show_internal_actions} ) );
380 $privates->row( "$prefix$action", $action_obj->class );
384 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
387 $walker->( $walker, $self->tree, '' );
388 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
391 # List all public actions
392 $_->list($c) for @{ $self->dispatch_types };
399 Sebastian Riedel, C<sri@cpan.org>
400 Matt S Trout, C<mst@shadowcatsystems.co.uk>
404 This program is free software, you can redistribute it and/or modify it under
405 the same terms as Perl itself.