Fixed extra.conf to use correct tmp dir
[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;
1abd6db7 9use Text::ASCIITable;
1abd6db7 10use Tree::Simple;
11use Tree::Simple::Visitor::FindByPath;
12
fbcc39ad 13# Stringify to class
14use overload '""' => sub { return ref shift }, fallback => 1;
15
16__PACKAGE__->mk_accessors(qw/actions tree/);
1abd6db7 17
1abd6db7 18=head1 NAME
19
9c053379 20Catalyst::Dispatcher - The Catalyst Dispatcher
1abd6db7 21
22=head1 SYNOPSIS
23
24See L<Catalyst>.
25
26=head1 DESCRIPTION
27
28=head1 METHODS
29
30=over 4
31
fbcc39ad 32=item $self->detach( $c, $command [, \@arguments ] )
6ef62eb2 33
34=cut
35
36sub detach {
fbcc39ad 37 my ( $self, $c, $command, @args ) = @_;
bd7d2e94 38 $c->forward( $command, @args ) if $command;
fbcc39ad 39 die $Catalyst::DETACH;
6ef62eb2 40}
41
fbcc39ad 42=item $self->dispatch($c)
1abd6db7 43
44=cut
45
46sub dispatch {
fbcc39ad 47 my ( $self, $c ) = @_;
1abd6db7 48 my $action = $c->req->action;
49 my $namespace = '';
50 $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
51 if $action eq 'default';
99fe1710 52
1abd6db7 53 unless ($namespace) {
54 if ( my $result = $c->get_action($action) ) {
cfd04b0c 55 $namespace = $result->[0]->[0]->prefix;
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
cfd04b0c 63 my @containers = $self->get_containers( $namespace );
64 my %actions;
65 foreach my $name (qw/begin auto end/) {
66 $actions{$name} = [
67 map { $_->{$name} }
68 grep { exists $_->{$name} }
69 map { $_->actions }
70 @containers
71 ];
72 }
73
1abd6db7 74 if ( @{$results} ) {
75
fbcc39ad 76 # Errors break the normal flow and the end action is instantly run
77 my $error = 0;
78
1abd6db7 79 # Execute last begin
80 $c->state(1);
cfd04b0c 81 if ( my $begin = @{ $actions{begin} }[-1] ) {
82 $begin->execute($c);
fbcc39ad 83 $error++ if scalar @{ $c->error };
1abd6db7 84 }
85
86 # Execute the auto chain
1196a46d 87 my $autorun = 0;
cfd04b0c 88 for my $auto ( @{ $actions{auto} } ) {
fbcc39ad 89 last if $error;
9ddd9d05 90 $autorun++;
cfd04b0c 91 $auto->execute($c);
fbcc39ad 92 $error++ if scalar @{ $c->error };
1abd6db7 93 last unless $c->state;
94 }
95
96 # Execute the action or last default
e5f21aa2 97 my $mkay = $autorun ? $c->state ? 1 : 0 : 1;
61cfdd57 98 if ( ( my $action = $c->req->action ) && $mkay ) {
fbcc39ad 99 unless ($error) {
100 if ( my $result =
101 @{ $c->get_action( $action, $default, 1 ) }[-1] )
102 {
103 $result->[0]->execute($c);
104 $error++ if scalar @{ $c->error };
105 }
1abd6db7 106 }
107 }
108
109 # Execute last end
cfd04b0c 110 if ( my $end = @{ $actions{end} }[-1] ) {
111 $end->execute($c);
1abd6db7 112 }
fbcc39ad 113 }
114
115 else {
1abd6db7 116 my $path = $c->req->path;
117 my $error = $path
118 ? qq/Unknown resource "$path"/
119 : "No default action defined";
120 $c->log->error($error) if $c->debug;
121 $c->error($error);
122 }
123}
124
fbcc39ad 125=item $self->forward( $c, $command [, \@arguments ] )
1abd6db7 126
127=cut
128
129sub forward {
fbcc39ad 130 my $self = shift;
1abd6db7 131 my $c = shift;
132 my $command = shift;
99fe1710 133
1abd6db7 134 unless ($command) {
135 $c->log->debug('Nothing to forward to') if $c->debug;
136 return 0;
137 }
99fe1710 138
71c3bcc3 139 # Relative forwards from detach
fbcc39ad 140 my $caller = ( caller(1) )[0]->isa('Catalyst::Dispatcher')
141 && ( ( caller(2) )[3] =~ /::detach$/ ) ? caller(3) : caller(1);
71c3bcc3 142
e0fc6749 143 my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
99fe1710 144
fbcc39ad 145 my $results = [];
146
8199eac3 147 my $command_copy = $command;
148
149 unless ( $command_copy =~ s/^\/// ) {
5d68b17b 150 my $namespace =
151 Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} ) || '';
152 $command_copy = "${namespace}/${command}";
1abd6db7 153 }
99fe1710 154
8199eac3 155 unless ( $command_copy =~ /\// ) {
156 $results = $c->get_action( $command_copy, '/' );
157 }
e494bd6b 158 else {
8199eac3 159 my @extra_args;
160 DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) {
161 my $tail = $2;
162 $results = $c->get_action( $tail, $1 );
163 if ( @{$results} ) {
164 $command = $tail;
165 push( @{$arguments}, @extra_args );
166 last DESCEND;
167 }
168 unshift( @extra_args, $tail );
169 }
e494bd6b 170 }
99fe1710 171
1abd6db7 172 unless ( @{$results} ) {
bd7d2e94 173
fbcc39ad 174 unless ( $c->components->{$command} ) {
bd7d2e94 175 my $error =
176qq/Couldn't forward to command "$command". Invalid action or component./;
3b2ed580 177 $c->error($error);
178 $c->log->debug($error) if $c->debug;
1abd6db7 179 return 0;
180 }
bd7d2e94 181
1f6bb799 182 my $class = $command;
1abd6db7 183 my $method = shift || 'process';
99fe1710 184
1f6bb799 185 if ( my $code = $c->components->{$class}->can($method) ) {
fbcc39ad 186 my $action = Catalyst::Action->new(
187 {
188 code => $code,
189 reverse => "$class->$method",
190 namespace => $class,
cfd04b0c 191 prefix => $class,
fbcc39ad 192 }
193 );
194 $results = [ [$action] ];
195 }
196
197 else {
bd7d2e94 198 my $error =
199 qq/Couldn't forward to "$class". Does not implement "$method"/;
3b2ed580 200 $c->error($error);
201 $c->log->debug($error)
1abd6db7 202 if $c->debug;
203 return 0;
204 }
99fe1710 205
1abd6db7 206 }
bd7d2e94 207
208 local $c->request->{arguments} = [ @{$arguments} ];
99fe1710 209
1abd6db7 210 for my $result ( @{$results} ) {
fbcc39ad 211 $result->[0]->execute($c);
e0fc6749 212 return if scalar @{ $c->error };
1abd6db7 213 last unless $c->state;
214 }
99fe1710 215
1abd6db7 216 return $c->state;
217}
218
fbcc39ad 219=item $self->prepare_action($c)
220
221=cut
222
223sub prepare_action {
224 my ( $self, $c ) = @_;
225 my $path = $c->req->path;
226 my @path = split /\//, $c->req->path;
227 $c->req->args( \my @args );
228
229 while (@path) {
230 $path = join '/', @path;
231 if ( my $result = ${ $c->get_action($path) }[0] ) {
232
233 # It's a regex
234 if ($#$result) {
235 my $match = $result->[1];
236 my @snippets = @{ $result->[2] };
237 $c->log->debug(
238 qq/Requested action is "$path" and matched "$match"/)
239 if $c->debug;
240 $c->log->debug(
241 'Snippets are "' . join( ' ', @snippets ) . '"' )
242 if ( $c->debug && @snippets );
243 $c->req->action($match);
244 $c->req->snippets( \@snippets );
245 }
246
247 else {
248 $c->req->action($path);
249 $c->log->debug(qq/Requested action is "$path"/) if $c->debug;
250 }
1abd6db7 251
fbcc39ad 252 $c->req->match($path);
253 last;
254 }
255 unshift @args, pop @path;
256 }
257
258 unless ( $c->req->action ) {
259 $c->req->action('default');
260 $c->req->match('');
261 }
262
263 $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
264 if ( $c->debug && @args );
265}
266
267=item $self->get_action( $c, $action, $namespace, $inherit )
1abd6db7 268
269=cut
270
271sub get_action {
fbcc39ad 272 my ( $self, $c, $action, $namespace, $inherit ) = @_;
1abd6db7 273 return [] unless $action;
274 $namespace ||= '';
2d16b61a 275 $inherit ||= 0;
99fe1710 276
1abd6db7 277 if ($namespace) {
c8d9780f 278
cfd04b0c 279 my @match = $self->get_containers( $namespace );
99fe1710 280
c8d9780f 281 my @results;
99fe1710 282
cfd04b0c 283 foreach my $child ($inherit ? @match: $match[-1]) {
284 my $node = $child->actions;
c8d9780f 285 push(@results, [ $node->{$action} ]) if defined $node->{$action};
1abd6db7 286 }
287 return \@results;
288 }
99fe1710 289
fbcc39ad 290 elsif ( my $p = $self->actions->{plain}->{$action} ) { return [ [$p] ] }
291 elsif ( my $r = $self->actions->{regex}->{$action} ) { return [ [$r] ] }
99fe1710 292
1abd6db7 293 else {
99fe1710 294
fbcc39ad 295 for my $i ( 0 .. $#{ $self->actions->{compiled} } ) {
296 my $name = $self->actions->{compiled}->[$i]->[0];
297 my $regex = $self->actions->{compiled}->[$i]->[1];
99fe1710 298
2d752b2a 299 if ( my @snippets = ( $action =~ $regex ) ) {
fbcc39ad 300 return [
301 [ $self->actions->{regex}->{$name}, $name, \@snippets ] ];
1abd6db7 302 }
99fe1710 303
1abd6db7 304 }
305 }
306 return [];
307}
308
cfd04b0c 309=item $self->get_containers( $namespace )
310
311=cut
312
313sub get_containers {
314 my ( $self, $namespace ) = @_;
315
316 return ($self->tree->getNodeValue) if $namespace eq '/';
317
318 my $visitor = Tree::Simple::Visitor::FindByPath->new;
319 my @path = split('/', $namespace);
320 $visitor->setSearchPath( @path );
321 $self->tree->accept($visitor);
322
323 my @match = $visitor->getResults;
324 @match = ($self->tree) unless @match;
325
326 if (!defined $visitor->getResult) {
327 my $extra = $path[(scalar @match) - 1];
328 last unless $extra;
329 $visitor->setSearchPath($extra);
330 $match[-1]->accept($visitor);
331 push(@match, $visitor->getResult) if defined $visitor->getResult;
332 }
333
334 return map { $_->getNodeValue } @match;
335}
336
fbcc39ad 337=item $self->set_action( $c, $action, $code, $namespace, $attrs )
1abd6db7 338
339=cut
340
341sub set_action {
fbcc39ad 342 my ( $self, $c, $method, $code, $namespace, $attrs ) = @_;
1abd6db7 343
e494bd6b 344 my $prefix =
345 Catalyst::Utils::class2prefix( $namespace, $c->config->{case_sensitive} )
346 || '';
1abd6db7 347 my %flags;
348
349 for my $attr ( @{$attrs} ) {
0299ba22 350 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
351 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
749472d6 352 elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) {
353 push @{ $flags{path} }, $1;
354 }
355 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
0299ba22 356 elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) {
749472d6 357 push @{ $flags{regex} }, $2;
0299ba22 358 }
1abd6db7 359 }
360
d0e524d2 361 if ( $flags{private} && ( keys %flags > 1 ) ) {
8d4e224b 362 $c->log->debug( 'Bad action definition "'
d0e524d2 363 . join( ' ', @{$attrs} )
8d4e224b 364 . qq/" for "$namespace->$method"/ )
365 if $c->debug;
d0e524d2 366 return;
367 }
1abd6db7 368 return unless keys %flags;
369
fbcc39ad 370 my $parent = $self->tree;
1abd6db7 371 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 372
b7aebc12 373 if ($prefix) {
374 for my $part ( split '/', $prefix ) {
1abd6db7 375 $visitor->setSearchPath($part);
376 $parent->accept($visitor);
b7aebc12 377 my $child = $visitor->getResult;
378
379 unless ($child) {
380 my $container = Catalyst::ActionContainer->new(
381 { part => $part, actions => {} });
382 $child = $parent->addChild( Tree::Simple->new($container) );
383 $visitor->setSearchPath($part);
384 $parent->accept($visitor);
385 $child = $visitor->getResult;
386 }
387
388 $parent = $child;
1abd6db7 389 }
1abd6db7 390 }
99fe1710 391
fbcc39ad 392 my $reverse = $prefix ? "$prefix/$method" : $method;
393
394 my $action = Catalyst::Action->new(
395 {
396 code => $code,
397 reverse => $reverse,
398 namespace => $namespace,
cfd04b0c 399 prefix => $prefix,
fbcc39ad 400 }
401 );
402
b7aebc12 403 $parent->getNodeValue->actions->{$method} = $action;
fbcc39ad 404
749472d6 405 my @path;
406 for my $path ( @{ $flags{path} } ) {
407 $path =~ s/^\w+//;
408 $path =~ s/\w+$//;
409 if ( $path =~ /^\s*'(.*)'\s*$/ ) { $path = $1 }
410 if ( $path =~ /^\s*"(.*)"\s*$/ ) { $path = $1 }
411 push @path, $path;
1abd6db7 412 }
749472d6 413 $flags{path} = \@path;
414
415 my @regex;
416 for my $regex ( @{ $flags{regex} } ) {
417 $regex =~ s/^\w+//;
418 $regex =~ s/\w+$//;
419 if ( $regex =~ /^\s*'(.*)'\s*$/ ) { $regex = $1 }
420 if ( $regex =~ /^\s*"(.*)"\s*$/ ) { $regex = $1 }
421 push @regex, $regex;
1abd6db7 422 }
749472d6 423 $flags{regex} = \@regex;
1abd6db7 424
749472d6 425 if ( $flags{local} || $flags{global} ) {
426 push( @{ $flags{path} }, $prefix ? "/$prefix/$method" : "/$method" )
427 if $flags{local};
99fe1710 428
749472d6 429 push( @{ $flags{path} }, "/$method" ) if $flags{global};
430 }
99fe1710 431
749472d6 432 for my $path ( @{ $flags{path} } ) {
433 if ( $path =~ /^\// ) { $path =~ s/^\/// }
434 else { $path = $prefix ? "$prefix/$path" : $path }
435 $self->actions->{plain}->{$path} = $action;
1abd6db7 436 }
99fe1710 437
749472d6 438 for my $regex ( @{ $flags{regex} } ) {
fbcc39ad 439 push @{ $self->actions->{compiled} }, [ $regex, qr#$regex# ];
440 $self->actions->{regex}->{$regex} = $action;
1abd6db7 441 }
1abd6db7 442}
443
fbcc39ad 444=item $self->setup_actions( $class, $component )
1abd6db7 445
446=cut
447
448sub setup_actions {
fbcc39ad 449 my ( $self, $class ) = @_;
99fe1710 450
12e28165 451 # These are the core structures
452 $self->actions(
453 {
454 plain => {},
455 private => {},
456 regex => {},
fbcc39ad 457 compiled => []
12e28165 458 }
459 );
460
461 # We use a tree
b7aebc12 462 my $container = Catalyst::ActionContainer->new(
463 { part => '/', actions => {} } );
464 $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) );
e494bd6b 465
fbcc39ad 466 for my $comp ( keys %{ $class->components } ) {
e494bd6b 467
468 # We only setup components that inherit from Catalyst::Base
a268a011 469 next unless $comp->isa('Catalyst::Base');
99fe1710 470
812a28c9 471 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
1abd6db7 472 my ( $code, $attrs ) = @{$action};
473 my $name = '';
474 no strict 'refs';
475 my @cache = ( $comp, @{"$comp\::ISA"} );
476 my %namespaces;
99fe1710 477
1abd6db7 478 while ( my $namespace = shift @cache ) {
479 $namespaces{$namespace}++;
480 for my $isa ( @{"$comp\::ISA"} ) {
481 next if $namespaces{$isa};
482 push @cache, $isa;
483 $namespaces{$isa}++;
484 }
485 }
99fe1710 486
1abd6db7 487 for my $namespace ( keys %namespaces ) {
99fe1710 488
1abd6db7 489 for my $sym ( values %{ $namespace . '::' } ) {
99fe1710 490
1abd6db7 491 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
99fe1710 492
1abd6db7 493 $name = *{$sym}{NAME};
fbcc39ad 494 $class->set_action( $name, $code, $comp, $attrs );
1abd6db7 495 last;
496 }
99fe1710 497
1abd6db7 498 }
99fe1710 499
1abd6db7 500 }
99fe1710 501
1abd6db7 502 }
99fe1710 503
1abd6db7 504 }
e494bd6b 505
fbcc39ad 506 return unless $class->debug;
99fe1710 507
1abd6db7 508 my $actions = $self->actions;
509 my $privates = Text::ASCIITable->new;
5fbed090 510 $privates->setCols( 'Private', 'Class' );
511 $privates->setColWidth( 'Private', 36, 1 );
512 $privates->setColWidth( 'Class', 37, 1 );
99fe1710 513
1abd6db7 514 my $walker = sub {
515 my ( $walker, $parent, $prefix ) = @_;
516 $prefix .= $parent->getNodeValue || '';
517 $prefix .= '/' unless $prefix =~ /\/$/;
b7aebc12 518 my $node = $parent->getNodeValue->actions;
99fe1710 519
b7aebc12 520 for my $action ( keys %{ $node } ) {
521 my $action_obj = $node->{$action};
fbcc39ad 522 $privates->addRow( "$prefix$action", $action_obj->namespace );
1abd6db7 523 }
99fe1710 524
1abd6db7 525 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
526 };
99fe1710 527
1abd6db7 528 $walker->( $walker, $self->tree, '' );
fbcc39ad 529 $class->log->debug( "Loaded private actions:\n" . $privates->draw )
a268a011 530 if ( @{ $privates->{tbl_rows} } );
99fe1710 531
1abd6db7 532 my $publics = Text::ASCIITable->new;
533 $publics->setCols( 'Public', 'Private' );
699e1247 534 $publics->setColWidth( 'Public', 36, 1 );
535 $publics->setColWidth( 'Private', 37, 1 );
99fe1710 536
1abd6db7 537 for my $plain ( sort keys %{ $actions->{plain} } ) {
fbcc39ad 538 my $action = $actions->{plain}->{$plain};
539 $publics->addRow( "/$plain", "/$action" );
1abd6db7 540 }
99fe1710 541
fbcc39ad 542 $class->log->debug( "Loaded public actions:\n" . $publics->draw )
a268a011 543 if ( @{ $publics->{tbl_rows} } );
99fe1710 544
1abd6db7 545 my $regexes = Text::ASCIITable->new;
546 $regexes->setCols( 'Regex', 'Private' );
699e1247 547 $regexes->setColWidth( 'Regex', 36, 1 );
548 $regexes->setColWidth( 'Private', 37, 1 );
99fe1710 549
1abd6db7 550 for my $regex ( sort keys %{ $actions->{regex} } ) {
fbcc39ad 551 my $action = $actions->{regex}->{$regex};
552 $regexes->addRow( $regex, "/$action" );
1abd6db7 553 }
99fe1710 554
fbcc39ad 555 $class->log->debug( "Loaded regex actions:\n" . $regexes->draw )
a268a011 556 if ( @{ $regexes->{tbl_rows} } );
1abd6db7 557}
558
1abd6db7 559=back
560
561=head1 AUTHOR
562
563Sebastian Riedel, C<sri@cpan.org>
564
565=head1 COPYRIGHT
566
567This program is free software, you can redistribute it and/or modify it under
568the same terms as Perl itself.
569
570=cut
571
5721;