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 || ''; |
fe14cf78 |
138 | my $path = $class . '.pm'; |
139 | $path =~ s/::/\//g; |
99fe1710 |
140 | |
fe14cf78 |
141 | unless ( $INC{ $path } ) { |
47ce945e |
142 | my $error = qq/Couldn't forward to "$class". Invalid or not loaded./; |
3b2ed580 |
143 | $c->error($error); |
144 | $c->log->debug($error) if $c->debug; |
1abd6db7 |
145 | return 0; |
146 | } |
3245f607 |
147 | |
148 | unless ( UNIVERSAL::isa( $class, 'Catalyst::Base' ) ) { |
149 | my $error = qq/Can't forward to "$class". Class is not a Catalyst component./; |
150 | $c->error($error); |
151 | $c->log->debug($error) if $c->debug; |
152 | return 0; |
153 | } |
99fe1710 |
154 | |
1abd6db7 |
155 | my $method = shift || 'process'; |
99fe1710 |
156 | |
1abd6db7 |
157 | if ( my $code = $class->can($method) ) { |
158 | $c->actions->{reverse}->{"$code"} = "$class->$method"; |
159 | $results = [ [ [ $class, $code ] ] ]; |
160 | } |
99fe1710 |
161 | |
1abd6db7 |
162 | else { |
5d2c7203 |
163 | my $error = 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 | |
181 | =item $c->get_action( $action, $namespace ) |
182 | |
183 | Get an action in a given namespace. |
184 | |
185 | =cut |
186 | |
187 | sub get_action { |
188 | my ( $c, $action, $namespace ) = @_; |
189 | return [] unless $action; |
190 | $namespace ||= ''; |
99fe1710 |
191 | |
1abd6db7 |
192 | if ($namespace) { |
193 | $namespace = '' if $namespace eq '/'; |
194 | my $parent = $c->tree; |
195 | my @results; |
196 | my %allowed = ( begin => 1, auto => 1, default => 1, end => 1 ); |
99fe1710 |
197 | |
1abd6db7 |
198 | if ( $allowed{$action} ) { |
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 | |
258 | Set an action in a given namespace. |
259 | |
260 | =cut |
261 | |
262 | sub set_action { |
263 | my ( $c, $method, $code, $namespace, $attrs ) = @_; |
264 | |
f05af9ba |
265 | my $prefix = Catalyst::Utils::class2prefix($namespace) || ''; |
1abd6db7 |
266 | my %flags; |
267 | |
268 | for my $attr ( @{$attrs} ) { |
269 | if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ } |
270 | elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ } |
271 | elsif ( $attr =~ /^Path\((.+)\)$/i ) { $flags{path} = $1 } |
272 | elsif ( $attr =~ /^Private$/i ) { $flags{private}++ } |
273 | elsif ( $attr =~ /^(Regex|Regexp)\((.+)\)$/i ) { $flags{regex} = $2 } |
274 | } |
275 | |
d0e524d2 |
276 | if ( $flags{private} && ( keys %flags > 1 ) ) { |
8d4e224b |
277 | $c->log->debug( 'Bad action definition "' |
d0e524d2 |
278 | . join( ' ', @{$attrs} ) |
8d4e224b |
279 | . qq/" for "$namespace->$method"/ ) |
280 | if $c->debug; |
d0e524d2 |
281 | return; |
282 | } |
1abd6db7 |
283 | return unless keys %flags; |
284 | |
285 | my $parent = $c->tree; |
286 | my $visitor = Tree::Simple::Visitor::FindByPath->new; |
99fe1710 |
287 | |
1abd6db7 |
288 | for my $part ( split '/', $prefix ) { |
289 | $visitor->setSearchPath($part); |
290 | $parent->accept($visitor); |
291 | my $child = $visitor->getResult; |
99fe1710 |
292 | |
1abd6db7 |
293 | unless ($child) { |
294 | $child = $parent->addChild( Tree::Simple->new($part) ); |
295 | $visitor->setSearchPath($part); |
296 | $parent->accept($visitor); |
297 | $child = $visitor->getResult; |
298 | } |
99fe1710 |
299 | |
1abd6db7 |
300 | $parent = $child; |
301 | } |
99fe1710 |
302 | |
1abd6db7 |
303 | my $uid = $parent->getUID; |
304 | $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ]; |
305 | my $forward = $prefix ? "$prefix/$method" : $method; |
306 | |
307 | if ( $flags{path} ) { |
308 | $flags{path} =~ s/^\w+//; |
309 | $flags{path} =~ s/\w+$//; |
310 | if ( $flags{path} =~ /^'(.*)'$/ ) { $flags{path} = $1 } |
311 | if ( $flags{path} =~ /^"(.*)"$/ ) { $flags{path} = $1 } |
312 | } |
99fe1710 |
313 | |
1abd6db7 |
314 | if ( $flags{regex} ) { |
315 | $flags{regex} =~ s/^\w+//; |
316 | $flags{regex} =~ s/\w+$//; |
317 | if ( $flags{regex} =~ /^'(.*)'$/ ) { $flags{regex} = $1 } |
318 | if ( $flags{regex} =~ /^"(.*)"$/ ) { $flags{regex} = $1 } |
319 | } |
320 | |
321 | my $reverse = $prefix ? "$prefix/$method" : $method; |
322 | |
323 | if ( $flags{local} || $flags{global} || $flags{path} ) { |
99fe1710 |
324 | my $path = $flags{path} || $method; |
1abd6db7 |
325 | my $absolute = 0; |
99fe1710 |
326 | |
1abd6db7 |
327 | if ( $path =~ /^\/(.+)/ ) { |
328 | $path = $1; |
329 | $absolute = 1; |
330 | } |
99fe1710 |
331 | |
1abd6db7 |
332 | $absolute = 1 if $flags{global}; |
333 | my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path; |
334 | $c->actions->{plain}->{$name} = [ $namespace, $code ]; |
335 | } |
99fe1710 |
336 | |
1abd6db7 |
337 | if ( my $regex = $flags{regex} ) { |
338 | push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ]; |
339 | $c->actions->{regex}->{$regex} = [ $namespace, $code ]; |
340 | } |
341 | |
342 | $c->actions->{reverse}->{"$code"} = $reverse; |
343 | } |
344 | |
345 | =item $class->setup_actions($component) |
346 | |
347 | Setup actions for a component. |
348 | |
349 | =cut |
350 | |
351 | sub setup_actions { |
352 | my ( $self, $comps ) = @_; |
99fe1710 |
353 | |
1abd6db7 |
354 | for my $comp (@$comps) { |
355 | $comp = ref $comp || $comp; |
99fe1710 |
356 | |
1abd6db7 |
357 | for my $action ( @{ $comp->_cache } ) { |
358 | my ( $code, $attrs ) = @{$action}; |
359 | my $name = ''; |
360 | no strict 'refs'; |
361 | my @cache = ( $comp, @{"$comp\::ISA"} ); |
362 | my %namespaces; |
99fe1710 |
363 | |
1abd6db7 |
364 | while ( my $namespace = shift @cache ) { |
365 | $namespaces{$namespace}++; |
366 | for my $isa ( @{"$comp\::ISA"} ) { |
367 | next if $namespaces{$isa}; |
368 | push @cache, $isa; |
369 | $namespaces{$isa}++; |
370 | } |
371 | } |
99fe1710 |
372 | |
1abd6db7 |
373 | for my $namespace ( keys %namespaces ) { |
99fe1710 |
374 | |
1abd6db7 |
375 | for my $sym ( values %{ $namespace . '::' } ) { |
99fe1710 |
376 | |
1abd6db7 |
377 | if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) { |
99fe1710 |
378 | |
1abd6db7 |
379 | $name = *{$sym}{NAME}; |
380 | $self->set_action( $name, $code, $comp, $attrs ); |
381 | last; |
382 | } |
99fe1710 |
383 | |
1abd6db7 |
384 | } |
99fe1710 |
385 | |
1abd6db7 |
386 | } |
99fe1710 |
387 | |
1abd6db7 |
388 | } |
99fe1710 |
389 | |
1abd6db7 |
390 | } |
99fe1710 |
391 | |
1abd6db7 |
392 | my $actions = $self->actions; |
393 | my $privates = Text::ASCIITable->new; |
5fbed090 |
394 | $privates->setCols( 'Private', 'Class' ); |
395 | $privates->setColWidth( 'Private', 36, 1 ); |
396 | $privates->setColWidth( 'Class', 37, 1 ); |
99fe1710 |
397 | |
1abd6db7 |
398 | my $walker = sub { |
399 | my ( $walker, $parent, $prefix ) = @_; |
400 | $prefix .= $parent->getNodeValue || ''; |
401 | $prefix .= '/' unless $prefix =~ /\/$/; |
402 | my $uid = $parent->getUID; |
99fe1710 |
403 | |
1abd6db7 |
404 | for my $action ( keys %{ $actions->{private}->{$uid} } ) { |
405 | my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} }; |
d36e81fd |
406 | $privates->addRow( "$prefix$action", $class ); |
1abd6db7 |
407 | } |
99fe1710 |
408 | |
1abd6db7 |
409 | $walker->( $walker, $_, $prefix ) for $parent->getAllChildren; |
410 | }; |
99fe1710 |
411 | |
1abd6db7 |
412 | $walker->( $walker, $self->tree, '' ); |
413 | $self->log->debug( 'Loaded private actions', $privates->draw ) |
414 | if ( @{ $privates->{tbl_rows} } && $self->debug ); |
99fe1710 |
415 | |
1abd6db7 |
416 | my $publics = Text::ASCIITable->new; |
417 | $publics->setCols( 'Public', 'Private' ); |
699e1247 |
418 | $publics->setColWidth( 'Public', 36, 1 ); |
419 | $publics->setColWidth( 'Private', 37, 1 ); |
99fe1710 |
420 | |
1abd6db7 |
421 | for my $plain ( sort keys %{ $actions->{plain} } ) { |
422 | my ( $class, $code ) = @{ $actions->{plain}->{$plain} }; |
7ace516e |
423 | my $reverse = $self->actions->{reverse}->{$code}; |
424 | $reverse = $reverse ? "/$reverse" : $code; |
cd677e12 |
425 | $publics->addRow( "/$plain", $reverse ); |
1abd6db7 |
426 | } |
99fe1710 |
427 | |
1abd6db7 |
428 | $self->log->debug( 'Loaded public actions', $publics->draw ) |
429 | if ( @{ $publics->{tbl_rows} } && $self->debug ); |
99fe1710 |
430 | |
1abd6db7 |
431 | my $regexes = Text::ASCIITable->new; |
432 | $regexes->setCols( 'Regex', 'Private' ); |
699e1247 |
433 | $regexes->setColWidth( 'Regex', 36, 1 ); |
434 | $regexes->setColWidth( 'Private', 37, 1 ); |
99fe1710 |
435 | |
1abd6db7 |
436 | for my $regex ( sort keys %{ $actions->{regex} } ) { |
437 | my ( $class, $code ) = @{ $actions->{regex}->{$regex} }; |
7ace516e |
438 | my $reverse = $self->actions->{reverse}->{$code}; |
439 | $reverse = $reverse ? "/$reverse" : $code; |
cd677e12 |
440 | $regexes->addRow( $regex, $reverse ); |
1abd6db7 |
441 | } |
99fe1710 |
442 | |
1abd6db7 |
443 | $self->log->debug( 'Loaded regex actions', $regexes->draw ) |
444 | if ( @{ $regexes->{tbl_rows} } && $self->debug ); |
445 | } |
446 | |
1abd6db7 |
447 | =back |
448 | |
449 | =head1 AUTHOR |
450 | |
451 | Sebastian Riedel, C<sri@cpan.org> |
452 | |
453 | =head1 COPYRIGHT |
454 | |
455 | This program is free software, you can redistribute it and/or modify it under |
456 | the same terms as Perl itself. |
457 | |
458 | =cut |
459 | |
460 | 1; |