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