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