Fixed forward
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Dispatcher.pm
CommitLineData
68a748b9 1package Catalyst::Dispatcher;
1abd6db7 2
3use strict;
fbcc39ad 4use base 'Class::Accessor::Fast';
a2f2cde9 5use Catalyst::Exception;
f05af9ba 6use Catalyst::Utils;
fbcc39ad 7use Catalyst::Action;
b7aebc12 8use Catalyst::ActionContainer;
b96f127f 9use Catalyst::DispatchType::Default;
bcccee4e 10use Catalyst::DispatchType::Index;
87b85407 11use Text::SimpleTable;
1abd6db7 12use Tree::Simple;
13use Tree::Simple::Visitor::FindByPath;
14
fbcc39ad 15# Stringify to class
16use overload '""' => sub { return ref shift }, fallback => 1;
17
49070d25 18__PACKAGE__->mk_accessors(
19 qw/tree dispatch_types registered_dispatch_types
20 method_action_class action_container_class/
21);
6d030e6f 22
23# Preload these action types
24our @PRELOAD = qw/Path Regex/;
1abd6db7 25
2d1d8f91 26# Postload these action types
27our @POSTLOAD = qw/Index Default/;
28
1abd6db7 29=head1 NAME
30
9c053379 31Catalyst::Dispatcher - The Catalyst Dispatcher
1abd6db7 32
33=head1 SYNOPSIS
34
35See L<Catalyst>.
36
37=head1 DESCRIPTION
38
39=head1 METHODS
40
41=over 4
42
fbcc39ad 43=item $self->detach( $c, $command [, \@arguments ] )
6ef62eb2 44
45=cut
46
47sub detach {
fbcc39ad 48 my ( $self, $c, $command, @args ) = @_;
bd7d2e94 49 $c->forward( $command, @args ) if $command;
fbcc39ad 50 die $Catalyst::DETACH;
6ef62eb2 51}
52
fbcc39ad 53=item $self->dispatch($c)
1abd6db7 54
55=cut
56
57sub dispatch {
fbcc39ad 58 my ( $self, $c ) = @_;
cfd04b0c 59
66e28e3f 60 if ( $c->action ) {
b0bb11ec 61 $c->forward( join( '/', '', $c->namespace, '_DISPATCH' ) );
fbcc39ad 62 }
63
64 else {
1abd6db7 65 my $path = $c->req->path;
66 my $error = $path
67 ? qq/Unknown resource "$path"/
68 : "No default action defined";
69 $c->log->error($error) if $c->debug;
70 $c->error($error);
71 }
72}
73
fbcc39ad 74=item $self->forward( $c, $command [, \@arguments ] )
1abd6db7 75
76=cut
77
78sub forward {
fbcc39ad 79 my $self = shift;
1abd6db7 80 my $c = shift;
81 my $command = shift;
99fe1710 82
49070d25 83 $command = ref $command if ref $command;
84
1abd6db7 85 unless ($command) {
86 $c->log->debug('Nothing to forward to') if $c->debug;
87 return 0;
88 }
99fe1710 89
e0fc6749 90 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
99fe1710 91
a9dc674c 92 my $result;
fbcc39ad 93
8199eac3 94 my $command_copy = $command;
95
96 unless ( $command_copy =~ s/^\/// ) {
91d4abc5 97 my $namespace = $c->namespace;
5d68b17b 98 $command_copy = "${namespace}/${command}";
1abd6db7 99 }
99fe1710 100
8199eac3 101 unless ( $command_copy =~ /\// ) {
a9dc674c 102 $result = $c->get_action( $command_copy, '/' );
8199eac3 103 }
e494bd6b 104 else {
8199eac3 105 my @extra_args;
106 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
107 my $tail = $2;
a9dc674c 108 $result = $c->get_action( $tail, $1 );
49070d25 109 if ($result) {
8199eac3 110 $command = $tail;
111 push( @{$arguments}, @extra_args );
112 last DESCEND;
113 }
114 unshift( @extra_args, $tail );
115 }
e494bd6b 116 }
99fe1710 117
49070d25 118 unless ($result) {
bd7d2e94 119
d6e0d7e6 120 my $comp;
121
122 unless ( $comp = $c->component($command) ) {
bd7d2e94 123 my $error =
124qq/Couldn't forward to command "$command". Invalid action or component./;
3b2ed580 125 $c->error($error);
126 $c->log->debug($error) if $c->debug;
1abd6db7 127 return 0;
128 }
bd7d2e94 129
d6e0d7e6 130 my $class = ref $comp;
1abd6db7 131 my $method = shift || 'process';
99fe1710 132
d6e0d7e6 133 if ( my $code = $class->can($method) ) {
97d6d2bd 134 my $action = $self->method_action_class->new(
fbcc39ad 135 {
6b239949 136 name => $method,
fbcc39ad 137 code => $code,
138 reverse => "$class->$method",
11bd4e3e 139 class => $class,
fbcc39ad 140 namespace => $class,
141 }
142 );
a9dc674c 143 $result = $action;
fbcc39ad 144 }
145
146 else {
bd7d2e94 147 my $error =
148 qq/Couldn't forward to "$class". Does not implement "$method"/;
3b2ed580 149 $c->error($error);
150 $c->log->debug($error)
1abd6db7 151 if $c->debug;
152 return 0;
153 }
99fe1710 154
1abd6db7 155 }
bd7d2e94 156
157 local $c->request->{arguments} = [ @{$arguments} ];
99fe1710 158
a9dc674c 159 $result->execute($c);
99fe1710 160
1abd6db7 161 return $c->state;
162}
163
fbcc39ad 164=item $self->prepare_action($c)
165
166=cut
167
168sub prepare_action {
169 my ( $self, $c ) = @_;
170 my $path = $c->req->path;
171 my @path = split /\//, $c->req->path;
172 $c->req->args( \my @args );
173
78d760bb 174 push( @path, '/' ) unless @path; # Root action
175
b96f127f 176 DESCEND: while (@path) {
fbcc39ad 177 $path = join '/', @path;
fbcc39ad 178
78d760bb 179 $path = '' if $path eq '/'; # Root action
180
22f3a8dd 181 # Check out dispatch types to see if any will handle the path at
182 # this level
183
78d760bb 184 foreach my $type ( @{ $self->dispatch_types } ) {
2633d7dc 185 last DESCEND if $type->match( $c, $path );
66e28e3f 186 }
b96f127f 187
22f3a8dd 188 # If not, move the last part path to args
189
b96f127f 190 unshift @args, pop @path;
fbcc39ad 191 }
192
193 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
194 if ( $c->debug && @args );
195}
196
a9dc674c 197=item $self->get_action( $c, $action, $namespace )
1abd6db7 198
199=cut
200
201sub get_action {
79a3189a 202 my ( $self, $c, $name, $namespace ) = @_;
203 return unless $name;
bcccee4e 204 $namespace ||= '';
772ab8ae 205 $namespace = '' if $namespace eq '/';
99fe1710 206
78d760bb 207 my @match = $self->get_containers($namespace);
c8d9780f 208
79a3189a 209 return unless @match;
772ab8ae 210
49070d25 211 if ( my $action = $match[-1]->get_action( $c, $name ) ) {
79a3189a 212 return $action if $action->namespace eq $namespace;
1abd6db7 213 }
1abd6db7 214}
215
a9dc674c 216=item $self->get_actions( $c, $action, $namespace )
217
218=cut
219
220sub get_actions {
221 my ( $self, $c, $action, $namespace ) = @_;
222 return [] unless $action;
223 $namespace ||= '';
224 $namespace = '' if $namespace eq '/';
225
226 my @match = $self->get_containers($namespace);
227
49070d25 228 return map { $_->get_action( $c, $action ) } @match;
a9dc674c 229}
230
cfd04b0c 231=item $self->get_containers( $namespace )
232
233=cut
234
235sub get_containers {
236 my ( $self, $namespace ) = @_;
237
90ce41ba 238 # If the namespace is / just return the root ActionContainer
239
78d760bb 240 return ( $self->tree->getNodeValue )
241 if ( !$namespace || ( $namespace eq '/' ) );
cfd04b0c 242
90ce41ba 243 # Use a visitor to recurse down the tree finding the ActionContainers
244 # for each namespace in the chain.
245
cfd04b0c 246 my $visitor = Tree::Simple::Visitor::FindByPath->new;
78d760bb 247 my @path = split( '/', $namespace );
248 $visitor->setSearchPath(@path);
cfd04b0c 249 $self->tree->accept($visitor);
250
251 my @match = $visitor->getResults;
78d760bb 252 @match = ( $self->tree ) unless @match;
cfd04b0c 253
78d760bb 254 if ( !defined $visitor->getResult ) {
90ce41ba 255
256 # If we don't manage to match, the visitor doesn't return the last
257 # node is matched, so foo/bar/baz would only find the 'foo' node,
258 # not the foo and foo/bar nodes as it should. This does another
259 # single-level search to see if that's the case, and the 'last unless'
260 # should catch any failures - or short-circuit this if this *is* a
261 # bug in the visitor and gets fixed.
262
78d760bb 263 my $extra = $path[ ( scalar @match ) - 1 ];
cfd04b0c 264 last unless $extra;
265 $visitor->setSearchPath($extra);
266 $match[-1]->accept($visitor);
78d760bb 267 push( @match, $visitor->getResult ) if defined $visitor->getResult;
cfd04b0c 268 }
269
270 return map { $_->getNodeValue } @match;
271}
272
79a3189a 273sub register {
274 my ( $self, $c, $action ) = @_;
275
276 my $namespace = $action->namespace;
277 my $parent = $self->tree;
278 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 279
11bd4e3e 280 if ($namespace) {
281 for my $part ( split '/', $namespace ) {
1abd6db7 282 $visitor->setSearchPath($part);
283 $parent->accept($visitor);
b7aebc12 284 my $child = $visitor->getResult;
78d760bb 285
b7aebc12 286 unless ($child) {
90ce41ba 287
288 # Create a new tree node and an ActionContainer to form
289 # its value.
290
78d760bb 291 my $container =
292 Catalyst::ActionContainer->new(
293 { part => $part, actions => {} } );
b7aebc12 294 $child = $parent->addChild( Tree::Simple->new($container) );
295 $visitor->setSearchPath($part);
296 $parent->accept($visitor);
297 $child = $visitor->getResult;
298 }
78d760bb 299
b7aebc12 300 $parent = $child;
1abd6db7 301 }
1abd6db7 302 }
99fe1710 303
90ce41ba 304 # Set the method value
49070d25 305 $parent->getNodeValue->actions->{ $action->name } = $action;
fbcc39ad 306
91d4abc5 307 my $registered = $self->registered_dispatch_types;
308
49070d25 309 foreach my $key ( keys %{ $action->attributes } ) {
310 my $class = "Catalyst::DispatchType::$key";
311 unless ( $registered->{$class} ) {
312 eval "require $class";
313 push( @{ $self->dispatch_types }, $class->new ) unless $@;
314 $registered->{$class} = 1;
315 }
91d4abc5 316 }
317
22f3a8dd 318 # Pass the action to our dispatch types so they can register it if reqd.
b96f127f 319 foreach my $type ( @{ $self->dispatch_types } ) {
2633d7dc 320 $type->register( $c, $action );
b96f127f 321 }
1abd6db7 322}
323
fbcc39ad 324=item $self->setup_actions( $class, $component )
1abd6db7 325
326=cut
327
328sub setup_actions {
11bd4e3e 329 my ( $self, $c ) = @_;
99fe1710 330
6d030e6f 331 $self->dispatch_types( [] );
91d4abc5 332 $self->registered_dispatch_types( {} );
49070d25 333 $self->method_action_class('Catalyst::Action');
334 $self->action_container_class('Catalyst::ActionContainer');
12e28165 335
6d030e6f 336 # Preload action types
337 for my $type (@PRELOAD) {
338 my $class = "Catalyst::DispatchType::$type";
339 eval "require $class";
340 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
341 if $@;
342 push @{ $self->dispatch_types }, $class->new;
91d4abc5 343 $self->registered_dispatch_types->{$class} = 1;
6d030e6f 344 }
b96f127f 345
12e28165 346 # We use a tree
78d760bb 347 my $container =
348 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
b7aebc12 349 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
e494bd6b 350
49070d25 351 $c->register_actions($c);
99fe1710 352
49070d25 353 foreach my $comp ( values %{ $c->components } ) {
354 $comp->register_actions($c) if $comp->can('register_actions');
1abd6db7 355 }
e494bd6b 356
2d1d8f91 357 # Postload action types
358 for my $type (@POSTLOAD) {
359 my $class = "Catalyst::DispatchType::$type";
360 eval "require $class";
361 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
362 if $@;
363 push @{ $self->dispatch_types }, $class->new;
364 }
6d030e6f 365
11bd4e3e 366 return unless $c->debug;
99fe1710 367
49070d25 368 my $privates = Text::SimpleTable->new( [ 36, 'Private' ], [ 37, 'Class' ] );
99fe1710 369
87b85407 370 my $has_private = 0;
1abd6db7 371 my $walker = sub {
372 my ( $walker, $parent, $prefix ) = @_;
373 $prefix .= $parent->getNodeValue || '';
374 $prefix .= '/' unless $prefix =~ /\/$/;
b7aebc12 375 my $node = $parent->getNodeValue->actions;
99fe1710 376
78d760bb 377 for my $action ( keys %{$node} ) {
b7aebc12 378 my $action_obj = $node->{$action};
b0bb11ec 379 next
380 if ( ( $action =~ /^_.*/ )
381 && ( !$c->config->{show_internal_actions} ) );
87b85407 382 $privates->row( "$prefix$action", $action_obj->class );
383 $has_private = 1;
1abd6db7 384 }
99fe1710 385
1abd6db7 386 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
387 };
99fe1710 388
1abd6db7 389 $walker->( $walker, $self->tree, '' );
11bd4e3e 390 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
49070d25 391 if ($has_private);
99fe1710 392
a9cbd748 393 # List all public actions
11bd4e3e 394 $_->list($c) for @{ $self->dispatch_types };
1abd6db7 395}
396
1abd6db7 397=back
398
399=head1 AUTHOR
400
401Sebastian Riedel, C<sri@cpan.org>
158c88c0 402Matt S Trout, C<mst@shadowcatsystems.co.uk>
1abd6db7 403
404=head1 COPYRIGHT
405
406This program is free software, you can redistribute it and/or modify it under
407the same terms as Perl itself.
408
409=cut
410
4111;