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;
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/);
20 # Preload these action types
21 our @PRELOAD = qw/Path Regex/;
23 # Postload these action types
24 our @POSTLOAD = qw/Index Default/;
28 Catalyst::Dispatcher - The Catalyst Dispatcher
40 =item $self->detach( $c, $command [, \@arguments ] )
45 my ( $self, $c, $command, @args ) = @_;
46 $c->forward( $command, @args ) if $command;
47 die $Catalyst::DETACH;
50 =item $self->dispatch($c)
55 my ( $self, $c ) = @_;
58 $c->forward( join( '/', '', $c->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 =item $self->forward( $c, $command [, \@arguments ] )
81 $c->log->debug('Nothing to forward to') if $c->debug;
85 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
89 my $command_copy = $command;
91 unless ( $command_copy =~ s/^\/// ) {
92 my $namespace = $c->namespace;
93 $command_copy = "${namespace}/${command}";
96 unless ( $command_copy =~ /\// ) {
97 $result = $c->get_action( $command_copy, '/' );
101 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
103 $result = $c->get_action( $tail, $1 );
106 push( @{$arguments}, @extra_args );
109 unshift( @extra_args, $tail );
115 unless ( $c->components->{$command} ) {
117 qq/Couldn't forward to command "$command". Invalid action or component./;
119 $c->log->debug($error) if $c->debug;
123 my $class = $command;
124 my $method = shift || 'process';
126 if ( my $code = $c->components->{$class}->can($method) ) {
127 my $action = Catalyst::Action->new(
131 reverse => "$class->$method",
141 qq/Couldn't forward to "$class". Does not implement "$method"/;
143 $c->log->debug($error)
150 local $c->request->{arguments} = [ @{$arguments} ];
151 local $c->{namespace} = $result->namespace;
153 $result->execute($c);
158 =item $self->prepare_action($c)
163 my ( $self, $c ) = @_;
164 my $path = $c->req->path;
165 my @path = split /\//, $c->req->path;
166 $c->req->args( \my @args );
168 push( @path, '/' ) unless @path; # Root action
170 DESCEND: while (@path) {
171 $path = join '/', @path;
173 $path = '' if $path eq '/'; # Root action
175 # Check out dispatch types to see if any will handle the path at
178 foreach my $type ( @{ $self->dispatch_types } ) {
179 last DESCEND if $type->match( $c, $path );
182 # If not, move the last part path to args
184 unshift @args, pop @path;
187 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
188 if ( $c->debug && @args );
191 =item $self->get_action( $c, $action, $namespace )
196 my ( $self, $c, $name, $namespace ) = @_;
199 $namespace = '' if $namespace eq '/';
201 my @match = $self->get_containers($namespace);
203 return unless @match;
205 if ( my $action = $match[-1]->get_action( $c, $name ) )
207 return $action if $action->namespace eq $namespace;
211 =item $self->get_actions( $c, $action, $namespace )
216 my ( $self, $c, $action, $namespace ) = @_;
217 return [] unless $action;
219 $namespace = '' if $namespace eq '/';
221 my @match = $self->get_containers($namespace);
223 return map { $_->get_action($c, $action) } @match;
226 =item $self->get_containers( $namespace )
231 my ( $self, $namespace ) = @_;
233 # If the namespace is / just return the root ActionContainer
235 return ( $self->tree->getNodeValue )
236 if ( !$namespace || ( $namespace eq '/' ) );
238 # Use a visitor to recurse down the tree finding the ActionContainers
239 # for each namespace in the chain.
241 my $visitor = Tree::Simple::Visitor::FindByPath->new;
242 my @path = split( '/', $namespace );
243 $visitor->setSearchPath(@path);
244 $self->tree->accept($visitor);
246 my @match = $visitor->getResults;
247 @match = ( $self->tree ) unless @match;
249 if ( !defined $visitor->getResult ) {
251 # If we don't manage to match, the visitor doesn't return the last
252 # node is matched, so foo/bar/baz would only find the 'foo' node,
253 # not the foo and foo/bar nodes as it should. This does another
254 # single-level search to see if that's the case, and the 'last unless'
255 # should catch any failures - or short-circuit this if this *is* a
256 # bug in the visitor and gets fixed.
258 my $extra = $path[ ( scalar @match ) - 1 ];
260 $visitor->setSearchPath($extra);
261 $match[-1]->accept($visitor);
262 push( @match, $visitor->getResult ) if defined $visitor->getResult;
265 return map { $_->getNodeValue } @match;
269 my ( $self, $c, $action ) = @_;
271 my $namespace = $action->namespace;
272 my $parent = $self->tree;
273 my $visitor = Tree::Simple::Visitor::FindByPath->new;
276 for my $part ( split '/', $namespace ) {
277 $visitor->setSearchPath($part);
278 $parent->accept($visitor);
279 my $child = $visitor->getResult;
283 # Create a new tree node and an ActionContainer to form
287 Catalyst::ActionContainer->new(
288 { part => $part, actions => {} } );
289 $child = $parent->addChild( Tree::Simple->new($container) );
290 $visitor->setSearchPath($part);
291 $parent->accept($visitor);
292 $child = $visitor->getResult;
299 # Set the method value
300 $parent->getNodeValue->actions->{$action->name} = $action;
302 my $registered = $self->registered_dispatch_types;
304 foreach my $key (keys %{$action->attributes}) {
305 my $class = "Catalyst::DispatchType::$key";
306 unless ( $registered->{$class} ) {
307 eval "require $class";
308 push( @{ $self->dispatch_types }, $class->new ) unless $@;
309 $registered->{$class} = 1;
313 # Pass the action to our dispatch types so they can register it if reqd.
314 foreach my $type ( @{ $self->dispatch_types } ) {
315 $type->register( $c, $action );
319 =item $self->setup_actions( $class, $component )
324 my ( $self, $c ) = @_;
326 $self->dispatch_types( [] );
327 $self->registered_dispatch_types( {} );
329 # Preload action types
330 for my $type (@PRELOAD) {
331 my $class = "Catalyst::DispatchType::$type";
332 eval "require $class";
333 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
335 push @{ $self->dispatch_types }, $class->new;
336 $self->registered_dispatch_types->{$class} = 1;
341 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
342 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
344 $c->register_actions( $c );
346 foreach my $comp ( values %{$c->components} ) {
347 $comp->register_actions( $c ) if $comp->can('register_actions');
350 # Postload action types
351 for my $type (@POSTLOAD) {
352 my $class = "Catalyst::DispatchType::$type";
353 eval "require $class";
354 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
356 push @{ $self->dispatch_types }, $class->new;
359 return unless $c->debug;
361 my $privates = Text::ASCIITable->new;
362 $privates->setCols( 'Private', 'Class' );
363 $privates->setColWidth( 'Private', 36, 1 );
364 $privates->setColWidth( 'Class', 37, 1 );
367 my ( $walker, $parent, $prefix ) = @_;
368 $prefix .= $parent->getNodeValue || '';
369 $prefix .= '/' unless $prefix =~ /\/$/;
370 my $node = $parent->getNodeValue->actions;
372 for my $action ( keys %{$node} ) {
373 my $action_obj = $node->{$action};
375 if ( ( $action =~ /^_.*/ )
376 && ( !$c->config->{show_internal_actions} ) );
377 $privates->addRow( "$prefix$action", $action_obj->class );
380 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
383 $walker->( $walker, $self->tree, '' );
384 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
385 if ( @{ $privates->{tbl_rows} } );
387 # List all public actions
388 $_->list($c) for @{ $self->dispatch_types };
395 Sebastian Riedel, C<sri@cpan.org>
399 This program is free software, you can redistribute it and/or modify it under
400 the same terms as Perl itself.