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