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