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; |
87b85407 |
11 | use Text::SimpleTable; |
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 | |
49070d25 |
18 | __PACKAGE__->mk_accessors( |
19 | qw/tree dispatch_types registered_dispatch_types |
01ce0928 |
20 | method_action_class action_container_class/ |
49070d25 |
21 | ); |
6d030e6f |
22 | |
23 | # Preload these action types |
24 | our @PRELOAD = qw/Path Regex/; |
1abd6db7 |
25 | |
2d1d8f91 |
26 | # Postload these action types |
27 | our @POSTLOAD = qw/Index Default/; |
28 | |
1abd6db7 |
29 | =head1 NAME |
30 | |
9c053379 |
31 | Catalyst::Dispatcher - The Catalyst Dispatcher |
1abd6db7 |
32 | |
33 | =head1 SYNOPSIS |
34 | |
35 | See L<Catalyst>. |
36 | |
37 | =head1 DESCRIPTION |
38 | |
39 | =head1 METHODS |
40 | |
b5ecfcf0 |
41 | =head2 $self->detach( $c, $command [, \@arguments ] ) |
6ef62eb2 |
42 | |
43 | =cut |
44 | |
45 | sub detach { |
fbcc39ad |
46 | my ( $self, $c, $command, @args ) = @_; |
bd7d2e94 |
47 | $c->forward( $command, @args ) if $command; |
fbcc39ad |
48 | die $Catalyst::DETACH; |
6ef62eb2 |
49 | } |
50 | |
b5ecfcf0 |
51 | =head2 $self->dispatch($c) |
1abd6db7 |
52 | |
53 | =cut |
54 | |
55 | sub dispatch { |
fbcc39ad |
56 | my ( $self, $c ) = @_; |
66e28e3f |
57 | if ( $c->action ) { |
28591cd7 |
58 | $c->forward( join( '/', '', $c->action->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 | |
b5ecfcf0 |
71 | =head2 $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 | |
138ce4c0 |
85 | my $local_args = 0; |
6d12f1d4 |
86 | my $arguments = $c->req->args; |
138ce4c0 |
87 | if ( ref( $_[-1] ) eq 'ARRAY' ) { |
6d12f1d4 |
88 | $arguments = pop(@_); |
89 | $local_args = 1; |
138ce4c0 |
90 | } |
99fe1710 |
91 | |
a9dc674c |
92 | my $result; |
fbcc39ad |
93 | |
16aa17e9 |
94 | unless ( ref $command ) { |
95 | my $command_copy = $command; |
8199eac3 |
96 | |
16aa17e9 |
97 | unless ( $command_copy =~ s/^\/// ) { |
46245bee |
98 | my $namespace = $c->stack->[-1]->namespace; |
99 | $command_copy = "${namespace}/${command}"; |
16aa17e9 |
100 | } |
99fe1710 |
101 | |
16aa17e9 |
102 | unless ( $command_copy =~ /\// ) { |
103 | $result = $c->get_action( $command_copy, '/' ); |
104 | } |
105 | else { |
106 | my @extra_args; |
107 | DESCEND: while ( $command_copy =~ s/^(.*)\/(\w+)$/$1/ ) { |
108 | my $tail = $2; |
109 | $result = $c->get_action( $tail, $1 ); |
110 | if ($result) { |
6d12f1d4 |
111 | $local_args = 1; |
112 | $command = $tail; |
d3f21b2f |
113 | unshift( @{$arguments}, @extra_args ); |
16aa17e9 |
114 | last DESCEND; |
115 | } |
116 | unshift( @extra_args, $tail ); |
8199eac3 |
117 | } |
8199eac3 |
118 | } |
e494bd6b |
119 | } |
99fe1710 |
120 | |
49070d25 |
121 | unless ($result) { |
bd7d2e94 |
122 | |
f3b3f450 |
123 | my $class = ref($command) || ref( $c->component($command) ); |
124 | my $method = shift || 'process'; |
d6e0d7e6 |
125 | |
f3b3f450 |
126 | unless ($class) { |
bd7d2e94 |
127 | my $error = |
128 | qq/Couldn't forward to command "$command". Invalid action or component./; |
3b2ed580 |
129 | $c->error($error); |
130 | $c->log->debug($error) if $c->debug; |
1abd6db7 |
131 | return 0; |
132 | } |
bd7d2e94 |
133 | |
d6e0d7e6 |
134 | if ( my $code = $class->can($method) ) { |
97d6d2bd |
135 | my $action = $self->method_action_class->new( |
fbcc39ad |
136 | { |
6b239949 |
137 | name => $method, |
fbcc39ad |
138 | code => $code, |
139 | reverse => "$class->$method", |
11bd4e3e |
140 | class => $class, |
46245bee |
141 | namespace => Catalyst::Utils::class2prefix( |
142 | $class, $c->config->{case_sensitive} |
143 | ), |
fbcc39ad |
144 | } |
145 | ); |
a9dc674c |
146 | $result = $action; |
fbcc39ad |
147 | } |
148 | |
149 | else { |
bd7d2e94 |
150 | my $error = |
151 | qq/Couldn't forward to "$class". Does not implement "$method"/; |
3b2ed580 |
152 | $c->error($error); |
153 | $c->log->debug($error) |
1abd6db7 |
154 | if $c->debug; |
155 | return 0; |
156 | } |
99fe1710 |
157 | |
1abd6db7 |
158 | } |
bd7d2e94 |
159 | |
6d12f1d4 |
160 | if ($local_args) { |
161 | local $c->request->{arguments} = [ @{$arguments} ]; |
162 | $result->execute($c); |
163 | } |
164 | else { $result->execute($c) } |
99fe1710 |
165 | |
1abd6db7 |
166 | return $c->state; |
167 | } |
168 | |
b5ecfcf0 |
169 | =head2 $self->prepare_action($c) |
fbcc39ad |
170 | |
171 | =cut |
172 | |
173 | sub prepare_action { |
174 | my ( $self, $c ) = @_; |
175 | my $path = $c->req->path; |
176 | my @path = split /\//, $c->req->path; |
177 | $c->req->args( \my @args ); |
178 | |
78d760bb |
179 | push( @path, '/' ) unless @path; # Root action |
180 | |
b96f127f |
181 | DESCEND: while (@path) { |
fbcc39ad |
182 | $path = join '/', @path; |
fbcc39ad |
183 | |
78d760bb |
184 | $path = '' if $path eq '/'; # Root action |
185 | |
22f3a8dd |
186 | # Check out dispatch types to see if any will handle the path at |
187 | # this level |
188 | |
78d760bb |
189 | foreach my $type ( @{ $self->dispatch_types } ) { |
2633d7dc |
190 | last DESCEND if $type->match( $c, $path ); |
66e28e3f |
191 | } |
b96f127f |
192 | |
22f3a8dd |
193 | # If not, move the last part path to args |
b96f127f |
194 | unshift @args, pop @path; |
fbcc39ad |
195 | } |
196 | |
e3a13771 |
197 | $c->log->debug( 'Path is "' . $c->req->match . '"' ) |
198 | if ( $c->debug && $c->req->match ); |
199 | |
fbcc39ad |
200 | $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' ) |
201 | if ( $c->debug && @args ); |
202 | } |
203 | |
b5ecfcf0 |
204 | =head2 $self->get_action( $action, $namespace ) |
1abd6db7 |
205 | |
206 | =cut |
207 | |
208 | sub get_action { |
bcd1002b |
209 | my ( $self, $name, $namespace ) = @_; |
79a3189a |
210 | return unless $name; |
bcccee4e |
211 | $namespace ||= ''; |
772ab8ae |
212 | $namespace = '' if $namespace eq '/'; |
99fe1710 |
213 | |
78d760bb |
214 | my @match = $self->get_containers($namespace); |
c8d9780f |
215 | |
79a3189a |
216 | return unless @match; |
772ab8ae |
217 | |
684d10ed |
218 | if ( my $action = $match[-1]->get_action($name) ) { |
79a3189a |
219 | return $action if $action->namespace eq $namespace; |
1abd6db7 |
220 | } |
1abd6db7 |
221 | } |
222 | |
b5ecfcf0 |
223 | =head2 $self->get_actions( $c, $action, $namespace ) |
a9dc674c |
224 | |
225 | =cut |
226 | |
227 | sub get_actions { |
228 | my ( $self, $c, $action, $namespace ) = @_; |
229 | return [] unless $action; |
230 | $namespace ||= ''; |
231 | $namespace = '' if $namespace eq '/'; |
232 | |
233 | my @match = $self->get_containers($namespace); |
234 | |
684d10ed |
235 | return map { $_->get_action($action) } @match; |
a9dc674c |
236 | } |
237 | |
b5ecfcf0 |
238 | =head2 $self->get_containers( $namespace ) |
cfd04b0c |
239 | |
240 | =cut |
241 | |
242 | sub get_containers { |
243 | my ( $self, $namespace ) = @_; |
244 | |
90ce41ba |
245 | # If the namespace is / just return the root ActionContainer |
246 | |
78d760bb |
247 | return ( $self->tree->getNodeValue ) |
248 | if ( !$namespace || ( $namespace eq '/' ) ); |
cfd04b0c |
249 | |
90ce41ba |
250 | # Use a visitor to recurse down the tree finding the ActionContainers |
251 | # for each namespace in the chain. |
252 | |
cfd04b0c |
253 | my $visitor = Tree::Simple::Visitor::FindByPath->new; |
78d760bb |
254 | my @path = split( '/', $namespace ); |
255 | $visitor->setSearchPath(@path); |
cfd04b0c |
256 | $self->tree->accept($visitor); |
257 | |
258 | my @match = $visitor->getResults; |
78d760bb |
259 | @match = ( $self->tree ) unless @match; |
cfd04b0c |
260 | |
78d760bb |
261 | if ( !defined $visitor->getResult ) { |
90ce41ba |
262 | |
263 | # If we don't manage to match, the visitor doesn't return the last |
264 | # node is matched, so foo/bar/baz would only find the 'foo' node, |
265 | # not the foo and foo/bar nodes as it should. This does another |
266 | # single-level search to see if that's the case, and the 'last unless' |
267 | # should catch any failures - or short-circuit this if this *is* a |
268 | # bug in the visitor and gets fixed. |
269 | |
f3b3f450 |
270 | if ( my $extra = $path[ ( scalar @match ) - 1 ] ) { |
540966c1 |
271 | $visitor->setSearchPath($extra); |
272 | $match[-1]->accept($visitor); |
273 | push( @match, $visitor->getResult ) if defined $visitor->getResult; |
274 | } |
cfd04b0c |
275 | } |
276 | |
277 | return map { $_->getNodeValue } @match; |
278 | } |
279 | |
b5ecfcf0 |
280 | =head2 $self->register( $c, $action ) |
aad72cc9 |
281 | |
282 | =cut |
283 | |
79a3189a |
284 | sub register { |
285 | my ( $self, $c, $action ) = @_; |
286 | |
694d15f1 |
287 | my $registered = $self->registered_dispatch_types; |
288 | |
289 | my $priv = 0; |
290 | foreach my $key ( keys %{ $action->attributes } ) { |
291 | $priv++ if $key eq 'Private'; |
292 | my $class = "Catalyst::DispatchType::$key"; |
293 | unless ( $registered->{$class} ) { |
294 | eval "require $class"; |
295 | push( @{ $self->dispatch_types }, $class->new ) unless $@; |
296 | $registered->{$class} = 1; |
297 | } |
298 | } |
299 | |
300 | # Pass the action to our dispatch types so they can register it if reqd. |
01ce0928 |
301 | my $reg = 0; |
302 | foreach my $type ( @{ $self->dispatch_types } ) { |
303 | $reg++ if $type->register( $c, $action ); |
694d15f1 |
304 | } |
305 | |
306 | return unless $reg + $priv; |
307 | |
79a3189a |
308 | my $namespace = $action->namespace; |
309 | my $parent = $self->tree; |
310 | my $visitor = Tree::Simple::Visitor::FindByPath->new; |
99fe1710 |
311 | |
11bd4e3e |
312 | if ($namespace) { |
313 | for my $part ( split '/', $namespace ) { |
1abd6db7 |
314 | $visitor->setSearchPath($part); |
315 | $parent->accept($visitor); |
b7aebc12 |
316 | my $child = $visitor->getResult; |
78d760bb |
317 | |
b7aebc12 |
318 | unless ($child) { |
90ce41ba |
319 | |
320 | # Create a new tree node and an ActionContainer to form |
321 | # its value. |
322 | |
78d760bb |
323 | my $container = |
324 | Catalyst::ActionContainer->new( |
325 | { part => $part, actions => {} } ); |
b7aebc12 |
326 | $child = $parent->addChild( Tree::Simple->new($container) ); |
327 | $visitor->setSearchPath($part); |
328 | $parent->accept($visitor); |
329 | $child = $visitor->getResult; |
330 | } |
78d760bb |
331 | |
b7aebc12 |
332 | $parent = $child; |
1abd6db7 |
333 | } |
1abd6db7 |
334 | } |
99fe1710 |
335 | |
90ce41ba |
336 | # Set the method value |
49070d25 |
337 | $parent->getNodeValue->actions->{ $action->name } = $action; |
1abd6db7 |
338 | } |
339 | |
b5ecfcf0 |
340 | =head2 $self->setup_actions( $class, $component ) |
1abd6db7 |
341 | |
342 | =cut |
343 | |
344 | sub setup_actions { |
11bd4e3e |
345 | my ( $self, $c ) = @_; |
99fe1710 |
346 | |
6d030e6f |
347 | $self->dispatch_types( [] ); |
91d4abc5 |
348 | $self->registered_dispatch_types( {} ); |
49070d25 |
349 | $self->method_action_class('Catalyst::Action'); |
350 | $self->action_container_class('Catalyst::ActionContainer'); |
12e28165 |
351 | |
6d030e6f |
352 | # Preload action types |
353 | for my $type (@PRELOAD) { |
354 | my $class = "Catalyst::DispatchType::$type"; |
355 | eval "require $class"; |
356 | Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ ) |
357 | if $@; |
358 | push @{ $self->dispatch_types }, $class->new; |
91d4abc5 |
359 | $self->registered_dispatch_types->{$class} = 1; |
6d030e6f |
360 | } |
b96f127f |
361 | |
12e28165 |
362 | # We use a tree |
78d760bb |
363 | my $container = |
364 | Catalyst::ActionContainer->new( { part => '/', actions => {} } ); |
b7aebc12 |
365 | $self->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) ); |
e494bd6b |
366 | |
49070d25 |
367 | foreach my $comp ( values %{ $c->components } ) { |
368 | $comp->register_actions($c) if $comp->can('register_actions'); |
1abd6db7 |
369 | } |
e494bd6b |
370 | |
2d1d8f91 |
371 | # Postload action types |
372 | for my $type (@POSTLOAD) { |
373 | my $class = "Catalyst::DispatchType::$type"; |
374 | eval "require $class"; |
375 | Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ ) |
376 | if $@; |
377 | push @{ $self->dispatch_types }, $class->new; |
378 | } |
6d030e6f |
379 | |
11bd4e3e |
380 | return unless $c->debug; |
99fe1710 |
381 | |
684d10ed |
382 | my $privates = Text::SimpleTable->new( |
dbf03873 |
383 | [ 20, 'Private' ], |
384 | [ 38, 'Class' ], |
385 | [ 12, 'Method' ] |
684d10ed |
386 | ); |
99fe1710 |
387 | |
87b85407 |
388 | my $has_private = 0; |
1abd6db7 |
389 | my $walker = sub { |
390 | my ( $walker, $parent, $prefix ) = @_; |
391 | $prefix .= $parent->getNodeValue || ''; |
392 | $prefix .= '/' unless $prefix =~ /\/$/; |
b7aebc12 |
393 | my $node = $parent->getNodeValue->actions; |
99fe1710 |
394 | |
78d760bb |
395 | for my $action ( keys %{$node} ) { |
b7aebc12 |
396 | my $action_obj = $node->{$action}; |
b0bb11ec |
397 | next |
398 | if ( ( $action =~ /^_.*/ ) |
399 | && ( !$c->config->{show_internal_actions} ) ); |
684d10ed |
400 | $privates->row( "$prefix$action", $action_obj->class, $action ); |
87b85407 |
401 | $has_private = 1; |
1abd6db7 |
402 | } |
99fe1710 |
403 | |
1abd6db7 |
404 | $walker->( $walker, $_, $prefix ) for $parent->getAllChildren; |
405 | }; |
99fe1710 |
406 | |
1abd6db7 |
407 | $walker->( $walker, $self->tree, '' ); |
11bd4e3e |
408 | $c->log->debug( "Loaded Private actions:\n" . $privates->draw ) |
49070d25 |
409 | if ($has_private); |
99fe1710 |
410 | |
a9cbd748 |
411 | # List all public actions |
11bd4e3e |
412 | $_->list($c) for @{ $self->dispatch_types }; |
1abd6db7 |
413 | } |
414 | |
1abd6db7 |
415 | =head1 AUTHOR |
416 | |
417 | Sebastian Riedel, C<sri@cpan.org> |
158c88c0 |
418 | Matt S Trout, C<mst@shadowcatsystems.co.uk> |
1abd6db7 |
419 | |
420 | =head1 COPYRIGHT |
421 | |
422 | This program is free software, you can redistribute it and/or modify it under |
423 | the same terms as Perl itself. |
424 | |
425 | =cut |
426 | |
427 | 1; |