fix objects that evaulate to false. rafl
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Dispatcher.pm
CommitLineData
68a748b9 1package Catalyst::Dispatcher;
1abd6db7 2
3use strict;
4use base 'Class::Data::Inheritable';
a2f2cde9 5use Catalyst::Exception;
f05af9ba 6use Catalyst::Utils;
1abd6db7 7use Text::ASCIITable;
1abd6db7 8use Tree::Simple;
9use Tree::Simple::Visitor::FindByPath;
10
11__PACKAGE__->mk_classdata($_) for qw/actions tree/;
12
1abd6db7 13=head1 NAME
14
9c053379 15Catalyst::Dispatcher - The Catalyst Dispatcher
1abd6db7 16
17=head1 SYNOPSIS
18
19See L<Catalyst>.
20
21=head1 DESCRIPTION
22
23=head1 METHODS
24
25=over 4
26
bd7d2e94 27=item $c->detach( $command [, \@arguments ] )
6ef62eb2 28
29Like C<forward> but doesn't return.
30
31=cut
32
33sub detach {
bd7d2e94 34 my ( $c, $command, @args ) = @_;
35 $c->forward( $command, @args ) if $command;
6ef62eb2 36 die $Catalyst::Engine::DETACH;
37}
38
1abd6db7 39=item $c->dispatch
40
41Dispatch request to actions.
42
43=cut
44
45sub dispatch {
46 my $c = shift;
47 my $action = $c->req->action;
48 my $namespace = '';
49 $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
50 if $action eq 'default';
99fe1710 51
1abd6db7 52 unless ($namespace) {
53 if ( my $result = $c->get_action($action) ) {
e494bd6b 54 $namespace = Catalyst::Utils::class2prefix( $result->[0]->[0]->[0],
55 $c->config->{case_sensitive} );
1abd6db7 56 }
57 }
99fe1710 58
1abd6db7 59 my $default = $action eq 'default' ? $namespace : undef;
2d16b61a 60 my $results = $c->get_action( $action, $default, $default ? 1 : 0 );
1abd6db7 61 $namespace ||= '/';
99fe1710 62
1abd6db7 63 if ( @{$results} ) {
64
65 # Execute last begin
66 $c->state(1);
2d16b61a 67 if ( my $begin = @{ $c->get_action( 'begin', $namespace, 1 ) }[-1] ) {
1abd6db7 68 $c->execute( @{ $begin->[0] } );
69 return if scalar @{ $c->error };
70 }
71
72 # Execute the auto chain
1196a46d 73 my $autorun = 0;
9ddd9d05 74 for my $auto ( @{ $c->get_action( 'auto', $namespace, 1 ) } ) {
75 $autorun++;
1abd6db7 76 $c->execute( @{ $auto->[0] } );
77 return if scalar @{ $c->error };
78 last unless $c->state;
79 }
80
81 # Execute the action or last default
e5f21aa2 82 my $mkay = $autorun ? $c->state ? 1 : 0 : 1;
61cfdd57 83 if ( ( my $action = $c->req->action ) && $mkay ) {
2d16b61a 84 if ( my $result = @{ $c->get_action( $action, $default, 1 ) }[-1] )
85 {
1abd6db7 86 $c->execute( @{ $result->[0] } );
87 }
88 }
89
90 # Execute last end
2d16b61a 91 if ( my $end = @{ $c->get_action( 'end', $namespace, 1 ) }[-1] ) {
1abd6db7 92 $c->execute( @{ $end->[0] } );
93 return if scalar @{ $c->error };
94 }
95 }
99fe1710 96
1abd6db7 97 else {
98 my $path = $c->req->path;
99 my $error = $path
100 ? qq/Unknown resource "$path"/
101 : "No default action defined";
102 $c->log->error($error) if $c->debug;
103 $c->error($error);
104 }
105}
106
e0fc6749 107=item $c->forward( $command [, \@arguments ] )
1abd6db7 108
109Forward processing to a private action or a method from a class.
110If you define a class without method it will default to process().
a1dad9c1 111also takes an optional arrayref containing arguments to be passed
112to the new function. $c->req->args will be reset upon returning
113from the function.
1abd6db7 114
115 $c->forward('/foo');
116 $c->forward('index');
117 $c->forward(qw/MyApp::Model::CDBI::Foo do_stuff/);
118 $c->forward('MyApp::View::TT');
119
120=cut
121
122sub forward {
123 my $c = shift;
124 my $command = shift;
99fe1710 125
1abd6db7 126 unless ($command) {
127 $c->log->debug('Nothing to forward to') if $c->debug;
128 return 0;
129 }
99fe1710 130
71c3bcc3 131 # Relative forwards from detach
132 my $caller = ( caller(0) )[0]->isa('Catalyst::Dispatcher')
133 && ( ( caller(1) )[3] =~ /::detach$/ ) ? caller(1) : caller(0);
134
1abd6db7 135 my $namespace = '/';
e0fc6749 136 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
99fe1710 137
1abd6db7 138 if ( $command =~ /^\// ) {
db0ca23c 139 $command =~ /^\/(.*)\/(\w+)$/;
1abd6db7 140 $namespace = $1 || '/';
db0ca23c 141 $command = $2 || $command;
142 $command =~ s/^\///;
1abd6db7 143 }
99fe1710 144
e494bd6b 145 else {
146 $namespace =
147 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} )
148 || '/';
149 }
99fe1710 150
1abd6db7 151 my $results = $c->get_action( $command, $namespace );
99fe1710 152
1abd6db7 153 unless ( @{$results} ) {
bd7d2e94 154
a1c8f974 155 unless ( defined( $c->components->{$command} ) ) {
bd7d2e94 156 my $error =
157qq/Couldn't forward to command "$command". Invalid action or component./;
3b2ed580 158 $c->error($error);
159 $c->log->debug($error) if $c->debug;
1abd6db7 160 return 0;
161 }
bd7d2e94 162
1f6bb799 163 my $class = $command;
1abd6db7 164 my $method = shift || 'process';
99fe1710 165
1f6bb799 166 if ( my $code = $c->components->{$class}->can($method) ) {
1abd6db7 167 $c->actions->{reverse}->{"$code"} = "$class->$method";
168 $results = [ [ [ $class, $code ] ] ];
169 }
99fe1710 170
1abd6db7 171 else {
bd7d2e94 172 my $error =
173 qq/Couldn't forward to "$class". Does not implement "$method"/;
3b2ed580 174 $c->error($error);
175 $c->log->debug($error)
1abd6db7 176 if $c->debug;
177 return 0;
178 }
99fe1710 179
1abd6db7 180 }
bd7d2e94 181
182 local $c->request->{arguments} = [ @{$arguments} ];
99fe1710 183
1abd6db7 184 for my $result ( @{$results} ) {
185 $c->execute( @{ $result->[0] } );
e0fc6749 186 return if scalar @{ $c->error };
1abd6db7 187 last unless $c->state;
188 }
99fe1710 189
1abd6db7 190 return $c->state;
191}
192
2d16b61a 193=item $c->get_action( $action, $namespace, $inherit )
1abd6db7 194
195Get an action in a given namespace.
196
197=cut
198
199sub get_action {
2d16b61a 200 my ( $c, $action, $namespace, $inherit ) = @_;
1abd6db7 201 return [] unless $action;
202 $namespace ||= '';
2d16b61a 203 $inherit ||= 0;
99fe1710 204
1abd6db7 205 if ($namespace) {
206 $namespace = '' if $namespace eq '/';
207 my $parent = $c->tree;
208 my @results;
99fe1710 209
2d16b61a 210 if ($inherit) {
1abd6db7 211 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
212 push @results, [$result] if $result;
213 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 214
1abd6db7 215 for my $part ( split '/', $namespace ) {
216 $visitor->setSearchPath($part);
217 $parent->accept($visitor);
218 my $child = $visitor->getResult;
219 my $uid = $child->getUID if $child;
220 my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
221 push @results, [$match] if $match;
222 $parent = $child if $child;
223 }
99fe1710 224
1abd6db7 225 }
99fe1710 226
1abd6db7 227 else {
99fe1710 228
1abd6db7 229 if ($namespace) {
230 my $visitor = Tree::Simple::Visitor::FindByPath->new;
231 $visitor->setSearchPath( split '/', $namespace );
232 $parent->accept($visitor);
233 my $child = $visitor->getResult;
234 my $uid = $child->getUID if $child;
235 my $match = $c->actions->{private}->{$uid}->{$action}
236 if $uid;
237 push @results, [$match] if $match;
238 }
99fe1710 239
1abd6db7 240 else {
241 my $result =
242 $c->actions->{private}->{ $parent->getUID }->{$action};
243 push @results, [$result] if $result;
244 }
99fe1710 245
1abd6db7 246 }
247 return \@results;
248 }
99fe1710 249
1abd6db7 250 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
251 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
99fe1710 252
1abd6db7 253 else {
99fe1710 254
1abd6db7 255 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
256 my $name = $c->actions->{compiled}->[$i]->[0];
257 my $regex = $c->actions->{compiled}->[$i]->[1];
99fe1710 258
2d752b2a 259 if ( my @snippets = ( $action =~ $regex ) ) {
1abd6db7 260 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
261 }
99fe1710 262
1abd6db7 263 }
264 }
265 return [];
266}
267
268=item $c->set_action( $action, $code, $namespace, $attrs )
269
270Set an action in a given namespace.
271
272=cut
273
274sub set_action {
275 my ( $c, $method, $code, $namespace, $attrs ) = @_;
276
e494bd6b 277 my $prefix =
278 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
279 || '';
1abd6db7 280 my %flags;
281
282 for my $attr ( @{$attrs} ) {
0299ba22 283 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
284 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
285 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) { $flags{path} = $1 }
286 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
287 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
288 $flags{regex} = $2;
289 }
1abd6db7 290 }
291
d0e524d2 292 if ( $flags{private} && ( keys %flags > 1 ) ) {
8d4e224b 293 $c->log->debug( 'Bad action definition "'
d0e524d2 294 . join( ' ', @{$attrs} )
8d4e224b 295 . qq/" for "$namespace->$method"/ )
296 if $c->debug;
d0e524d2 297 return;
298 }
1abd6db7 299 return unless keys %flags;
300
301 my $parent = $c->tree;
302 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 303
1abd6db7 304 for my $part ( split '/', $prefix ) {
305 $visitor->setSearchPath($part);
306 $parent->accept($visitor);
307 my $child = $visitor->getResult;
99fe1710 308
1abd6db7 309 unless ($child) {
310 $child = $parent->addChild( Tree::Simple->new($part) );
311 $visitor->setSearchPath($part);
312 $parent->accept($visitor);
313 $child = $visitor->getResult;
314 }
99fe1710 315
1abd6db7 316 $parent = $child;
317 }
99fe1710 318
1abd6db7 319 my $uid = $parent->getUID;
320 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
321 my $forward = $prefix ? "$prefix/$method" : $method;
322
323 if ( $flags{path} ) {
324 $flags{path} =~ s/^\w+//;
325 $flags{path} =~ s/\w+$//;
71c3bcc3 326 if ( $flags{path} =~ /^\s*'(.*)'\s*$/ ) { $flags{path} = $1 }
327 if ( $flags{path} =~ /^\s*"(.*)"\s*$/ ) { $flags{path} = $1 }
1abd6db7 328 }
99fe1710 329
1abd6db7 330 if ( $flags{regex} ) {
331 $flags{regex} =~ s/^\w+//;
332 $flags{regex} =~ s/\w+$//;
71c3bcc3 333 if ( $flags{regex} =~ /^\s*'(.*)'\s*$/ ) { $flags{regex} = $1 }
334 if ( $flags{regex} =~ /^\s*"(.*)"\s*$/ ) { $flags{regex} = $1 }
1abd6db7 335 }
336
337 my $reverse = $prefix ? "$prefix/$method" : $method;
338
339 if ( $flags{local} || $flags{global} || $flags{path} ) {
99fe1710 340 my $path = $flags{path} || $method;
1abd6db7 341 my $absolute = 0;
99fe1710 342
1abd6db7 343 if ( $path =~ /^\/(.+)/ ) {
344 $path = $1;
345 $absolute = 1;
346 }
99fe1710 347
1abd6db7 348 $absolute = 1 if $flags{global};
349 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
350 $c->actions->{plain}->{$name} = [ $namespace, $code ];
351 }
99fe1710 352
1abd6db7 353 if ( my $regex = $flags{regex} ) {
354 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
355 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
356 }
357
358 $c->actions->{reverse}->{"$code"} = $reverse;
359}
360
361=item $class->setup_actions($component)
362
363Setup actions for a component.
364
365=cut
366
367sub setup_actions {
a268a011 368 my $self = shift;
99fe1710 369
12e28165 370 # These are the core structures
371 $self->actions(
372 {
373 plain => {},
374 private => {},
375 regex => {},
376 compiled => [],
377 reverse => {}
378 }
379 );
380
381 # We use a tree
382 $self->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
e494bd6b 383
a268a011 384 for my $comp ( keys %{ $self->components } ) {
e494bd6b 385
386 # We only setup components that inherit from Catalyst::Base
a268a011 387 next unless $comp->isa('Catalyst::Base');
99fe1710 388
812a28c9 389 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
1abd6db7 390 my ( $code, $attrs ) = @{$action};
391 my $name = '';
392 no strict 'refs';
393 my @cache = ( $comp, @{"$comp\::ISA"} );
394 my %namespaces;
99fe1710 395
1abd6db7 396 while ( my $namespace = shift @cache ) {
397 $namespaces{$namespace}++;
398 for my $isa ( @{"$comp\::ISA"} ) {
399 next if $namespaces{$isa};
400 push @cache, $isa;
401 $namespaces{$isa}++;
402 }
403 }
99fe1710 404
1abd6db7 405 for my $namespace ( keys %namespaces ) {
99fe1710 406
1abd6db7 407 for my $sym ( values %{ $namespace . '::' } ) {
99fe1710 408
1abd6db7 409 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
99fe1710 410
1abd6db7 411 $name = *{$sym}{NAME};
412 $self->set_action( $name, $code, $comp, $attrs );
413 last;
414 }
99fe1710 415
1abd6db7 416 }
99fe1710 417
1abd6db7 418 }
99fe1710 419
1abd6db7 420 }
99fe1710 421
1abd6db7 422 }
e494bd6b 423
a268a011 424 return unless $self->debug;
99fe1710 425
1abd6db7 426 my $actions = $self->actions;
427 my $privates = Text::ASCIITable->new;
5fbed090 428 $privates->setCols( 'Private', 'Class' );
429 $privates->setColWidth( 'Private', 36, 1 );
430 $privates->setColWidth( 'Class', 37, 1 );
99fe1710 431
1abd6db7 432 my $walker = sub {
433 my ( $walker, $parent, $prefix ) = @_;
434 $prefix .= $parent->getNodeValue || '';
435 $prefix .= '/' unless $prefix =~ /\/$/;
436 my $uid = $parent->getUID;
99fe1710 437
1abd6db7 438 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
439 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
d36e81fd 440 $privates->addRow( "$prefix$action", $class );
1abd6db7 441 }
99fe1710 442
1abd6db7 443 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
444 };
99fe1710 445
1abd6db7 446 $walker->( $walker, $self->tree, '' );
f45789b1 447 $self->log->debug( "Loaded private actions:\n" . $privates->draw )
a268a011 448 if ( @{ $privates->{tbl_rows} } );
99fe1710 449
1abd6db7 450 my $publics = Text::ASCIITable->new;
451 $publics->setCols( 'Public', 'Private' );
699e1247 452 $publics->setColWidth( 'Public', 36, 1 );
453 $publics->setColWidth( 'Private', 37, 1 );
99fe1710 454
1abd6db7 455 for my $plain ( sort keys %{ $actions->{plain} } ) {
456 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
7ace516e 457 my $reverse = $self->actions->{reverse}->{$code};
458 $reverse = $reverse ? "/$reverse" : $code;
cd677e12 459 $publics->addRow( "/$plain", $reverse );
1abd6db7 460 }
99fe1710 461
f45789b1 462 $self->log->debug( "Loaded public actions:\n" . $publics->draw )
a268a011 463 if ( @{ $publics->{tbl_rows} } );
99fe1710 464
1abd6db7 465 my $regexes = Text::ASCIITable->new;
466 $regexes->setCols( 'Regex', 'Private' );
699e1247 467 $regexes->setColWidth( 'Regex', 36, 1 );
468 $regexes->setColWidth( 'Private', 37, 1 );
99fe1710 469
1abd6db7 470 for my $regex ( sort keys %{ $actions->{regex} } ) {
471 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
7ace516e 472 my $reverse = $self->actions->{reverse}->{$code};
473 $reverse = $reverse ? "/$reverse" : $code;
cd677e12 474 $regexes->addRow( $regex, $reverse );
1abd6db7 475 }
99fe1710 476
f45789b1 477 $self->log->debug( "Loaded regex actions:\n" . $regexes->draw )
a268a011 478 if ( @{ $regexes->{tbl_rows} } );
1abd6db7 479}
480
1abd6db7 481=back
482
483=head1 AUTHOR
484
485Sebastian Riedel, C<sri@cpan.org>
486
487=head1 COPYRIGHT
488
489This program is free software, you can redistribute it and/or modify it under
490the same terms as Perl itself.
491
492=cut
493
4941;