Commit | Line | Data |
68a748b9 |
1 | package Catalyst::Dispatcher; |
1abd6db7 |
2 | |
3 | use strict; |
fbcc39ad |
4 | use base 'Class::Accessor::Fast'; |
a2f2cde9 |
5 | use Catalyst::Exception; |
f05af9ba |
6 | use Catalyst::Utils; |
fbcc39ad |
7 | use Catalyst::Action; |
b7aebc12 |
8 | use Catalyst::ActionContainer; |
b96f127f |
9 | use Catalyst::DispatchType::Default; |
bcccee4e |
10 | use Catalyst::DispatchType::Index; |
1abd6db7 |
11 | use Text::ASCIITable; |
1abd6db7 |
12 | use Tree::Simple; |
13 | use Tree::Simple::Visitor::FindByPath; |
14 | |
fbcc39ad |
15 | # Stringify to class |
16 | use overload '""' => sub { return ref shift }, fallback => 1; |
17 | |
6d030e6f |
18 | __PACKAGE__->mk_accessors(qw/tree dispatch_types/); |
19 | |
20 | # Preload these action types |
21 | our @PRELOAD = qw/Path Regex/; |
1abd6db7 |
22 | |
2d1d8f91 |
23 | # Postload these action types |
24 | our @POSTLOAD = qw/Index Default/; |
25 | |
1abd6db7 |
26 | =head1 NAME |
27 | |
9c053379 |
28 | Catalyst::Dispatcher - The Catalyst Dispatcher |
1abd6db7 |
29 | |
30 | =head1 SYNOPSIS |
31 | |
32 | See L<Catalyst>. |
33 | |
34 | =head1 DESCRIPTION |
35 | |
36 | =head1 METHODS |
37 | |
38 | =over 4 |
39 | |
fbcc39ad |
40 | =item $self->detach( $c, $command [, \@arguments ] ) |
6ef62eb2 |
41 | |
42 | =cut |
43 | |
44 | sub detach { |
fbcc39ad |
45 | my ( $self, $c, $command, @args ) = @_; |
bd7d2e94 |
46 | $c->forward( $command, @args ) if $command; |
fbcc39ad |
47 | die $Catalyst::DETACH; |
6ef62eb2 |
48 | } |
49 | |
fbcc39ad |
50 | =item $self->dispatch($c) |
1abd6db7 |
51 | |
52 | =cut |
53 | |
54 | sub dispatch { |
fbcc39ad |
55 | my ( $self, $c ) = @_; |
cfd04b0c |
56 | |
66e28e3f |
57 | if ( $c->action ) { |
ba599d1c |
58 | $c->forward( join('/', '', $c->namespace, '_DISPATCH') ); |
fbcc39ad |
59 | } |
60 | |
61 | else { |
1abd6db7 |
62 | my $path = $c->req->path; |
63 | my $error = $path |
64 | ? qq/Unknown resource "$path"/ |
65 | : "No default action defined"; |
66 | $c->log->error($error) if $c->debug; |
67 | $c->error($error); |
68 | } |
69 | } |
70 | |
fbcc39ad |
71 | =item $self->forward( $c, $command [, \@arguments ] ) |
1abd6db7 |
72 | |
73 | =cut |
74 | |
75 | sub forward { |
fbcc39ad |
76 | my $self = shift; |
1abd6db7 |
77 | my $c = shift; |
78 | my $command = shift; |
99fe1710 |
79 | |
1abd6db7 |
80 | unless ($command) { |
81 | $c->log->debug('Nothing to forward to') if $c->debug; |
82 | return 0; |
83 | } |
99fe1710 |
84 | |
71c3bcc3 |
85 | # Relative forwards from detach |
fbcc39ad |
86 | my $caller = ( caller(1) )[0]->isa('Catalyst::Dispatcher') |
87 | && ( ( caller(2) )[3] =~ /::detach$/ ) ? caller(3) : caller(1); |
71c3bcc3 |
88 | |
e0fc6749 |
89 | my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args; |
99fe1710 |
90 | |
fbcc39ad |
91 | my $results = []; |
92 | |
8199eac3 |
93 | my $command_copy = $command; |
94 | |
95 | unless ( $command_copy =~ s/^\/// ) { |
5d68b17b |
96 | my $namespace = |
78d760bb |
97 | Catalyst::Utils::class2prefix( $caller, $c->config->{case_sensitive} ) |
98 | || ''; |
5d68b17b |
99 | $command_copy = "${namespace}/${command}"; |
1abd6db7 |
100 | } |
99fe1710 |
101 | |
8199eac3 |
102 | unless ( $command_copy =~ /\// ) { |
103 | $results = $c->get_action( $command_copy, '/' ); |
104 | } |
e494bd6b |
105 | else { |
8199eac3 |
106 | my @extra_args; |
107 | DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) { |
108 | my $tail = $2; |
109 | $results = $c->get_action( $tail, $1 ); |
110 | if ( @{$results} ) { |
111 | $command = $tail; |
112 | push( @{$arguments}, @extra_args ); |
113 | last DESCEND; |
114 | } |
115 | unshift( @extra_args, $tail ); |
116 | } |
e494bd6b |
117 | } |
99fe1710 |
118 | |
1abd6db7 |
119 | unless ( @{$results} ) { |
bd7d2e94 |
120 | |
fbcc39ad |
121 | unless ( $c->components->{$command} ) { |
bd7d2e94 |
122 | my $error = |
123 | qq/Couldn't forward to command "$command". Invalid action or component./; |
3b2ed580 |
124 | $c->error($error); |
125 | $c->log->debug($error) if $c->debug; |
1abd6db7 |
126 | return 0; |
127 | } |
bd7d2e94 |
128 | |
1f6bb799 |
129 | my $class = $command; |
1abd6db7 |
130 | my $method = shift || 'process'; |
99fe1710 |
131 | |
1f6bb799 |
132 | if ( my $code = $c->components->{$class}->can($method) ) { |
fbcc39ad |
133 | my $action = Catalyst::Action->new( |
134 | { |
6b239949 |
135 | name => $method, |
fbcc39ad |
136 | code => $code, |
137 | reverse => "$class->$method", |
11bd4e3e |
138 | class => $class, |
fbcc39ad |
139 | namespace => $class, |
140 | } |
141 | ); |
142 | $results = [ [$action] ]; |
143 | } |
144 | |
145 | else { |
bd7d2e94 |
146 | my $error = |
147 | qq/Couldn't forward to "$class". Does not implement "$method"/; |
3b2ed580 |
148 | $c->error($error); |
149 | $c->log->debug($error) |
1abd6db7 |
150 | if $c->debug; |
151 | return 0; |
152 | } |
99fe1710 |
153 | |
1abd6db7 |
154 | } |
bd7d2e94 |
155 | |
156 | local $c->request->{arguments} = [ @{$arguments} ]; |
99fe1710 |
157 | |
1abd6db7 |
158 | for my $result ( @{$results} ) { |
fbcc39ad |
159 | $result->[0]->execute($c); |
e0fc6749 |
160 | return if scalar @{ $c->error }; |
1abd6db7 |
161 | last unless $c->state; |
162 | } |
99fe1710 |
163 | |
1abd6db7 |
164 | return $c->state; |
165 | } |
166 | |
fbcc39ad |
167 | =item $self->prepare_action($c) |
168 | |
169 | =cut |
170 | |
171 | sub prepare_action { |
172 | my ( $self, $c ) = @_; |
173 | my $path = $c->req->path; |
174 | my @path = split /\//, $c->req->path; |
175 | $c->req->args( \my @args ); |
176 | |
78d760bb |
177 | push( @path, '/' ) unless @path; # Root action |
178 | |
b96f127f |
179 | DESCEND: while (@path) { |
fbcc39ad |
180 | $path = join '/', @path; |
fbcc39ad |
181 | |
78d760bb |
182 | $path = '' if $path eq '/'; # Root action |
183 | |
22f3a8dd |
184 | # Check out dispatch types to see if any will handle the path at |
185 | # this level |
186 | |
78d760bb |
187 | foreach my $type ( @{ $self->dispatch_types } ) { |
2633d7dc |
188 | last DESCEND if $type->match( $c, $path ); |
66e28e3f |
189 | } |
b96f127f |
190 | |
22f3a8dd |
191 | # If not, move the last part path to args |
192 | |
b96f127f |
193 | unshift @args, pop @path; |
fbcc39ad |
194 | } |
195 | |
196 | $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' ) |
197 | if ( $c->debug && @args ); |
198 | } |
199 | |
200 | =item $self->get_action( $c, $action, $namespace, $inherit ) |
1abd6db7 |
201 | |
202 | =cut |
203 | |
204 | sub get_action { |
fbcc39ad |
205 | my ( $self, $c, $action, $namespace, $inherit ) = @_; |
1abd6db7 |
206 | return [] unless $action; |
bcccee4e |
207 | $namespace ||= ''; |
772ab8ae |
208 | $namespace = '' if $namespace eq '/'; |
209 | $inherit ||= 0; |
99fe1710 |
210 | |
78d760bb |
211 | my @match = $self->get_containers($namespace); |
c8d9780f |
212 | |
772ab8ae |
213 | if ($inherit) { # Return [ [ $act_obj ], ... ] for valid containers |
214 | return [ |
215 | map { [ $_->{$action} ] } # Make [ $action_obj ] |
216 | grep { defined $_->{$action} } # If it exists in the container |
217 | map { $_->actions } # Get action hash for container |
218 | @match |
219 | ]; |
220 | } |
221 | else { |
222 | my $node = $match[-1]->actions; # Only bother looking at the last one |
223 | |
224 | if ( defined $node->{$action} |
11bd4e3e |
225 | && ( $node->{$action}->namespace eq $namespace ) ) |
772ab8ae |
226 | { |
227 | return [ [ $node->{$action} ] ]; |
228 | } |
229 | else { |
230 | return []; |
bcccee4e |
231 | } |
1abd6db7 |
232 | } |
1abd6db7 |
233 | } |
234 | |
cfd04b0c |
235 | =item $self->get_containers( $namespace ) |
236 | |
237 | =cut |
238 | |
239 | sub get_containers { |
240 | my ( $self, $namespace ) = @_; |
241 | |
90ce41ba |
242 | # If the namespace is / just return the root ActionContainer |
243 | |
78d760bb |
244 | return ( $self->tree->getNodeValue ) |
245 | if ( !$namespace || ( $namespace eq '/' ) ); |
cfd04b0c |
246 | |
90ce41ba |
247 | # Use a visitor to recurse down the tree finding the ActionContainers |
248 | # for each namespace in the chain. |
249 | |
cfd04b0c |
250 | my $visitor = Tree::Simple::Visitor::FindByPath->new; |
78d760bb |
251 | my @path = split( '/', $namespace ); |
252 | $visitor->setSearchPath(@path); |
cfd04b0c |
253 | $self->tree->accept($visitor); |
254 | |
255 | my @match = $visitor->getResults; |
78d760bb |
256 | @match = ( $self->tree ) unless @match; |
cfd04b0c |
257 | |
78d760bb |
258 | if ( !defined $visitor->getResult ) { |
90ce41ba |
259 | |
260 | # If we don't manage to match, the visitor doesn't return the last |
261 | # node is matched, so foo/bar/baz would only find the 'foo' node, |
262 | # not the foo and foo/bar nodes as it should. This does another |
263 | # single-level search to see if that's the case, and the 'last unless' |
264 | # should catch any failures - or short-circuit this if this *is* a |
265 | # bug in the visitor and gets fixed. |
266 | |
78d760bb |
267 | my $extra = $path[ ( scalar @match ) - 1 ]; |
cfd04b0c |
268 | last unless $extra; |
269 | $visitor->setSearchPath($extra); |
270 | $match[-1]->accept($visitor); |
78d760bb |
271 | push( @match, $visitor->getResult ) if defined $visitor->getResult; |
cfd04b0c |
272 | } |
273 | |
274 | return map { $_->getNodeValue } @match; |
275 | } |
276 | |
11bd4e3e |
277 | =item $self->set_action( $c, $action, $code, $class, $attrs ) |
1abd6db7 |
278 | |
279 | =cut |
280 | |
281 | sub set_action { |
11bd4e3e |
282 | my ( $self, $c, $method, $code, $class, $attrs ) = @_; |
1abd6db7 |
283 | |
11bd4e3e |
284 | my $namespace = |
285 | Catalyst::Utils::class2prefix( $class, $c->config->{case_sensitive} ) |
e494bd6b |
286 | || ''; |
b96f127f |
287 | my %attributes; |
1abd6db7 |
288 | |
289 | for my $attr ( @{$attrs} ) { |
22f3a8dd |
290 | |
291 | # Parse out :Foo(bar) into Foo => bar etc (and arrayify) |
292 | |
6d030e6f |
293 | my %initialized; |
294 | $initialized{ ref $_ }++ for @{ $self->dispatch_types }; |
295 | |
78d760bb |
296 | if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+)\s*\))?$/ ) ) { |
6d030e6f |
297 | |
298 | # Initialize types |
299 | my $class = "Catalyst::DispatchType::$key"; |
300 | unless ( $initialized{$class} ) { |
301 | eval "require $class"; |
302 | push( @{ $self->dispatch_types }, $class->new ) unless $@; |
303 | $initialized{$class}++; |
304 | } |
305 | |
b96f127f |
306 | if ( defined $value ) { |
78d760bb |
307 | ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ ); |
b96f127f |
308 | } |
78d760bb |
309 | push( @{ $attributes{$key} }, $value ); |
b96f127f |
310 | } |
1abd6db7 |
311 | } |
312 | |
6b239949 |
313 | if ( $attributes{Private} && ( keys %attributes > 1 ) ) { |
8d4e224b |
314 | $c->log->debug( 'Bad action definition "' |
d0e524d2 |
315 | . join( ' ', @{$attrs} ) |
11bd4e3e |
316 | . qq/" for "$class->$method"/ ) |
8d4e224b |
317 | if $c->debug; |
d0e524d2 |
318 | return; |
319 | } |
6b239949 |
320 | return unless keys %attributes; |
1abd6db7 |
321 | |
fbcc39ad |
322 | my $parent = $self->tree; |
1abd6db7 |
323 | my $visitor = Tree::Simple::Visitor::FindByPath->new; |
99fe1710 |
324 | |
11bd4e3e |
325 | if ($namespace) { |
326 | for my $part ( split '/', $namespace ) { |
1abd6db7 |
327 | $visitor->setSearchPath($part); |
328 | $parent->accept($visitor); |
b7aebc12 |
329 | my $child = $visitor->getResult; |
78d760bb |
330 | |
b7aebc12 |
331 | unless ($child) { |
90ce41ba |
332 | |
333 | # Create a new tree node and an ActionContainer to form |
334 | # its value. |
335 | |
78d760bb |
336 | my $container = |
337 | Catalyst::ActionContainer->new( |
338 | { part => $part, actions => {} } ); |
b7aebc12 |
339 | $child = $parent->addChild( Tree::Simple->new($container) ); |
340 | $visitor->setSearchPath($part); |
341 | $parent->accept($visitor); |
342 | $child = $visitor->getResult; |
343 | } |
78d760bb |
344 | |
b7aebc12 |
345 | $parent = $child; |
1abd6db7 |
346 | } |
1abd6db7 |
347 | } |
99fe1710 |
348 | |
11bd4e3e |
349 | my $reverse = $namespace ? "$namespace/$method" : $method; |
fbcc39ad |
350 | |
351 | my $action = Catalyst::Action->new( |
352 | { |
6b239949 |
353 | name => $method, |
b96f127f |
354 | code => $code, |
355 | reverse => $reverse, |
356 | namespace => $namespace, |
11bd4e3e |
357 | class => $class, |
b96f127f |
358 | attributes => \%attributes, |
fbcc39ad |
359 | } |
360 | ); |
361 | |
90ce41ba |
362 | # Set the method value |
b7aebc12 |
363 | $parent->getNodeValue->actions->{$method} = $action; |
fbcc39ad |
364 | |
22f3a8dd |
365 | # Pass the action to our dispatch types so they can register it if reqd. |
b96f127f |
366 | foreach my $type ( @{ $self->dispatch_types } ) { |
2633d7dc |
367 | $type->register( $c, $action ); |
b96f127f |
368 | } |
1abd6db7 |
369 | } |
370 | |
fbcc39ad |
371 | =item $self->setup_actions( $class, $component ) |
1abd6db7 |
372 | |
373 | =cut |
374 | |
375 | sub setup_actions { |
11bd4e3e |
376 | my ( $self, $c ) = @_; |
99fe1710 |
377 | |
6d030e6f |
378 | $self->dispatch_types( [] ); |
12e28165 |
379 | |
6d030e6f |
380 | # Preload action types |
381 | for my $type (@PRELOAD) { |
382 | my $class = "Catalyst::DispatchType::$type"; |
383 | eval "require $class"; |
384 | Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ ) |
385 | if $@; |
386 | push @{ $self->dispatch_types }, $class->new; |
387 | } |
b96f127f |
388 | |
12e28165 |
389 | # We use a tree |
78d760bb |
390 | my $container = |
391 | Catalyst::ActionContainer->new( { part => '/', actions => {} } ); |
b7aebc12 |
392 | $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) ); |
e494bd6b |
393 | |
11bd4e3e |
394 | for my $comp ( keys %{ $c->components } ) { |
e494bd6b |
395 | |
396 | # We only setup components that inherit from Catalyst::Base |
a268a011 |
397 | next unless $comp->isa('Catalyst::Base'); |
99fe1710 |
398 | |
812a28c9 |
399 | for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) { |
1abd6db7 |
400 | my ( $code, $attrs ) = @{$action}; |
401 | my $name = ''; |
402 | no strict 'refs'; |
403 | my @cache = ( $comp, @{"$comp\::ISA"} ); |
11bd4e3e |
404 | my %classes; |
99fe1710 |
405 | |
11bd4e3e |
406 | while ( my $class = shift @cache ) { |
407 | $classes{$class}++; |
ba599d1c |
408 | for my $isa ( @{"$class\::ISA"} ) { |
11bd4e3e |
409 | next if $classes{$isa}; |
1abd6db7 |
410 | push @cache, $isa; |
11bd4e3e |
411 | $classes{$isa}++; |
1abd6db7 |
412 | } |
413 | } |
99fe1710 |
414 | |
11bd4e3e |
415 | for my $class ( keys %classes ) { |
416 | for my $sym ( values %{ $class . '::' } ) { |
1abd6db7 |
417 | if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) { |
418 | $name = *{$sym}{NAME}; |
11bd4e3e |
419 | $self->set_action( $c, $name, $code, $comp, $attrs ); |
1abd6db7 |
420 | last; |
421 | } |
422 | } |
423 | } |
424 | } |
425 | } |
e494bd6b |
426 | |
2d1d8f91 |
427 | # Postload action types |
428 | for my $type (@POSTLOAD) { |
429 | my $class = "Catalyst::DispatchType::$type"; |
430 | eval "require $class"; |
431 | Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ ) |
432 | if $@; |
433 | push @{ $self->dispatch_types }, $class->new; |
434 | } |
6d030e6f |
435 | |
11bd4e3e |
436 | return unless $c->debug; |
99fe1710 |
437 | |
1abd6db7 |
438 | my $privates = Text::ASCIITable->new; |
5fbed090 |
439 | $privates->setCols( 'Private', 'Class' ); |
440 | $privates->setColWidth( 'Private', 36, 1 ); |
441 | $privates->setColWidth( 'Class', 37, 1 ); |
99fe1710 |
442 | |
1abd6db7 |
443 | my $walker = sub { |
444 | my ( $walker, $parent, $prefix ) = @_; |
445 | $prefix .= $parent->getNodeValue || ''; |
446 | $prefix .= '/' unless $prefix =~ /\/$/; |
b7aebc12 |
447 | my $node = $parent->getNodeValue->actions; |
99fe1710 |
448 | |
78d760bb |
449 | for my $action ( keys %{$node} ) { |
b7aebc12 |
450 | my $action_obj = $node->{$action}; |
11bd4e3e |
451 | $privates->addRow( "$prefix$action", $action_obj->class ); |
1abd6db7 |
452 | } |
99fe1710 |
453 | |
1abd6db7 |
454 | $walker->( $walker, $_, $prefix ) for $parent->getAllChildren; |
455 | }; |
99fe1710 |
456 | |
1abd6db7 |
457 | $walker->( $walker, $self->tree, '' ); |
11bd4e3e |
458 | $c->log->debug( "Loaded Private actions:\n" . $privates->draw ) |
a268a011 |
459 | if ( @{ $privates->{tbl_rows} } ); |
99fe1710 |
460 | |
a9cbd748 |
461 | # List all public actions |
11bd4e3e |
462 | $_->list($c) for @{ $self->dispatch_types }; |
1abd6db7 |
463 | } |
464 | |
1abd6db7 |
465 | =back |
466 | |
467 | =head1 AUTHOR |
468 | |
469 | Sebastian Riedel, C<sri@cpan.org> |
470 | |
471 | =head1 COPYRIGHT |
472 | |
473 | This program is free software, you can redistribute it and/or modify it under |
474 | the same terms as Perl itself. |
475 | |
476 | =cut |
477 | |
478 | 1; |