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