Fixed public action lists
[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;
6b239949 9use Catalyst::DispatchType::Path;
b96f127f 10use Catalyst::DispatchType::Regex;
11use Catalyst::DispatchType::Default;
1abd6db7 12use Text::ASCIITable;
1abd6db7 13use Tree::Simple;
14use Tree::Simple::Visitor::FindByPath;
15
fbcc39ad 16# Stringify to class
17use overload '""' => sub { return ref shift }, fallback => 1;
18
b96f127f 19__PACKAGE__->mk_accessors(qw/actions tree dispatch_types/);
1abd6db7 20
1abd6db7 21=head1 NAME
22
9c053379 23Catalyst::Dispatcher - The Catalyst Dispatcher
1abd6db7 24
25=head1 SYNOPSIS
26
27See L<Catalyst>.
28
29=head1 DESCRIPTION
30
31=head1 METHODS
32
33=over 4
34
fbcc39ad 35=item $self->detach( $c, $command [, \@arguments ] )
6ef62eb2 36
37=cut
38
39sub detach {
fbcc39ad 40 my ( $self, $c, $command, @args ) = @_;
bd7d2e94 41 $c->forward( $command, @args ) if $command;
fbcc39ad 42 die $Catalyst::DETACH;
6ef62eb2 43}
44
fbcc39ad 45=item $self->dispatch($c)
1abd6db7 46
47=cut
48
49sub dispatch {
fbcc39ad 50 my ( $self, $c ) = @_;
cfd04b0c 51
66e28e3f 52 if ( $c->action ) {
53
54 my @containers = $self->get_containers( $c->namespace );
55 my %actions;
56 foreach my $name (qw/begin auto end/) {
90ce41ba 57
58 # Go down the container list representing each part of the
59 # current namespace inheritance tree, grabbing the actions hash
60 # of the ActionContainer object and looking for actions of the
61 # appropriate name registered to the namespace
62
66e28e3f 63 $actions{$name} = [
78d760bb 64 map { $_->{$name} }
65 grep { exists $_->{$name} }
66 map { $_->actions } @containers
66e28e3f 67 ];
68 }
1abd6db7 69
fbcc39ad 70 # Errors break the normal flow and the end action is instantly run
71 my $error = 0;
72
1abd6db7 73 # Execute last begin
74 $c->state(1);
66e28e3f 75 if ( my $begin = @{ $actions{begin} }[-1] ) {
cfd04b0c 76 $begin->execute($c);
fbcc39ad 77 $error++ if scalar @{ $c->error };
1abd6db7 78 }
79
80 # Execute the auto chain
1196a46d 81 my $autorun = 0;
cfd04b0c 82 for my $auto ( @{ $actions{auto} } ) {
fbcc39ad 83 last if $error;
9ddd9d05 84 $autorun++;
cfd04b0c 85 $auto->execute($c);
fbcc39ad 86 $error++ if scalar @{ $c->error };
1abd6db7 87 last unless $c->state;
88 }
89
90 # Execute the action or last default
e5f21aa2 91 my $mkay = $autorun ? $c->state ? 1 : 0 : 1;
78d760bb 92 if ($mkay) {
fbcc39ad 93 unless ($error) {
66e28e3f 94 $c->action->execute($c);
95 $error++ if scalar @{ $c->error };
1abd6db7 96 }
97 }
98
99 # Execute last end
cfd04b0c 100 if ( my $end = @{ $actions{end} }[-1] ) {
101 $end->execute($c);
1abd6db7 102 }
fbcc39ad 103 }
104
105 else {
1abd6db7 106 my $path = $c->req->path;
107 my $error = $path
108 ? qq/Unknown resource "$path"/
109 : "No default action defined";
110 $c->log->error($error) if $c->debug;
111 $c->error($error);
112 }
113}
114
fbcc39ad 115=item $self->forward( $c, $command [, \@arguments ] )
1abd6db7 116
117=cut
118
119sub forward {
fbcc39ad 120 my $self = shift;
1abd6db7 121 my $c = shift;
122 my $command = shift;
99fe1710 123
1abd6db7 124 unless ($command) {
125 $c->log->debug('Nothing to forward to') if $c->debug;
126 return 0;
127 }
99fe1710 128
71c3bcc3 129 # Relative forwards from detach
fbcc39ad 130 my $caller = ( caller(1) )[0]->isa('Catalyst::Dispatcher')
131 && ( ( caller(2) )[3] =~ /::detach$/ ) ? caller(3) : caller(1);
71c3bcc3 132
e0fc6749 133 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
99fe1710 134
fbcc39ad 135 my $results = [];
136
8199eac3 137 my $command_copy = $command;
138
139 unless ( $command_copy =~ s/^\/// ) {
5d68b17b 140 my $namespace =
78d760bb 141 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} )
142 || '';
5d68b17b 143 $command_copy = "${namespace}/${command}";
1abd6db7 144 }
99fe1710 145
8199eac3 146 unless ( $command_copy =~ /\// ) {
147 $results = $c->get_action( $command_copy, '/' );
148 }
e494bd6b 149 else {
8199eac3 150 my @extra_args;
151 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
152 my $tail = $2;
153 $results = $c->get_action( $tail, $1 );
154 if ( @{$results} ) {
155 $command = $tail;
156 push( @{$arguments}, @extra_args );
157 last DESCEND;
158 }
159 unshift( @extra_args, $tail );
160 }
e494bd6b 161 }
99fe1710 162
1abd6db7 163 unless ( @{$results} ) {
bd7d2e94 164
fbcc39ad 165 unless ( $c->components->{$command} ) {
bd7d2e94 166 my $error =
167qq/Couldn't forward to command "$command". Invalid action or component./;
3b2ed580 168 $c->error($error);
169 $c->log->debug($error) if $c->debug;
1abd6db7 170 return 0;
171 }
bd7d2e94 172
1f6bb799 173 my $class = $command;
1abd6db7 174 my $method = shift || 'process';
99fe1710 175
1f6bb799 176 if ( my $code = $c->components->{$class}->can($method) ) {
fbcc39ad 177 my $action = Catalyst::Action->new(
178 {
6b239949 179 name => $method,
fbcc39ad 180 code => $code,
181 reverse => "$class->$method",
182 namespace => $class,
cfd04b0c 183 prefix => $class,
fbcc39ad 184 }
185 );
186 $results = [ [$action] ];
187 }
188
189 else {
bd7d2e94 190 my $error =
191 qq/Couldn't forward to "$class". Does not implement "$method"/;
3b2ed580 192 $c->error($error);
193 $c->log->debug($error)
1abd6db7 194 if $c->debug;
195 return 0;
196 }
99fe1710 197
1abd6db7 198 }
bd7d2e94 199
200 local $c->request->{arguments} = [ @{$arguments} ];
99fe1710 201
1abd6db7 202 for my $result ( @{$results} ) {
fbcc39ad 203 $result->[0]->execute($c);
e0fc6749 204 return if scalar @{ $c->error };
1abd6db7 205 last unless $c->state;
206 }
99fe1710 207
1abd6db7 208 return $c->state;
209}
210
fbcc39ad 211=item $self->prepare_action($c)
212
213=cut
214
215sub prepare_action {
216 my ( $self, $c ) = @_;
217 my $path = $c->req->path;
218 my @path = split /\//, $c->req->path;
219 $c->req->args( \my @args );
220
78d760bb 221 push( @path, '/' ) unless @path; # Root action
222
b96f127f 223 DESCEND: while (@path) {
fbcc39ad 224 $path = join '/', @path;
fbcc39ad 225
78d760bb 226 $path = '' if $path eq '/'; # Root action
227
22f3a8dd 228 # Check out dispatch types to see if any will handle the path at
229 # this level
230
78d760bb 231 foreach my $type ( @{ $self->dispatch_types } ) {
2633d7dc 232 last DESCEND if $type->match( $c, $path );
66e28e3f 233 }
b96f127f 234
22f3a8dd 235 # If not, move the last part path to args
236
b96f127f 237 unshift @args, pop @path;
fbcc39ad 238 }
239
240 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
241 if ( $c->debug && @args );
242}
243
244=item $self->get_action( $c, $action, $namespace, $inherit )
1abd6db7 245
246=cut
247
248sub get_action {
fbcc39ad 249 my ( $self, $c, $action, $namespace, $inherit ) = @_;
1abd6db7 250 return [] unless $action;
78d760bb 251 $namespace ||= '/';
2d16b61a 252 $inherit ||= 0;
99fe1710 253
78d760bb 254 my @match = $self->get_containers($namespace);
c8d9780f 255
78d760bb 256 my @results;
99fe1710 257
78d760bb 258 foreach my $child ( $inherit ? @match : $match[-1] ) {
259 my $node = $child->actions;
260 push( @results, [ $node->{$action} ] ) if defined $node->{$action};
1abd6db7 261 }
78d760bb 262 return \@results;
1abd6db7 263}
264
cfd04b0c 265=item $self->get_containers( $namespace )
266
267=cut
268
269sub get_containers {
270 my ( $self, $namespace ) = @_;
271
90ce41ba 272 # If the namespace is / just return the root ActionContainer
273
78d760bb 274 return ( $self->tree->getNodeValue )
275 if ( !$namespace || ( $namespace eq '/' ) );
cfd04b0c 276
90ce41ba 277 # Use a visitor to recurse down the tree finding the ActionContainers
278 # for each namespace in the chain.
279
cfd04b0c 280 my $visitor = Tree::Simple::Visitor::FindByPath->new;
78d760bb 281 my @path = split( '/', $namespace );
282 $visitor->setSearchPath(@path);
cfd04b0c 283 $self->tree->accept($visitor);
284
285 my @match = $visitor->getResults;
78d760bb 286 @match = ( $self->tree ) unless @match;
cfd04b0c 287
78d760bb 288 if ( !defined $visitor->getResult ) {
90ce41ba 289
290 # If we don't manage to match, the visitor doesn't return the last
291 # node is matched, so foo/bar/baz would only find the 'foo' node,
292 # not the foo and foo/bar nodes as it should. This does another
293 # single-level search to see if that's the case, and the 'last unless'
294 # should catch any failures - or short-circuit this if this *is* a
295 # bug in the visitor and gets fixed.
296
78d760bb 297 my $extra = $path[ ( scalar @match ) - 1 ];
cfd04b0c 298 last unless $extra;
299 $visitor->setSearchPath($extra);
300 $match[-1]->accept($visitor);
78d760bb 301 push( @match, $visitor->getResult ) if defined $visitor->getResult;
cfd04b0c 302 }
303
304 return map { $_->getNodeValue } @match;
305}
306
fbcc39ad 307=item $self->set_action( $c, $action, $code, $namespace, $attrs )
1abd6db7 308
309=cut
310
311sub set_action {
fbcc39ad 312 my ( $self, $c, $method, $code, $namespace, $attrs ) = @_;
1abd6db7 313
e494bd6b 314 my $prefix =
315 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
316 || '';
b96f127f 317 my %attributes;
1abd6db7 318
319 for my $attr ( @{$attrs} ) {
22f3a8dd 320
321 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
322
78d760bb 323 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+)\s*\))?$/ ) ) {
b96f127f 324 if ( defined $value ) {
78d760bb 325 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
b96f127f 326 }
78d760bb 327 push( @{ $attributes{$key} }, $value );
b96f127f 328 }
1abd6db7 329 }
330
6b239949 331 if ( $attributes{Private} && ( keys %attributes > 1 ) ) {
8d4e224b 332 $c->log->debug( 'Bad action definition "'
d0e524d2 333 . join( ' ', @{$attrs} )
8d4e224b 334 . qq/" for "$namespace->$method"/ )
335 if $c->debug;
d0e524d2 336 return;
337 }
6b239949 338 return unless keys %attributes;
1abd6db7 339
fbcc39ad 340 my $parent = $self->tree;
1abd6db7 341 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 342
b7aebc12 343 if ($prefix) {
344 for my $part ( split '/', $prefix ) {
1abd6db7 345 $visitor->setSearchPath($part);
346 $parent->accept($visitor);
b7aebc12 347 my $child = $visitor->getResult;
78d760bb 348
b7aebc12 349 unless ($child) {
90ce41ba 350
351 # Create a new tree node and an ActionContainer to form
352 # its value.
353
78d760bb 354 my $container =
355 Catalyst::ActionContainer->new(
356 { part => $part, actions => {} } );
b7aebc12 357 $child = $parent->addChild( Tree::Simple->new($container) );
358 $visitor->setSearchPath($part);
359 $parent->accept($visitor);
360 $child = $visitor->getResult;
361 }
78d760bb 362
b7aebc12 363 $parent = $child;
1abd6db7 364 }
1abd6db7 365 }
99fe1710 366
fbcc39ad 367 my $reverse = $prefix ? "$prefix/$method" : $method;
368
369 my $action = Catalyst::Action->new(
370 {
6b239949 371 name => $method,
b96f127f 372 code => $code,
373 reverse => $reverse,
374 namespace => $namespace,
375 prefix => $prefix,
376 attributes => \%attributes,
fbcc39ad 377 }
378 );
379
90ce41ba 380 # Set the method value
b7aebc12 381 $parent->getNodeValue->actions->{$method} = $action;
fbcc39ad 382
22f3a8dd 383 # Pass the action to our dispatch types so they can register it if reqd.
b96f127f 384 foreach my $type ( @{ $self->dispatch_types } ) {
2633d7dc 385 $type->register( $c, $action );
b96f127f 386 }
1abd6db7 387}
388
fbcc39ad 389=item $self->setup_actions( $class, $component )
1abd6db7 390
391=cut
392
393sub setup_actions {
fbcc39ad 394 my ( $self, $class ) = @_;
99fe1710 395
12e28165 396 # These are the core structures
397 $self->actions(
398 {
399 plain => {},
400 private => {},
401 regex => {},
fbcc39ad 402 compiled => []
12e28165 403 }
404 );
405
78d760bb 406 $self->dispatch_types(
407 [ map { "Catalyst::DispatchType::$_"->new } qw/Path Regex Default/ ] );
b96f127f 408
12e28165 409 # We use a tree
78d760bb 410 my $container =
411 Catalyst::ActionContainer->new( { part => '/', actions => {} } );
b7aebc12 412 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
e494bd6b 413
fbcc39ad 414 for my $comp ( keys %{ $class->components } ) {
e494bd6b 415
416 # We only setup components that inherit from Catalyst::Base
a268a011 417 next unless $comp->isa('Catalyst::Base');
99fe1710 418
812a28c9 419 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
1abd6db7 420 my ( $code, $attrs ) = @{$action};
421 my $name = '';
422 no strict 'refs';
423 my @cache = ( $comp, @{"$comp\::ISA"} );
424 my %namespaces;
99fe1710 425
1abd6db7 426 while ( my $namespace = shift @cache ) {
427 $namespaces{$namespace}++;
428 for my $isa ( @{"$comp\::ISA"} ) {
429 next if $namespaces{$isa};
430 push @cache, $isa;
431 $namespaces{$isa}++;
432 }
433 }
99fe1710 434
1abd6db7 435 for my $namespace ( keys %namespaces ) {
99fe1710 436
1abd6db7 437 for my $sym ( values %{ $namespace . '::' } ) {
99fe1710 438
1abd6db7 439 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
99fe1710 440
1abd6db7 441 $name = *{$sym}{NAME};
fbcc39ad 442 $class->set_action( $name, $code, $comp, $attrs );
1abd6db7 443 last;
444 }
99fe1710 445
1abd6db7 446 }
99fe1710 447
1abd6db7 448 }
99fe1710 449
1abd6db7 450 }
99fe1710 451
1abd6db7 452 }
e494bd6b 453
fbcc39ad 454 return unless $class->debug;
99fe1710 455
1abd6db7 456 my $actions = $self->actions;
457 my $privates = Text::ASCIITable->new;
5fbed090 458 $privates->setCols( 'Private', 'Class' );
459 $privates->setColWidth( 'Private', 36, 1 );
460 $privates->setColWidth( 'Class', 37, 1 );
99fe1710 461
1abd6db7 462 my $walker = sub {
463 my ( $walker, $parent, $prefix ) = @_;
464 $prefix .= $parent->getNodeValue || '';
465 $prefix .= '/' unless $prefix =~ /\/$/;
b7aebc12 466 my $node = $parent->getNodeValue->actions;
99fe1710 467
78d760bb 468 for my $action ( keys %{$node} ) {
b7aebc12 469 my $action_obj = $node->{$action};
fbcc39ad 470 $privates->addRow( "$prefix$action", $action_obj->namespace );
1abd6db7 471 }
99fe1710 472
1abd6db7 473 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
474 };
99fe1710 475
1abd6db7 476 $walker->( $walker, $self->tree, '' );
a9cbd748 477 $class->log->debug( "Loaded Private actions:\n" . $privates->draw )
a268a011 478 if ( @{ $privates->{tbl_rows} } );
99fe1710 479
a9cbd748 480 # List all public actions
481 $_->list($class) for @{ $self->dispatch_types };
1abd6db7 482}
483
1abd6db7 484=back
485
486=head1 AUTHOR
487
488Sebastian Riedel, C<sri@cpan.org>
489
490=head1 COPYRIGHT
491
492This program is free software, you can redistribute it and/or modify it under
493the same terms as Perl itself.
494
495=cut
496
4971;