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