Fixed public accessible internal actions
[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
7101bd1f 20 method_action_class action_container_class reserved_actions/
49070d25 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
7101bd1f 29# Reserved action names
30our @RESERVED = qw/begin auto default index end/;
31
1abd6db7 32=head1 NAME
33
9c053379 34Catalyst::Dispatcher - The Catalyst Dispatcher
1abd6db7 35
36=head1 SYNOPSIS
37
38See L<Catalyst>.
39
40=head1 DESCRIPTION
41
42=head1 METHODS
43
44=over 4
45
fbcc39ad 46=item $self->detach( $c, $command [, \@arguments ] )
6ef62eb2 47
48=cut
49
50sub detach {
fbcc39ad 51 my ( $self, $c, $command, @args ) = @_;
bd7d2e94 52 $c->forward( $command, @args ) if $command;
fbcc39ad 53 die $Catalyst::DETACH;
6ef62eb2 54}
55
fbcc39ad 56=item $self->dispatch($c)
1abd6db7 57
58=cut
59
60sub dispatch {
fbcc39ad 61 my ( $self, $c ) = @_;
cfd04b0c 62
66e28e3f 63 if ( $c->action ) {
28591cd7 64 $c->forward( join( '/', '', $c->action->namespace, '_DISPATCH' ) );
fbcc39ad 65 }
66
67 else {
1abd6db7 68 my $path = $c->req->path;
69 my $error = $path
70 ? qq/Unknown resource "$path"/
71 : "No default action defined";
72 $c->log->error($error) if $c->debug;
73 $c->error($error);
74 }
75}
76
fbcc39ad 77=item $self->forward( $c, $command [, \@arguments ] )
1abd6db7 78
79=cut
80
81sub forward {
fbcc39ad 82 my $self = shift;
1abd6db7 83 my $c = shift;
84 my $command = shift;
99fe1710 85
1abd6db7 86 unless ($command) {
87 $c->log->debug('Nothing to forward to') if $c->debug;
88 return 0;
89 }
99fe1710 90
138ce4c0 91 my $local_args = 0;
6d12f1d4 92 my $arguments = $c->req->args;
138ce4c0 93 if ( ref( $_[-1] ) eq 'ARRAY' ) {
6d12f1d4 94 $arguments = pop(@_);
95 $local_args = 1;
138ce4c0 96 }
99fe1710 97
a9dc674c 98 my $result;
fbcc39ad 99
16aa17e9 100 unless ( ref $command ) {
101 my $command_copy = $command;
8199eac3 102
16aa17e9 103 unless ( $command_copy =~ s/^\/// ) {
46245bee 104 my $namespace = $c->stack->[-1]->namespace;
105 $command_copy = "${namespace}/${command}";
16aa17e9 106 }
99fe1710 107
16aa17e9 108 unless ( $command_copy =~ /\// ) {
109 $result = $c->get_action( $command_copy, '/' );
110 }
111 else {
112 my @extra_args;
113 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
114 my $tail = $2;
115 $result = $c->get_action( $tail, $1 );
116 if ($result) {
6d12f1d4 117 $local_args = 1;
118 $command = $tail;
d3f21b2f 119 unshift( @{$arguments}, @extra_args );
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
6d12f1d4 166 if ($local_args) {
167 local $c->request->{arguments} = [ @{$arguments} ];
168 $result->execute($c);
169 }
170 else { $result->execute($c) }
99fe1710 171
1abd6db7 172 return $c->state;
173}
174
fbcc39ad 175=item $self->prepare_action($c)
176
177=cut
178
179sub prepare_action {
180 my ( $self, $c ) = @_;
181 my $path = $c->req->path;
182 my @path = split /\//, $c->req->path;
183 $c->req->args( \my @args );
184
78d760bb 185 push( @path, '/' ) unless @path; # Root action
186
b96f127f 187 DESCEND: while (@path) {
fbcc39ad 188 $path = join '/', @path;
fbcc39ad 189
78d760bb 190 $path = '' if $path eq '/'; # Root action
191
22f3a8dd 192 # Check out dispatch types to see if any will handle the path at
193 # this level
194
78d760bb 195 foreach my $type ( @{ $self->dispatch_types } ) {
2633d7dc 196 last DESCEND if $type->match( $c, $path );
66e28e3f 197 }
b96f127f 198
22f3a8dd 199 # If not, move the last part path to args
b96f127f 200 unshift @args, pop @path;
fbcc39ad 201 }
202
e3a13771 203 $c->log->debug( 'Path is "' . $c->req->match . '"' )
204 if ( $c->debug && $c->req->match );
205
fbcc39ad 206 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
207 if ( $c->debug && @args );
208}
209
bcd1002b 210=item $self->get_action( $action, $namespace )
1abd6db7 211
212=cut
213
214sub get_action {
bcd1002b 215 my ( $self, $name, $namespace ) = @_;
79a3189a 216 return unless $name;
bcccee4e 217 $namespace ||= '';
772ab8ae 218 $namespace = '' if $namespace eq '/';
99fe1710 219
78d760bb 220 my @match = $self->get_containers($namespace);
c8d9780f 221
79a3189a 222 return unless @match;
772ab8ae 223
684d10ed 224 if ( my $action = $match[-1]->get_action($name) ) {
79a3189a 225 return $action if $action->namespace eq $namespace;
1abd6db7 226 }
1abd6db7 227}
228
a9dc674c 229=item $self->get_actions( $c, $action, $namespace )
230
231=cut
232
233sub get_actions {
234 my ( $self, $c, $action, $namespace ) = @_;
235 return [] unless $action;
236 $namespace ||= '';
237 $namespace = '' if $namespace eq '/';
238
239 my @match = $self->get_containers($namespace);
240
684d10ed 241 return map { $_->get_action($action) } @match;
a9dc674c 242}
243
cfd04b0c 244=item $self->get_containers( $namespace )
245
246=cut
247
248sub get_containers {
249 my ( $self, $namespace ) = @_;
250
90ce41ba 251 # If the namespace is / just return the root ActionContainer
252
78d760bb 253 return ( $self->tree->getNodeValue )
254 if ( !$namespace || ( $namespace eq '/' ) );
cfd04b0c 255
90ce41ba 256 # Use a visitor to recurse down the tree finding the ActionContainers
257 # for each namespace in the chain.
258
cfd04b0c 259 my $visitor = Tree::Simple::Visitor::FindByPath->new;
78d760bb 260 my @path = split( '/', $namespace );
261 $visitor->setSearchPath(@path);
cfd04b0c 262 $self->tree->accept($visitor);
263
264 my @match = $visitor->getResults;
78d760bb 265 @match = ( $self->tree ) unless @match;
cfd04b0c 266
78d760bb 267 if ( !defined $visitor->getResult ) {
90ce41ba 268
269 # If we don't manage to match, the visitor doesn't return the last
270 # node is matched, so foo/bar/baz would only find the 'foo' node,
271 # not the foo and foo/bar nodes as it should. This does another
272 # single-level search to see if that's the case, and the 'last unless'
273 # should catch any failures - or short-circuit this if this *is* a
274 # bug in the visitor and gets fixed.
275
f3b3f450 276 if ( my $extra = $path[ ( scalar @match ) - 1 ] ) {
540966c1 277 $visitor->setSearchPath($extra);
278 $match[-1]->accept($visitor);
279 push( @match, $visitor->getResult ) if defined $visitor->getResult;
280 }
cfd04b0c 281 }
282
283 return map { $_->getNodeValue } @match;
284}
285
aad72cc9 286=item $self->register( $c, $action )
287
288=cut
289
79a3189a 290sub register {
291 my ( $self, $c, $action ) = @_;
292
694d15f1 293 my $registered = $self->registered_dispatch_types;
294
295 my $priv = 0;
296 foreach my $key ( keys %{ $action->attributes } ) {
297 $priv++ if $key eq 'Private';
298 my $class = "Catalyst::DispatchType::$key";
299 unless ( $registered->{$class} ) {
300 eval "require $class";
301 push( @{ $self->dispatch_types }, $class->new ) unless $@;
302 $registered->{$class} = 1;
303 }
304 }
305
7101bd1f 306 # Check if action name is reserved
307 my $reserved = 0;
308 for my $name ( @{ $self->reserved_actions } ) {
309 $reserved++ if $action->name eq $name;
310 }
311
694d15f1 312 # Pass the action to our dispatch types so they can register it if reqd.
7101bd1f 313 my $reg = $reserved;
314 unless ($reserved) {
315 foreach my $type ( @{ $self->dispatch_types } ) {
316 $reg++ if $type->register( $c, $action );
317 }
694d15f1 318 }
319
320 return unless $reg + $priv;
321
79a3189a 322 my $namespace = $action->namespace;
323 my $parent = $self->tree;
324 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 325
11bd4e3e 326 if ($namespace) {
327 for my $part ( split '/', $namespace ) {
1abd6db7 328 $visitor->setSearchPath($part);
329 $parent->accept($visitor);
b7aebc12 330 my $child = $visitor->getResult;
78d760bb 331
b7aebc12 332 unless ($child) {
90ce41ba 333
334 # Create a new tree node and an ActionContainer to form
335 # its value.
336
78d760bb 337 my $container =
338 Catalyst::ActionContainer->new(
339 { part => $part, actions => {} } );
b7aebc12 340 $child = $parent->addChild( Tree::Simple->new($container) );
341 $visitor->setSearchPath($part);
342 $parent->accept($visitor);
343 $child = $visitor->getResult;
344 }
78d760bb 345
b7aebc12 346 $parent = $child;
1abd6db7 347 }
1abd6db7 348 }
99fe1710 349
90ce41ba 350 # Set the method value
49070d25 351 $parent->getNodeValue->actions->{ $action->name } = $action;
1abd6db7 352}
353
fbcc39ad 354=item $self->setup_actions( $class, $component )
1abd6db7 355
356=cut
357
358sub setup_actions {
11bd4e3e 359 my ( $self, $c ) = @_;
99fe1710 360
6d030e6f 361 $self->dispatch_types( [] );
91d4abc5 362 $self->registered_dispatch_types( {} );
7101bd1f 363 $self->reserved_actions( \@RESERVED );
49070d25 364 $self->method_action_class('Catalyst::Action');
365 $self->action_container_class('Catalyst::ActionContainer');
12e28165 366
6d030e6f 367 # Preload action types
368 for my $type (@PRELOAD) {
369 my $class = "Catalyst::DispatchType::$type";
370 eval "require $class";
371 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
372 if $@;
373 push @{ $self->dispatch_types }, $class->new;
91d4abc5 374 $self->registered_dispatch_types->{$class} = 1;
6d030e6f 375 }
b96f127f 376
12e28165 377 # We use a tree
78d760bb 378 my $container =
379 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
b7aebc12 380 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
e494bd6b 381
49070d25 382 foreach my $comp ( values %{ $c->components } ) {
383 $comp->register_actions($c) if $comp->can('register_actions');
1abd6db7 384 }
e494bd6b 385
2d1d8f91 386 # Postload action types
387 for my $type (@POSTLOAD) {
388 my $class = "Catalyst::DispatchType::$type";
389 eval "require $class";
390 Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
391 if $@;
392 push @{ $self->dispatch_types }, $class->new;
393 }
6d030e6f 394
11bd4e3e 395 return unless $c->debug;
99fe1710 396
684d10ed 397 my $privates = Text::SimpleTable->new(
dbf03873 398 [ 20, 'Private' ],
399 [ 38, 'Class' ],
400 [ 12, 'Method' ]
684d10ed 401 );
99fe1710 402
87b85407 403 my $has_private = 0;
1abd6db7 404 my $walker = sub {
405 my ( $walker, $parent, $prefix ) = @_;
406 $prefix .= $parent->getNodeValue || '';
407 $prefix .= '/' unless $prefix =~ /\/$/;
b7aebc12 408 my $node = $parent->getNodeValue->actions;
99fe1710 409
78d760bb 410 for my $action ( keys %{$node} ) {
b7aebc12 411 my $action_obj = $node->{$action};
b0bb11ec 412 next
413 if ( ( $action =~ /^_.*/ )
414 && ( !$c->config->{show_internal_actions} ) );
684d10ed 415 $privates->row( "$prefix$action", $action_obj->class, $action );
87b85407 416 $has_private = 1;
1abd6db7 417 }
99fe1710 418
1abd6db7 419 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
420 };
99fe1710 421
1abd6db7 422 $walker->( $walker, $self->tree, '' );
11bd4e3e 423 $c->log->debug( "Loaded Private actions:\n" . $privates->draw )
49070d25 424 if ($has_private);
99fe1710 425
a9cbd748 426 # List all public actions
11bd4e3e 427 $_->list($c) for @{ $self->dispatch_types };
1abd6db7 428}
429
1abd6db7 430=back
431
432=head1 AUTHOR
433
434Sebastian Riedel, C<sri@cpan.org>
158c88c0 435Matt S Trout, C<mst@shadowcatsystems.co.uk>
1abd6db7 436
437=head1 COPYRIGHT
438
439This program is free software, you can redistribute it and/or modify it under
440the same terms as Perl itself.
441
442=cut
443
4441;