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