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