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
21 preload_dispatch_types postload_dispatch_types
25 # Preload these action types
26 our @PRELOAD = qw/Index Path Regex/;
28 # Postload these action types
29 our @POSTLOAD = qw/Default/;
33 Catalyst::Dispatcher - The Catalyst Dispatcher
47 my $class = ref($self) || $self;
49 my $obj = $class->SUPER::new( @_ );
51 # set the default pre- and and postloads
52 $obj->preload_dispatch_types( \@PRELOAD );
53 $obj->postload_dispatch_types( \@POSTLOAD );
57 =head2 $self->preload_dispatch_types
59 An arrayref of pre-loaded dispatchtype classes
61 Entries are considered to be available as C<Catalyst::DispatchType::CLASS>
62 To use a custom class outside the regular C<Catalyst> namespace, prefix
63 it with a C<+>, like so:
67 =head2 $self->postload_dispatch_types
69 An arrayref of post-loaded dispatchtype classes
71 Entries are considered to be available as C<Catalyst::DispatchType::CLASS>
72 To use a custom class outside the regular C<Catalyst> namespace, prefix
73 it with a C<+>, like so:
77 =head2 $self->detach( $c, $command [, \@arguments ] )
82 my ( $self, $c, $command, @args ) = @_;
83 $c->forward( $command, @args ) if $command;
84 die $Catalyst::DETACH;
87 =head2 $self->dispatch($c)
92 my ( $self, $c ) = @_;
94 $c->forward( join( '/', '', $c->action->namespace, '_DISPATCH' ) );
98 my $path = $c->req->path;
100 ? qq/Unknown resource "$path"/
101 : "No default action defined";
102 $c->log->error($error) if $c->debug;
107 =head2 $self->forward( $c, $command [, \@arguments ] )
117 $c->log->debug('Nothing to forward to') if $c->debug;
122 my $arguments = $c->req->args;
123 if ( ref( $_[-1] ) eq 'ARRAY' ) {
124 $arguments = pop(@_);
130 unless ( ref $command ) {
131 my $command_copy = $command;
133 unless ( $command_copy =~ s/^\/// ) {
134 my $namespace = $c->stack->[-1]->namespace;
135 $command_copy = "${namespace}/${command}";
138 unless ( $command_copy =~ /\// ) {
139 $result = $c->get_action( $command_copy, '/' );
143 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
145 $result = $c->get_action( $tail, $1 );
149 unshift( @{$arguments}, @extra_args );
152 unshift( @extra_args, $tail );
159 my $class = ref($command)
160 || ref( $c->component($command) )
161 || $c->component($command);
162 my $method = shift || 'process';
166 qq/Couldn't forward to command "$command". Invalid action or component./;
168 $c->log->debug($error) if $c->debug;
172 if ( my $code = $class->can($method) ) {
173 my $action = $self->method_action_class->new(
177 reverse => "$class->$method",
179 namespace => Catalyst::Utils::class2prefix(
180 $class, $c->config->{case_sensitive}
189 qq/Couldn't forward to "$class". Does not implement "$method"/;
191 $c->log->debug($error)
199 local $c->request->{arguments} = [ @{$arguments} ];
200 $result->execute($c);
202 else { $result->execute($c) }
207 =head2 $self->prepare_action($c)
212 my ( $self, $c ) = @_;
213 my $path = $c->req->path;
214 my @path = split /\//, $c->req->path;
215 $c->req->args( \my @args );
217 unshift( @path, '' ); # Root action
219 DESCEND: while (@path) {
220 $path = join '/', @path;
223 $path = '' if $path eq '/'; # Root action
225 # Check out dispatch types to see if any will handle the path at
228 foreach my $type ( @{ $self->dispatch_types } ) {
229 last DESCEND if $type->match( $c, $path );
232 # If not, move the last part path to args
233 my $arg = pop(@path);
234 $arg =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
238 $c->log->debug( 'Path is "' . $c->req->match . '"' )
239 if ( $c->debug && $c->req->match );
241 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
242 if ( $c->debug && @args );
245 =head2 $self->get_action( $action, $namespace )
250 my ( $self, $name, $namespace ) = @_;
253 $namespace = '' if $namespace eq '/';
255 my @match = $self->get_containers($namespace);
257 return unless @match;
259 if ( my $action = $match[-1]->get_action($name) ) {
260 return $action if $action->namespace eq $namespace;
264 =head2 $self->get_actions( $c, $action, $namespace )
269 my ( $self, $c, $action, $namespace ) = @_;
270 return [] unless $action;
272 $namespace = '' if $namespace eq '/';
274 my @match = $self->get_containers($namespace);
276 return map { $_->get_action($action) } @match;
279 =head2 $self->get_containers( $namespace )
284 my ( $self, $namespace ) = @_;
286 # If the namespace is / just return the root ActionContainer
288 return ( $self->tree->getNodeValue )
289 if ( !$namespace || ( $namespace eq '/' ) );
291 # Use a visitor to recurse down the tree finding the ActionContainers
292 # for each namespace in the chain.
294 my $visitor = Tree::Simple::Visitor::FindByPath->new;
295 my @path = split( '/', $namespace );
296 $visitor->setSearchPath(@path);
297 $self->tree->accept($visitor);
299 my @match = $visitor->getResults;
300 @match = ( $self->tree ) unless @match;
302 if ( !defined $visitor->getResult ) {
304 # If we don't manage to match, the visitor doesn't return the last
305 # node is matched, so foo/bar/baz would only find the 'foo' node,
306 # not the foo and foo/bar nodes as it should. This does another
307 # single-level search to see if that's the case, and the 'last unless'
308 # should catch any failures - or short-circuit this if this *is* a
309 # bug in the visitor and gets fixed.
311 if ( my $extra = $path[ ( scalar @match ) - 1 ] ) {
312 $visitor->setSearchPath($extra);
313 $match[-1]->accept($visitor);
314 push( @match, $visitor->getResult ) if defined $visitor->getResult;
318 return map { $_->getNodeValue } @match;
321 =head2 $self->register( $c, $action )
326 my ( $self, $c, $action ) = @_;
328 my $registered = $self->registered_dispatch_types;
331 foreach my $key ( keys %{ $action->attributes } ) {
332 $priv++ if $key eq 'Private';
333 my $class = "Catalyst::DispatchType::$key";
334 unless ( $registered->{$class} ) {
335 eval "require $class";
336 push( @{ $self->dispatch_types }, $class->new ) unless $@;
337 $registered->{$class} = 1;
341 # Pass the action to our dispatch types so they can register it if reqd.
343 foreach my $type ( @{ $self->dispatch_types } ) {
344 $reg++ if $type->register( $c, $action );
347 return unless $reg + $priv;
349 my $namespace = $action->namespace;
350 my $parent = $self->tree;
351 my $visitor = Tree::Simple::Visitor::FindByPath->new;
354 for my $part ( split '/', $namespace ) {
355 $visitor->setSearchPath($part);
356 $parent->accept($visitor);
357 my $child = $visitor->getResult;
361 # Create a new tree node and an ActionContainer to form
365 Catalyst::ActionContainer->new(
366 { part => $part, actions => {} } );
367 $child = $parent->addChild( Tree::Simple->new($container) );
368 $visitor->setSearchPath($part);
369 $parent->accept($visitor);
370 $child = $visitor->getResult;
377 # Set the method value
378 $parent->getNodeValue->actions->{ $action->name } = $action;
381 =head2 $self->setup_actions( $class, $component )
386 my ( $self, $c ) = @_;
388 $self->dispatch_types( [] );
389 $self->registered_dispatch_types( {} );
390 $self->method_action_class('Catalyst::Action');
391 $self->action_container_class('Catalyst::ActionContainer');
393 # Preload action types
394 for my $type ( @{$self->preload_dispatch_types} ) {
395 my $class = ($type =~ /^\+(.*)$/) ? $1 : "Catalyst::DispatchType::${type}";
396 eval "require $class";
397 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
399 push @{ $self->dispatch_types }, $class->new;
400 $self->registered_dispatch_types->{$class} = 1;
405 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
406 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
408 foreach my $comp ( values %{ $c->components } ) {
409 $comp->register_actions($c) if $comp->can('register_actions');
412 # Postload action types
413 for my $type ( @{$self->postload_dispatch_types} ) {
414 my $class = ($type =~ /^\+(.*)$/) ? $1 : "Catalyst::DispatchType::${type}";
415 eval "require $class";
416 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
418 push @{ $self->dispatch_types }, $class->new;
421 return unless $c->debug;
423 my $privates = Text::SimpleTable->new(
431 my ( $walker, $parent, $prefix ) = @_;
432 $prefix .= $parent->getNodeValue || '';
433 $prefix .= '/' unless $prefix =~ /\/$/;
434 my $node = $parent->getNodeValue->actions;
436 for my $action ( keys %{$node} ) {
437 my $action_obj = $node->{$action};
439 if ( ( $action =~ /^_.*/ )
440 && ( !$c->config->{show_internal_actions} ) );
441 $privates->row( "$prefix$action", $action_obj->class, $action );
445 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
448 $walker->( $walker, $self->tree, '' );
449 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
452 # List all public actions
453 $_->list($c) for @{ $self->dispatch_types };
458 Sebastian Riedel, C<sri@cpan.org>
459 Matt S Trout, C<mst@shadowcatsystems.co.uk>
463 This program is free software, you can redistribute it and/or modify it under
464 the same terms as Perl itself.