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