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