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