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