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