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 |
e7bb8d33 |
20 | method_action_class action_container_class |
21 | preload_dispatch_types postload_dispatch_types |
a13e21ab |
22 | action_hash container_hash |
9e81ba44 |
23 | / |
49070d25 |
24 | ); |
6d030e6f |
25 | |
26 | # Preload these action types |
61a9002d |
27 | our @PRELOAD = qw/Index Path Regex/; |
1abd6db7 |
28 | |
2d1d8f91 |
29 | # Postload these action types |
61a9002d |
30 | our @POSTLOAD = qw/Default/; |
2d1d8f91 |
31 | |
1abd6db7 |
32 | =head1 NAME |
33 | |
9c053379 |
34 | Catalyst::Dispatcher - The Catalyst Dispatcher |
1abd6db7 |
35 | |
36 | =head1 SYNOPSIS |
37 | |
38 | See L<Catalyst>. |
39 | |
40 | =head1 DESCRIPTION |
41 | |
4ab87e27 |
42 | This is the class that maps public urls to actions in your Catalyst |
43 | application based on the attributes you set. |
44 | |
1abd6db7 |
45 | =head1 METHODS |
46 | |
76ddf86b |
47 | =head2 new |
4ab87e27 |
48 | |
49 | Construct a new dispatcher. |
50 | |
e7bb8d33 |
51 | =cut |
52 | |
53 | sub new { |
9e81ba44 |
54 | my $self = shift; |
e7bb8d33 |
55 | my $class = ref($self) || $self; |
9e81ba44 |
56 | |
57 | my $obj = $class->SUPER::new(@_); |
58 | |
e7bb8d33 |
59 | # set the default pre- and and postloads |
9e81ba44 |
60 | $obj->preload_dispatch_types( \@PRELOAD ); |
e7bb8d33 |
61 | $obj->postload_dispatch_types( \@POSTLOAD ); |
a13e21ab |
62 | $obj->action_hash( {} ); |
63 | $obj->container_hash( {} ); |
64 | |
65 | # Create the root node of the tree |
66 | my $container = |
67 | Catalyst::ActionContainer->new( { part => '/', actions => {} } ); |
68 | $obj->tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) ); |
69 | |
9e81ba44 |
70 | return $obj; |
e7bb8d33 |
71 | } |
72 | |
73 | =head2 $self->preload_dispatch_types |
74 | |
75 | An arrayref of pre-loaded dispatchtype classes |
76 | |
77 | Entries are considered to be available as C<Catalyst::DispatchType::CLASS> |
78 | To use a custom class outside the regular C<Catalyst> namespace, prefix |
79 | it with a C<+>, like so: |
80 | |
81 | +My::Dispatch::Type |
82 | |
83 | =head2 $self->postload_dispatch_types |
84 | |
85 | An arrayref of post-loaded dispatchtype classes |
86 | |
87 | Entries are considered to be available as C<Catalyst::DispatchType::CLASS> |
88 | To use a custom class outside the regular C<Catalyst> namespace, prefix |
89 | it with a C<+>, like so: |
90 | |
91 | +My::Dispatch::Type |
92 | |
b5ecfcf0 |
93 | =head2 $self->detach( $c, $command [, \@arguments ] ) |
6ef62eb2 |
94 | |
4ab87e27 |
95 | Documented in L<Catalyst> |
96 | |
6ef62eb2 |
97 | =cut |
98 | |
99 | sub detach { |
fbcc39ad |
100 | my ( $self, $c, $command, @args ) = @_; |
bd7d2e94 |
101 | $c->forward( $command, @args ) if $command; |
fbcc39ad |
102 | die $Catalyst::DETACH; |
6ef62eb2 |
103 | } |
104 | |
b5ecfcf0 |
105 | =head2 $self->dispatch($c) |
1abd6db7 |
106 | |
4ab87e27 |
107 | Delegate the dispatch to the action that matched the url, or return a |
108 | message about unknown resource |
109 | |
110 | |
1abd6db7 |
111 | =cut |
112 | |
113 | sub dispatch { |
fbcc39ad |
114 | my ( $self, $c ) = @_; |
66e28e3f |
115 | if ( $c->action ) { |
28591cd7 |
116 | $c->forward( join( '/', '', $c->action->namespace, '_DISPATCH' ) ); |
fbcc39ad |
117 | } |
118 | |
119 | else { |
1abd6db7 |
120 | my $path = $c->req->path; |
121 | my $error = $path |
122 | ? qq/Unknown resource "$path"/ |
123 | : "No default action defined"; |
124 | $c->log->error($error) if $c->debug; |
125 | $c->error($error); |
126 | } |
127 | } |
128 | |
b5ecfcf0 |
129 | =head2 $self->forward( $c, $command [, \@arguments ] ) |
1abd6db7 |
130 | |
4ab87e27 |
131 | Documented in L<Catalyst> |
132 | |
1abd6db7 |
133 | =cut |
134 | |
135 | sub forward { |
e540158b |
136 | my ( $self, $c, $command ) = splice( @_, 0, 3 ); |
99fe1710 |
137 | |
1abd6db7 |
138 | unless ($command) { |
139 | $c->log->debug('Nothing to forward to') if $c->debug; |
140 | return 0; |
141 | } |
99fe1710 |
142 | |
e540158b |
143 | my $args = [ @{ $c->request->arguments } ]; |
99fe1710 |
144 | |
e540158b |
145 | @$args = @{ pop @_ } if ( ref( $_[-1] ) eq 'ARRAY' ); |
99fe1710 |
146 | |
adb53907 |
147 | my $action = $self->_invoke_as_path( $c, $command, $args ) |
e540158b |
148 | || $self->_invoke_as_component( $c, $command, shift ); |
99fe1710 |
149 | |
e540158b |
150 | unless ($action) { |
151 | my $error = |
152 | qq/Couldn't forward to command "$command": / |
153 | . qq/Invalid action or component./; |
154 | $c->error($error); |
155 | $c->log->debug($error) if $c->debug; |
156 | return 0; |
157 | } |
bd7d2e94 |
158 | |
e540158b |
159 | #push @$args, @_; |
adb53907 |
160 | |
e540158b |
161 | local $c->request->{arguments} = $args; |
162 | $action->execute($c); |
99fe1710 |
163 | |
1abd6db7 |
164 | return $c->state; |
165 | } |
166 | |
adb53907 |
167 | sub _action_rel2abs { |
e540158b |
168 | my ( $self, $c, $path ) = @_; |
169 | |
170 | unless ( $path =~ m#^/# ) { |
171 | my $namespace = $c->stack->[-1]->namespace; |
172 | $path = "$namespace/$path"; |
173 | } |
174 | |
175 | $path =~ s#^/##; |
176 | return $path; |
adb53907 |
177 | } |
178 | |
179 | sub _invoke_as_path { |
e540158b |
180 | my ( $self, $c, $rel_path, $args ) = @_; |
181 | |
182 | return if ref $rel_path; # it must be a string |
183 | |
184 | my $path = $self->_action_rel2abs( $c, $rel_path ); |
185 | |
186 | my ( $tail, @extra_args ); |
187 | while ( ( $path, $tail ) = ( $path =~ m#^(?:(.*)/)?(\w+)?$# ) ) |
188 | { # allow $path to be empty |
189 | if ( my $action = $c->get_action( $tail, $path ) ) { |
190 | push @$args, @extra_args; |
191 | return $action; |
192 | } |
193 | else { |
194 | return |
195 | unless $path |
196 | ; # if a match on the global namespace failed then the whole lookup failed |
197 | } |
198 | |
199 | unshift @extra_args, $tail; |
200 | } |
adb53907 |
201 | } |
202 | |
203 | sub _find_component_class { |
e540158b |
204 | my ( $self, $c, $component ) = @_; |
adb53907 |
205 | |
e540158b |
206 | return ref($component) |
207 | || ref( $c->component($component) ) |
208 | || $c->component($component); |
adb53907 |
209 | } |
210 | |
211 | sub _invoke_as_component { |
e540158b |
212 | my ( $self, $c, $component, $method ) = @_; |
213 | |
214 | my $class = $self->_find_component_class( $c, $component ) || return 0; |
215 | $method ||= "process"; |
216 | |
217 | if ( my $code = $class->can($method) ) { |
218 | return $self->method_action_class->new( |
219 | { |
220 | name => $method, |
221 | code => $code, |
222 | reverse => "$class->$method", |
223 | class => $class, |
224 | namespace => Catalyst::Utils::class2prefix( |
225 | $class, $c->config->{case_sensitive} |
226 | ), |
227 | } |
228 | ); |
229 | } |
230 | else { |
231 | my $error = |
232 | qq/Couldn't forward to "$class". Does not implement "$method"/; |
233 | $c->error($error); |
234 | $c->log->debug($error) |
235 | if $c->debug; |
236 | return 0; |
237 | } |
adb53907 |
238 | } |
239 | |
b5ecfcf0 |
240 | =head2 $self->prepare_action($c) |
fbcc39ad |
241 | |
4ab87e27 |
242 | Find an dispatch type that matches $c->req->path, and set args from it. |
243 | |
fbcc39ad |
244 | =cut |
245 | |
246 | sub prepare_action { |
247 | my ( $self, $c ) = @_; |
248 | my $path = $c->req->path; |
249 | my @path = split /\//, $c->req->path; |
250 | $c->req->args( \my @args ); |
251 | |
61a9002d |
252 | unshift( @path, '' ); # Root action |
78d760bb |
253 | |
b96f127f |
254 | DESCEND: while (@path) { |
fbcc39ad |
255 | $path = join '/', @path; |
61a9002d |
256 | $path =~ s#^/##; |
fbcc39ad |
257 | |
61a9002d |
258 | $path = '' if $path eq '/'; # Root action |
78d760bb |
259 | |
22f3a8dd |
260 | # Check out dispatch types to see if any will handle the path at |
261 | # this level |
262 | |
78d760bb |
263 | foreach my $type ( @{ $self->dispatch_types } ) { |
2633d7dc |
264 | last DESCEND if $type->match( $c, $path ); |
66e28e3f |
265 | } |
b96f127f |
266 | |
22f3a8dd |
267 | # If not, move the last part path to args |
4082e678 |
268 | my $arg = pop(@path); |
269 | $arg =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; |
270 | unshift @args, $arg; |
fbcc39ad |
271 | } |
272 | |
e3a13771 |
273 | $c->log->debug( 'Path is "' . $c->req->match . '"' ) |
274 | if ( $c->debug && $c->req->match ); |
275 | |
fbcc39ad |
276 | $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' ) |
277 | if ( $c->debug && @args ); |
278 | } |
279 | |
b5ecfcf0 |
280 | =head2 $self->get_action( $action, $namespace ) |
1abd6db7 |
281 | |
4ab87e27 |
282 | returns a named action from a given namespace. |
283 | |
1abd6db7 |
284 | =cut |
285 | |
286 | sub get_action { |
bcd1002b |
287 | my ( $self, $name, $namespace ) = @_; |
79a3189a |
288 | return unless $name; |
bcccee4e |
289 | $namespace ||= ''; |
772ab8ae |
290 | $namespace = '' if $namespace eq '/'; |
99fe1710 |
291 | |
a13e21ab |
292 | return $self->action_hash->{"$namespace/$name"}; |
1abd6db7 |
293 | } |
294 | |
b5ecfcf0 |
295 | =head2 $self->get_actions( $c, $action, $namespace ) |
a9dc674c |
296 | |
297 | =cut |
298 | |
299 | sub get_actions { |
300 | my ( $self, $c, $action, $namespace ) = @_; |
301 | return [] unless $action; |
302 | $namespace ||= ''; |
303 | $namespace = '' if $namespace eq '/'; |
304 | |
305 | my @match = $self->get_containers($namespace); |
306 | |
684d10ed |
307 | return map { $_->get_action($action) } @match; |
a9dc674c |
308 | } |
309 | |
b5ecfcf0 |
310 | =head2 $self->get_containers( $namespace ) |
cfd04b0c |
311 | |
4ab87e27 |
312 | Return all the action containers for a given namespace, inclusive |
313 | |
cfd04b0c |
314 | =cut |
315 | |
316 | sub get_containers { |
317 | my ( $self, $namespace ) = @_; |
a13e21ab |
318 | $namespace ||= ''; |
319 | $namespace = '' if $namespace eq '/'; |
cfd04b0c |
320 | |
a13e21ab |
321 | my @containers; |
cfd04b0c |
322 | |
a13e21ab |
323 | do { |
324 | push @containers, $self->container_hash->{$namespace}; |
325 | } while ( $namespace =~ s#/[^/]+$## ); |
90ce41ba |
326 | |
a13e21ab |
327 | return reverse grep { defined } @containers, $self->container_hash->{''}; |
90ce41ba |
328 | |
a13e21ab |
329 | my @parts = split '/', $namespace; |
cfd04b0c |
330 | } |
331 | |
b5ecfcf0 |
332 | =head2 $self->register( $c, $action ) |
aad72cc9 |
333 | |
4ab87e27 |
334 | Make sure all required dispatch types for this action are loaded, then |
335 | pass the action to our dispatch types so they can register it if required. |
336 | Also, set up the tree with the action containers. |
337 | |
aad72cc9 |
338 | =cut |
339 | |
79a3189a |
340 | sub register { |
341 | my ( $self, $c, $action ) = @_; |
342 | |
694d15f1 |
343 | my $registered = $self->registered_dispatch_types; |
344 | |
345 | my $priv = 0; |
346 | foreach my $key ( keys %{ $action->attributes } ) { |
347 | $priv++ if $key eq 'Private'; |
348 | my $class = "Catalyst::DispatchType::$key"; |
349 | unless ( $registered->{$class} ) { |
350 | eval "require $class"; |
351 | push( @{ $self->dispatch_types }, $class->new ) unless $@; |
352 | $registered->{$class} = 1; |
353 | } |
354 | } |
355 | |
356 | # Pass the action to our dispatch types so they can register it if reqd. |
01ce0928 |
357 | my $reg = 0; |
358 | foreach my $type ( @{ $self->dispatch_types } ) { |
359 | $reg++ if $type->register( $c, $action ); |
694d15f1 |
360 | } |
361 | |
362 | return unless $reg + $priv; |
363 | |
79a3189a |
364 | my $namespace = $action->namespace; |
a13e21ab |
365 | my $name = $action->name; |
c7116517 |
366 | |
a13e21ab |
367 | my $container = $self->find_or_create_action_container($namespace); |
15e9b5dd |
368 | |
369 | # Set the method value |
a13e21ab |
370 | $container->add_action($action); |
c7116517 |
371 | |
a13e21ab |
372 | $self->action_hash->{"$namespace/$name"} = $action; |
373 | $self->container_hash->{$namespace} = $container; |
15e9b5dd |
374 | } |
375 | |
a13e21ab |
376 | sub find_or_create_action_container { |
377 | my ( $self, $namespace ) = @_; |
378 | |
379 | my $tree ||= $self->tree; |
99fe1710 |
380 | |
a13e21ab |
381 | return $tree->getNodeValue unless $namespace; |
78d760bb |
382 | |
a13e21ab |
383 | my @namespace = split '/', $namespace; |
384 | return $self->_find_or_create_namespace_node( $tree, @namespace ) |
385 | ->getNodeValue; |
8505565b |
386 | } |
90ce41ba |
387 | |
8505565b |
388 | sub _find_or_create_namespace_node { |
a13e21ab |
389 | my ( $self, $parent, $part, @namespace ) = @_; |
78d760bb |
390 | |
a13e21ab |
391 | return $parent unless $part; |
8505565b |
392 | |
a13e21ab |
393 | my $child = |
394 | ( grep { $_->getNodeValue->part eq $part } $parent->getAllChildren )[0]; |
8505565b |
395 | |
a13e21ab |
396 | unless ($child) { |
397 | my $container = Catalyst::ActionContainer->new($part); |
398 | $parent->addChild( $child = Tree::Simple->new($container) ); |
399 | } |
99fe1710 |
400 | |
a13e21ab |
401 | $self->_find_or_create_namespace_node( $child, @namespace ); |
1abd6db7 |
402 | } |
403 | |
4ab87e27 |
404 | =head2 $self->setup_actions( $class, $context ) |
405 | |
1abd6db7 |
406 | |
407 | =cut |
408 | |
409 | sub setup_actions { |
11bd4e3e |
410 | my ( $self, $c ) = @_; |
99fe1710 |
411 | |
6d030e6f |
412 | $self->dispatch_types( [] ); |
91d4abc5 |
413 | $self->registered_dispatch_types( {} ); |
49070d25 |
414 | $self->method_action_class('Catalyst::Action'); |
415 | $self->action_container_class('Catalyst::ActionContainer'); |
12e28165 |
416 | |
9e81ba44 |
417 | my @classes = |
418 | $self->do_load_dispatch_types( @{ $self->preload_dispatch_types } ); |
419 | @{ $self->registered_dispatch_types }{@classes} = (1) x @classes; |
b96f127f |
420 | |
49070d25 |
421 | foreach my $comp ( values %{ $c->components } ) { |
422 | $comp->register_actions($c) if $comp->can('register_actions'); |
1abd6db7 |
423 | } |
e494bd6b |
424 | |
9e81ba44 |
425 | $self->do_load_dispatch_types( @{ $self->postload_dispatch_types } ); |
6d030e6f |
426 | |
11bd4e3e |
427 | return unless $c->debug; |
99fe1710 |
428 | |
684d10ed |
429 | my $privates = Text::SimpleTable->new( |
dbf03873 |
430 | [ 20, 'Private' ], |
431 | [ 38, 'Class' ], |
432 | [ 12, 'Method' ] |
684d10ed |
433 | ); |
99fe1710 |
434 | |
87b85407 |
435 | my $has_private = 0; |
1abd6db7 |
436 | my $walker = sub { |
437 | my ( $walker, $parent, $prefix ) = @_; |
438 | $prefix .= $parent->getNodeValue || ''; |
439 | $prefix .= '/' unless $prefix =~ /\/$/; |
b7aebc12 |
440 | my $node = $parent->getNodeValue->actions; |
99fe1710 |
441 | |
78d760bb |
442 | for my $action ( keys %{$node} ) { |
b7aebc12 |
443 | my $action_obj = $node->{$action}; |
b0bb11ec |
444 | next |
445 | if ( ( $action =~ /^_.*/ ) |
446 | && ( !$c->config->{show_internal_actions} ) ); |
684d10ed |
447 | $privates->row( "$prefix$action", $action_obj->class, $action ); |
87b85407 |
448 | $has_private = 1; |
1abd6db7 |
449 | } |
99fe1710 |
450 | |
1abd6db7 |
451 | $walker->( $walker, $_, $prefix ) for $parent->getAllChildren; |
452 | }; |
99fe1710 |
453 | |
1abd6db7 |
454 | $walker->( $walker, $self->tree, '' ); |
11bd4e3e |
455 | $c->log->debug( "Loaded Private actions:\n" . $privates->draw ) |
49070d25 |
456 | if ($has_private); |
99fe1710 |
457 | |
a9cbd748 |
458 | # List all public actions |
11bd4e3e |
459 | $_->list($c) for @{ $self->dispatch_types }; |
1abd6db7 |
460 | } |
461 | |
9e81ba44 |
462 | sub do_load_dispatch_types { |
463 | my ( $self, @types ) = @_; |
464 | |
465 | my @loaded; |
466 | |
467 | # Preload action types |
468 | for my $type (@types) { |
469 | my $class = |
470 | ( $type =~ /^\+(.*)$/ ) ? $1 : "Catalyst::DispatchType::${type}"; |
471 | eval "require $class"; |
472 | Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ ) |
473 | if $@; |
474 | push @{ $self->dispatch_types }, $class->new; |
475 | |
476 | push @loaded, $class; |
477 | } |
478 | |
a13e21ab |
479 | return @loaded; |
9e81ba44 |
480 | } |
481 | |
1abd6db7 |
482 | =head1 AUTHOR |
483 | |
484 | Sebastian Riedel, C<sri@cpan.org> |
158c88c0 |
485 | Matt S Trout, C<mst@shadowcatsystems.co.uk> |
1abd6db7 |
486 | |
487 | =head1 COPYRIGHT |
488 | |
489 | This program is free software, you can redistribute it and/or modify it under |
490 | the same terms as Perl itself. |
491 | |
492 | =cut |
493 | |
494 | 1; |