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