Commit | Line | Data |
68a748b9 |
1 | package Catalyst::Dispatcher; |
1abd6db7 |
2 | |
059c085b |
3 | use Moose; |
068c0898 |
4 | use Class::MOP; |
10954d1d |
5 | with 'MooseX::Emulate::Class::Accessor::Fast'; |
059c085b |
6 | |
a2f2cde9 |
7 | use Catalyst::Exception; |
f05af9ba |
8 | use Catalyst::Utils; |
fbcc39ad |
9 | use Catalyst::Action; |
b7aebc12 |
10 | use Catalyst::ActionContainer; |
b96f127f |
11 | use Catalyst::DispatchType::Default; |
bcccee4e |
12 | use Catalyst::DispatchType::Index; |
39fc2ce1 |
13 | use Catalyst::Utils; |
87b85407 |
14 | use Text::SimpleTable; |
1abd6db7 |
15 | use Tree::Simple; |
16 | use Tree::Simple::Visitor::FindByPath; |
e7399d8b |
17 | use Class::Load qw(load_class try_load_class); |
0ca510f0 |
18 | use Encode 2.21 'decode_utf8'; |
1abd6db7 |
19 | |
792d99b4 |
20 | use namespace::clean -except => 'meta'; |
6f3df815 |
21 | |
c41cfce3 |
22 | # Refactoring note: |
23 | # do these belong as package vars or should we build these via a builder method? |
24 | # See Catalyst-Plugin-Server for them being added to, which should be much less ugly. |
25 | |
6d030e6f |
26 | # Preload these action types |
9c1fc6d6 |
27 | our @PRELOAD = qw/Index Path/; |
1abd6db7 |
28 | |
2d1d8f91 |
29 | # Postload these action types |
61a9002d |
30 | our @POSTLOAD = qw/Default/; |
2d1d8f91 |
31 | |
c41cfce3 |
32 | # Note - see back-compat methods at end of file. |
792d99b4 |
33 | has _tree => (is => 'rw', builder => '_build__tree'); |
5b8ac6cc |
34 | has dispatch_types => (is => 'rw', default => sub { [] }, required => 1, lazy => 1); |
c41cfce3 |
35 | has _registered_dispatch_types => (is => 'rw', default => sub { {} }, required => 1, lazy => 1); |
36 | has _method_action_class => (is => 'rw', default => 'Catalyst::Action'); |
37 | has _action_hash => (is => 'rw', required => 1, lazy => 1, default => sub { {} }); |
38 | has _container_hash => (is => 'rw', required => 1, lazy => 1, default => sub { {} }); |
7ffc9d9d |
39 | |
11ff9b94 |
40 | my %dispatch_types = ( pre => \@PRELOAD, post => \@POSTLOAD ); |
41 | foreach my $type (keys %dispatch_types) { |
42 | has $type . "load_dispatch_types" => ( |
43 | is => 'rw', required => 1, lazy => 1, default => sub { $dispatch_types{$type} }, |
44 | traits => ['MooseX::Emulate::Class::Accessor::Fast::Meta::Role::Attribute'], # List assignment is CAF style |
45 | ); |
46 | } |
083ee5d9 |
47 | |
1abd6db7 |
48 | =head1 NAME |
49 | |
9c053379 |
50 | Catalyst::Dispatcher - The Catalyst Dispatcher |
1abd6db7 |
51 | |
52 | =head1 SYNOPSIS |
53 | |
54 | See L<Catalyst>. |
55 | |
56 | =head1 DESCRIPTION |
57 | |
4ab87e27 |
58 | This is the class that maps public urls to actions in your Catalyst |
59 | application based on the attributes you set. |
60 | |
1abd6db7 |
61 | =head1 METHODS |
62 | |
b0ad47c1 |
63 | =head2 new |
4ab87e27 |
64 | |
65 | Construct a new dispatcher. |
66 | |
e7bb8d33 |
67 | =cut |
68 | |
792d99b4 |
69 | sub _build__tree { |
70 | my ($self) = @_; |
9e81ba44 |
71 | |
068c0898 |
72 | my $container = |
059c085b |
73 | Catalyst::ActionContainer->new( { part => '/', actions => {} } ); |
a13e21ab |
74 | |
792d99b4 |
75 | return Tree::Simple->new($container, Tree::Simple->ROOT); |
e7bb8d33 |
76 | } |
77 | |
78 | =head2 $self->preload_dispatch_types |
79 | |
80 | An arrayref of pre-loaded dispatchtype classes |
81 | |
82 | Entries are considered to be available as C<Catalyst::DispatchType::CLASS> |
83 | To use a custom class outside the regular C<Catalyst> namespace, prefix |
84 | it with a C<+>, like so: |
85 | |
86 | +My::Dispatch::Type |
87 | |
88 | =head2 $self->postload_dispatch_types |
89 | |
90 | An arrayref of post-loaded dispatchtype classes |
91 | |
92 | Entries are considered to be available as C<Catalyst::DispatchType::CLASS> |
93 | To use a custom class outside the regular C<Catalyst> namespace, prefix |
94 | it with a C<+>, like so: |
95 | |
96 | +My::Dispatch::Type |
97 | |
b5ecfcf0 |
98 | =head2 $self->dispatch($c) |
1abd6db7 |
99 | |
4ab87e27 |
100 | Delegate the dispatch to the action that matched the url, or return a |
101 | message about unknown resource |
102 | |
1abd6db7 |
103 | =cut |
104 | |
105 | sub dispatch { |
fbcc39ad |
106 | my ( $self, $c ) = @_; |
e63bdf38 |
107 | if ( my $action = $c->action ) { |
108 | $c->forward( join( '/', '', $action->namespace, '_DISPATCH' ) ); |
fbcc39ad |
109 | } |
fbcc39ad |
110 | else { |
1abd6db7 |
111 | my $path = $c->req->path; |
0ca510f0 |
112 | $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; |
113 | $path = decode_utf8($path); |
114 | |
1abd6db7 |
115 | my $error = $path |
116 | ? qq/Unknown resource "$path"/ |
117 | : "No default action defined"; |
118 | $c->log->error($error) if $c->debug; |
119 | $c->error($error); |
120 | } |
121 | } |
122 | |
2f381252 |
123 | # $self->_command2action( $c, $command [, \@arguments ] ) |
b456f8f3 |
124 | # $self->_command2action( $c, $command [, \@captures, \@arguments ] ) |
125 | # Search for an action, from the command and returns C<($action, $args, $captures)> on |
2f381252 |
126 | # success. Returns C<(0)> on error. |
1abd6db7 |
127 | |
2f381252 |
128 | sub _command2action { |
e72f8f51 |
129 | my ( $self, $c, $command, @extra_params ) = @_; |
99fe1710 |
130 | |
1abd6db7 |
131 | unless ($command) { |
2f381252 |
132 | $c->log->debug('Nothing to go to') if $c->debug; |
1abd6db7 |
133 | return 0; |
134 | } |
99fe1710 |
135 | |
b456f8f3 |
136 | my (@args, @captures); |
137 | |
138 | if ( ref( $extra_params[-2] ) eq 'ARRAY' ) { |
e1e81442 |
139 | @captures = @{ splice @extra_params, -2, 1 }; |
b456f8f3 |
140 | } |
068c0898 |
141 | |
e72f8f51 |
142 | if ( ref( $extra_params[-1] ) eq 'ARRAY' ) { |
143 | @args = @{ pop @extra_params } |
144 | } else { |
2f381252 |
145 | # this is a copy, it may take some abuse from |
146 | # ->_invoke_as_path if the path had trailing parts |
e72f8f51 |
147 | @args = @{ $c->request->arguments }; |
148 | } |
149 | |
150 | my $action; |
151 | |
2f381252 |
152 | # go to a string path ("/foo/bar/gorch") |
e31b525c |
153 | # or action object |
7e95ba12 |
154 | if (blessed($command) && $command->isa('Catalyst::Action')) { |
e31b525c |
155 | $action = $command; |
156 | } |
157 | else { |
158 | $action = $self->_invoke_as_path( $c, "$command", \@args ); |
159 | } |
99fe1710 |
160 | |
76ac74ec |
161 | # go to a component ( "View::Foo" or $c->component("...") |
2f381252 |
162 | # - a path or an object) |
e72f8f51 |
163 | unless ($action) { |
164 | my $method = @extra_params ? $extra_params[0] : "process"; |
165 | $action = $self->_invoke_as_component( $c, $command, $method ); |
166 | } |
99fe1710 |
167 | |
b456f8f3 |
168 | return $action, \@args, \@captures; |
2f381252 |
169 | } |
170 | |
ae0e35ee |
171 | =head2 $self->visit( $c, $command [, \@arguments ] ) |
2f381252 |
172 | |
173 | Documented in L<Catalyst> |
174 | |
175 | =cut |
176 | |
ae0e35ee |
177 | sub visit { |
2f381252 |
178 | my $self = shift; |
ae0e35ee |
179 | $self->_do_visit('visit', @_); |
180 | } |
181 | |
182 | sub _do_visit { |
183 | my $self = shift; |
184 | my $opname = shift; |
2f381252 |
185 | my ( $c, $command ) = @_; |
b456f8f3 |
186 | my ( $action, $args, $captures ) = $self->_command2action(@_); |
ae0e35ee |
187 | my $error = qq/Couldn't $opname("$command"): /; |
2f381252 |
188 | |
ae0e35ee |
189 | if (!$action) { |
3ea37672 |
190 | $error .= qq/Couldn't $opname to command "$command": / |
191 | .qq/Invalid action or component./; |
ae0e35ee |
192 | } |
193 | elsif (!defined $action->namespace) { |
194 | $error .= qq/Action has no namespace: cannot $opname() to a plain / |
382d317c |
195 | .qq/method or component, must be an :Action of some sort./ |
ae0e35ee |
196 | } |
197 | elsif (!$action->class->can('_DISPATCH')) { |
198 | $error .= qq/Action cannot _DISPATCH. / |
199 | .qq/Did you try to $opname() a non-controller action?/; |
200 | } |
201 | else { |
202 | $error = q(); |
203 | } |
204 | |
205 | if($error) { |
2f381252 |
206 | $c->error($error); |
207 | $c->log->debug($error) if $c->debug; |
208 | return 0; |
209 | } |
210 | |
52f71256 |
211 | $action = $self->expand_action($action); |
212 | |
2f381252 |
213 | local $c->request->{arguments} = $args; |
b456f8f3 |
214 | local $c->request->{captures} = $captures; |
ae0e35ee |
215 | local $c->{namespace} = $action->{'namespace'}; |
216 | local $c->{action} = $action; |
217 | |
2f381252 |
218 | $self->dispatch($c); |
ae0e35ee |
219 | } |
220 | |
221 | =head2 $self->go( $c, $command [, \@arguments ] ) |
222 | |
223 | Documented in L<Catalyst> |
224 | |
225 | =cut |
2f381252 |
226 | |
ae0e35ee |
227 | sub go { |
228 | my $self = shift; |
229 | $self->_do_visit('go', @_); |
f87b7c21 |
230 | Catalyst::Exception::Go->throw; |
2f381252 |
231 | } |
232 | |
233 | =head2 $self->forward( $c, $command [, \@arguments ] ) |
234 | |
235 | Documented in L<Catalyst> |
236 | |
237 | =cut |
238 | |
239 | sub forward { |
240 | my $self = shift; |
6f3df815 |
241 | no warnings 'recursion'; |
3ea37672 |
242 | $self->_do_forward(forward => @_); |
243 | } |
244 | |
245 | sub _do_forward { |
246 | my $self = shift; |
247 | my $opname = shift; |
2f381252 |
248 | my ( $c, $command ) = @_; |
b456f8f3 |
249 | my ( $action, $args, $captures ) = $self->_command2action(@_); |
99fe1710 |
250 | |
3ea37672 |
251 | if (!$action) { |
252 | my $error .= qq/Couldn't $opname to command "$command": / |
253 | .qq/Invalid action or component./; |
e540158b |
254 | $c->error($error); |
255 | $c->log->debug($error) if $c->debug; |
256 | return 0; |
257 | } |
bd7d2e94 |
258 | |
059c085b |
259 | |
12f0342e |
260 | local $c->request->{arguments} = $args; |
6f3df815 |
261 | no warnings 'recursion'; |
b8f669f3 |
262 | $action->dispatch( $c ); |
3ea37672 |
263 | |
1abd6db7 |
264 | return $c->state; |
265 | } |
266 | |
3ea37672 |
267 | =head2 $self->detach( $c, $command [, \@arguments ] ) |
268 | |
269 | Documented in L<Catalyst> |
270 | |
271 | =cut |
272 | |
273 | sub detach { |
274 | my ( $self, $c, $command, @args ) = @_; |
275 | $self->_do_forward(detach => $c, $command, @args ) if $command; |
f87b7c21 |
276 | Catalyst::Exception::Detach->throw; |
3ea37672 |
277 | } |
278 | |
adb53907 |
279 | sub _action_rel2abs { |
e540158b |
280 | my ( $self, $c, $path ) = @_; |
281 | |
282 | unless ( $path =~ m#^/# ) { |
283 | my $namespace = $c->stack->[-1]->namespace; |
284 | $path = "$namespace/$path"; |
285 | } |
286 | |
287 | $path =~ s#^/##; |
288 | return $path; |
adb53907 |
289 | } |
290 | |
291 | sub _invoke_as_path { |
e540158b |
292 | my ( $self, $c, $rel_path, $args ) = @_; |
293 | |
e540158b |
294 | my $path = $self->_action_rel2abs( $c, $rel_path ); |
295 | |
296 | my ( $tail, @extra_args ); |
297 | while ( ( $path, $tail ) = ( $path =~ m#^(?:(.*)/)?(\w+)?$# ) ) |
298 | { # allow $path to be empty |
299 | if ( my $action = $c->get_action( $tail, $path ) ) { |
300 | push @$args, @extra_args; |
301 | return $action; |
302 | } |
303 | else { |
304 | return |
305 | unless $path |
306 | ; # if a match on the global namespace failed then the whole lookup failed |
307 | } |
308 | |
309 | unshift @extra_args, $tail; |
310 | } |
adb53907 |
311 | } |
312 | |
02298d3a |
313 | sub _find_component { |
e540158b |
314 | my ( $self, $c, $component ) = @_; |
adb53907 |
315 | |
02298d3a |
316 | # fugly, why doesn't ->component('MyApp') work? |
317 | return $c if ($component eq blessed($c)); |
318 | |
319 | return blessed($component) |
320 | ? $component |
321 | : $c->component($component); |
adb53907 |
322 | } |
323 | |
324 | sub _invoke_as_component { |
02298d3a |
325 | my ( $self, $c, $component_or_class, $method ) = @_; |
e540158b |
326 | |
02298d3a |
327 | my $component = $self->_find_component($c, $component_or_class); |
328 | my $component_class = blessed $component || return 0; |
e540158b |
329 | |
02298d3a |
330 | if (my $code = $component_class->can('action_for')) { |
331 | my $possible_action = $component->$code($method); |
84c28acb |
332 | return $possible_action if $possible_action; |
333 | } |
334 | |
02298d3a |
335 | if ( my $code = $component_class->can($method) ) { |
c41cfce3 |
336 | return $self->_method_action_class->new( |
e540158b |
337 | { |
338 | name => $method, |
339 | code => $code, |
02298d3a |
340 | reverse => "$component_class->$method", |
341 | class => $component_class, |
e540158b |
342 | namespace => Catalyst::Utils::class2prefix( |
df960201 |
343 | $component_class, ref($c)->config->{case_sensitive} |
e540158b |
344 | ), |
345 | } |
346 | ); |
347 | } |
348 | else { |
349 | my $error = |
02298d3a |
350 | qq/Couldn't forward to "$component_class". Does not implement "$method"/; |
e540158b |
351 | $c->error($error); |
352 | $c->log->debug($error) |
353 | if $c->debug; |
354 | return 0; |
355 | } |
adb53907 |
356 | } |
357 | |
b5ecfcf0 |
358 | =head2 $self->prepare_action($c) |
fbcc39ad |
359 | |
4ab87e27 |
360 | Find an dispatch type that matches $c->req->path, and set args from it. |
361 | |
fbcc39ad |
362 | =cut |
363 | |
364 | sub prepare_action { |
365 | my ( $self, $c ) = @_; |
e63bdf38 |
366 | my $req = $c->req; |
367 | my $path = $req->path; |
368 | my @path = split /\//, $req->path; |
369 | $req->args( \my @args ); |
fbcc39ad |
370 | |
61a9002d |
371 | unshift( @path, '' ); # Root action |
78d760bb |
372 | |
b96f127f |
373 | DESCEND: while (@path) { |
fbcc39ad |
374 | $path = join '/', @path; |
5299fff8 |
375 | $path =~ s#^/+##; |
78d760bb |
376 | |
22f3a8dd |
377 | # Check out dispatch types to see if any will handle the path at |
378 | # this level |
379 | |
a2aac3b8 |
380 | foreach my $type ( @{ $self->dispatch_types } ) { |
2633d7dc |
381 | last DESCEND if $type->match( $c, $path ); |
66e28e3f |
382 | } |
b96f127f |
383 | |
22f3a8dd |
384 | # If not, move the last part path to args |
4082e678 |
385 | my $arg = pop(@path); |
386 | $arg =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; |
387 | unshift @args, $arg; |
fbcc39ad |
388 | } |
389 | |
e63bdf38 |
390 | s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg for grep { defined } @{$req->captures||[]}; |
66d7ad40 |
391 | |
0ca510f0 |
392 | if($c->debug && defined $req->match && length $req->match) { |
393 | my $match = $req->match; |
394 | $match =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; |
395 | $match = decode_utf8($match); |
396 | $c->log->debug( 'Path is "' . $match . '"' ) |
397 | } |
e3a13771 |
398 | |
0ca510f0 |
399 | $c->log->debug( 'Arguments are "' . join( '/', map { decode_utf8 $_ } @args ) . '"' ) |
fbcc39ad |
400 | if ( $c->debug && @args ); |
401 | } |
402 | |
b5ecfcf0 |
403 | =head2 $self->get_action( $action, $namespace ) |
1abd6db7 |
404 | |
4ab87e27 |
405 | returns a named action from a given namespace. |
406 | |
1abd6db7 |
407 | =cut |
408 | |
409 | sub get_action { |
bcd1002b |
410 | my ( $self, $name, $namespace ) = @_; |
79a3189a |
411 | return unless $name; |
3d0d6d21 |
412 | |
2f381252 |
413 | $namespace = join( "/", grep { length } split '/', ( defined $namespace ? $namespace : "" ) ); |
99fe1710 |
414 | |
c41cfce3 |
415 | return $self->_action_hash->{"${namespace}/${name}"}; |
1abd6db7 |
416 | } |
417 | |
b0ad47c1 |
418 | =head2 $self->get_action_by_path( $path ); |
068c0898 |
419 | |
ab990582 |
420 | Returns the named action by its full private path. |
3d0d6d21 |
421 | |
068c0898 |
422 | =cut |
3d0d6d21 |
423 | |
424 | sub get_action_by_path { |
425 | my ( $self, $path ) = @_; |
ea0e58d9 |
426 | $path =~ s/^\///; |
28928de9 |
427 | $path = "/$path" unless $path =~ /\//; |
c41cfce3 |
428 | $self->_action_hash->{$path}; |
3d0d6d21 |
429 | } |
430 | |
b5ecfcf0 |
431 | =head2 $self->get_actions( $c, $action, $namespace ) |
a9dc674c |
432 | |
433 | =cut |
434 | |
435 | sub get_actions { |
436 | my ( $self, $c, $action, $namespace ) = @_; |
437 | return [] unless $action; |
3d0d6d21 |
438 | |
28928de9 |
439 | $namespace = join( "/", grep { length } split '/', $namespace || "" ); |
a9dc674c |
440 | |
441 | my @match = $self->get_containers($namespace); |
442 | |
684d10ed |
443 | return map { $_->get_action($action) } @match; |
a9dc674c |
444 | } |
445 | |
b5ecfcf0 |
446 | =head2 $self->get_containers( $namespace ) |
cfd04b0c |
447 | |
4ab87e27 |
448 | Return all the action containers for a given namespace, inclusive |
449 | |
cfd04b0c |
450 | =cut |
451 | |
452 | sub get_containers { |
453 | my ( $self, $namespace ) = @_; |
a13e21ab |
454 | $namespace ||= ''; |
455 | $namespace = '' if $namespace eq '/'; |
cfd04b0c |
456 | |
a13e21ab |
457 | my @containers; |
cfd04b0c |
458 | |
7f23827b |
459 | if ( length $namespace ) { |
460 | do { |
c41cfce3 |
461 | push @containers, $self->_container_hash->{$namespace}; |
7f23827b |
462 | } while ( $namespace =~ s#/[^/]+$## ); |
463 | } |
90ce41ba |
464 | |
c41cfce3 |
465 | return reverse grep { defined } @containers, $self->_container_hash->{''}; |
cfd04b0c |
466 | } |
467 | |
ea0e58d9 |
468 | =head2 $self->uri_for_action($action, \@captures) |
469 | |
470 | Takes a Catalyst::Action object and action parameters and returns a URI |
471 | part such that if $c->req->path were this URI part, this action would be |
472 | dispatched to with $c->req->captures set to the supplied arrayref. |
473 | |
474 | If the action object is not available for external dispatch or the dispatcher |
475 | cannot determine an appropriate URI, this method will return undef. |
476 | |
477 | =cut |
478 | |
479 | sub uri_for_action { |
480 | my ( $self, $action, $captures) = @_; |
481 | $captures ||= []; |
a2aac3b8 |
482 | foreach my $dispatch_type ( @{ $self->dispatch_types } ) { |
ea0e58d9 |
483 | my $uri = $dispatch_type->uri_for_action( $action, $captures ); |
81e75875 |
484 | return( $uri eq '' ? '/' : $uri ) |
485 | if defined($uri); |
ea0e58d9 |
486 | } |
487 | return undef; |
488 | } |
489 | |
8f5a2bd9 |
490 | =head2 expand_action |
ae0e35ee |
491 | |
492 | expand an action into a full representation of the dispatch. |
493 | mostly useful for chained, other actions will just return a |
494 | single action. |
495 | |
496 | =cut |
497 | |
52f71256 |
498 | sub expand_action { |
499 | my ($self, $action) = @_; |
500 | |
a2aac3b8 |
501 | foreach my $dispatch_type (@{ $self->dispatch_types }) { |
52f71256 |
502 | my $expanded = $dispatch_type->expand_action($action); |
503 | return $expanded if $expanded; |
504 | } |
505 | |
506 | return $action; |
507 | } |
508 | |
b5ecfcf0 |
509 | =head2 $self->register( $c, $action ) |
aad72cc9 |
510 | |
4ab87e27 |
511 | Make sure all required dispatch types for this action are loaded, then |
512 | pass the action to our dispatch types so they can register it if required. |
513 | Also, set up the tree with the action containers. |
514 | |
aad72cc9 |
515 | =cut |
516 | |
79a3189a |
517 | sub register { |
518 | my ( $self, $c, $action ) = @_; |
519 | |
c41cfce3 |
520 | my $registered = $self->_registered_dispatch_types; |
694d15f1 |
521 | |
694d15f1 |
522 | foreach my $key ( keys %{ $action->attributes } ) { |
9a6ecf4f |
523 | next if $key eq 'Private'; |
694d15f1 |
524 | my $class = "Catalyst::DispatchType::$key"; |
525 | unless ( $registered->{$class} ) { |
c41cfce3 |
526 | # FIXME - Some error checking and re-throwing needed here, as |
527 | # we eat exceptions loading dispatch types. |
e7399d8b |
528 | # see also try_load_class |
529 | eval { load_class($class) }; |
5070f111 |
530 | my $load_failed = $@; |
6931f972 |
531 | $self->_check_deprecated_dispatch_type( $key, $load_failed ); |
5070f111 |
532 | push( @{ $self->dispatch_types }, $class->new ) unless $load_failed; |
694d15f1 |
533 | $registered->{$class} = 1; |
534 | } |
535 | } |
536 | |
a2aac3b8 |
537 | my @dtypes = @{ $self->dispatch_types }; |
1315d253 |
538 | my @normal_dtypes; |
539 | my @low_precedence_dtypes; |
540 | |
7b442de5 |
541 | for my $type ( @dtypes ) { |
c4586fd0 |
542 | if ($type->_is_low_precedence) { |
1315d253 |
543 | push @low_precedence_dtypes, $type; |
544 | } else { |
545 | push @normal_dtypes, $type; |
546 | } |
547 | } |
548 | |
694d15f1 |
549 | # Pass the action to our dispatch types so they can register it if reqd. |
1315d253 |
550 | my $was_registered = 0; |
551 | foreach my $type ( @normal_dtypes ) { |
552 | $was_registered = 1 if $type->register( $c, $action ); |
553 | } |
554 | |
555 | if (not $was_registered) { |
556 | foreach my $type ( @low_precedence_dtypes ) { |
557 | $type->register( $c, $action ); |
558 | } |
694d15f1 |
559 | } |
560 | |
79a3189a |
561 | my $namespace = $action->namespace; |
a13e21ab |
562 | my $name = $action->name; |
c7116517 |
563 | |
ad5e4650 |
564 | my $container = $self->_find_or_create_action_container($namespace); |
15e9b5dd |
565 | |
566 | # Set the method value |
a13e21ab |
567 | $container->add_action($action); |
c7116517 |
568 | |
c41cfce3 |
569 | $self->_action_hash->{"$namespace/$name"} = $action; |
570 | $self->_container_hash->{$namespace} = $container; |
15e9b5dd |
571 | } |
572 | |
ad5e4650 |
573 | sub _find_or_create_action_container { |
a13e21ab |
574 | my ( $self, $namespace ) = @_; |
575 | |
c41cfce3 |
576 | my $tree ||= $self->_tree; |
99fe1710 |
577 | |
a13e21ab |
578 | return $tree->getNodeValue unless $namespace; |
78d760bb |
579 | |
a13e21ab |
580 | my @namespace = split '/', $namespace; |
581 | return $self->_find_or_create_namespace_node( $tree, @namespace ) |
582 | ->getNodeValue; |
8505565b |
583 | } |
90ce41ba |
584 | |
8505565b |
585 | sub _find_or_create_namespace_node { |
a13e21ab |
586 | my ( $self, $parent, $part, @namespace ) = @_; |
78d760bb |
587 | |
a13e21ab |
588 | return $parent unless $part; |
8505565b |
589 | |
a13e21ab |
590 | my $child = |
591 | ( grep { $_->getNodeValue->part eq $part } $parent->getAllChildren )[0]; |
8505565b |
592 | |
a13e21ab |
593 | unless ($child) { |
594 | my $container = Catalyst::ActionContainer->new($part); |
595 | $parent->addChild( $child = Tree::Simple->new($container) ); |
596 | } |
99fe1710 |
597 | |
a13e21ab |
598 | $self->_find_or_create_namespace_node( $child, @namespace ); |
1abd6db7 |
599 | } |
600 | |
4ab87e27 |
601 | =head2 $self->setup_actions( $class, $context ) |
602 | |
965f3e35 |
603 | Loads all of the pre-load dispatch types, registers their actions and then |
604 | loads all of the post-load dispatch types, and iterates over the tree of |
8f59bbe2 |
605 | actions, displaying the debug information if appropriate. |
1abd6db7 |
606 | |
607 | =cut |
608 | |
609 | sub setup_actions { |
11bd4e3e |
610 | my ( $self, $c ) = @_; |
99fe1710 |
611 | |
9e81ba44 |
612 | my @classes = |
ad5e4650 |
613 | $self->_load_dispatch_types( @{ $self->preload_dispatch_types } ); |
c41cfce3 |
614 | @{ $self->_registered_dispatch_types }{@classes} = (1) x @classes; |
b96f127f |
615 | |
98d049ef |
616 | foreach my $comp ( values %{ $c->components } ) { |
617 | $comp = $comp->() if ref($comp) eq 'CODE'; |
49070d25 |
618 | $comp->register_actions($c) if $comp->can('register_actions'); |
1abd6db7 |
619 | } |
e494bd6b |
620 | |
ad5e4650 |
621 | $self->_load_dispatch_types( @{ $self->postload_dispatch_types } ); |
6d030e6f |
622 | |
11bd4e3e |
623 | return unless $c->debug; |
2eb2c42f |
624 | $self->_display_action_tables($c); |
625 | } |
626 | |
627 | sub _display_action_tables { |
628 | my ($self, $c) = @_; |
99fe1710 |
629 | |
48d435ba |
630 | my $avail_width = Catalyst::Utils::term_width() - 12; |
631 | my $col1_width = ($avail_width * .25) < 20 ? 20 : int($avail_width * .25); |
632 | my $col2_width = ($avail_width * .50) < 36 ? 36 : int($avail_width * .50); |
633 | my $col3_width = $avail_width - $col1_width - $col2_width; |
684d10ed |
634 | my $privates = Text::SimpleTable->new( |
48d435ba |
635 | [ $col1_width, 'Private' ], [ $col2_width, 'Class' ], [ $col3_width, 'Method' ] |
684d10ed |
636 | ); |
99fe1710 |
637 | |
87b85407 |
638 | my $has_private = 0; |
1abd6db7 |
639 | my $walker = sub { |
640 | my ( $walker, $parent, $prefix ) = @_; |
641 | $prefix .= $parent->getNodeValue || ''; |
642 | $prefix .= '/' unless $prefix =~ /\/$/; |
b7aebc12 |
643 | my $node = $parent->getNodeValue->actions; |
99fe1710 |
644 | |
78d760bb |
645 | for my $action ( keys %{$node} ) { |
b7aebc12 |
646 | my $action_obj = $node->{$action}; |
b0bb11ec |
647 | next |
648 | if ( ( $action =~ /^_.*/ ) |
649 | && ( !$c->config->{show_internal_actions} ) ); |
684d10ed |
650 | $privates->row( "$prefix$action", $action_obj->class, $action ); |
87b85407 |
651 | $has_private = 1; |
1abd6db7 |
652 | } |
99fe1710 |
653 | |
1abd6db7 |
654 | $walker->( $walker, $_, $prefix ) for $parent->getAllChildren; |
655 | }; |
99fe1710 |
656 | |
c41cfce3 |
657 | $walker->( $walker, $self->_tree, '' ); |
1cf0345b |
658 | $c->log->debug( "Loaded Private actions:\n" . $privates->draw . "\n" ) |
659 | if $has_private; |
99fe1710 |
660 | |
a9cbd748 |
661 | # List all public actions |
a2aac3b8 |
662 | $_->list($c) for @{ $self->dispatch_types }; |
1abd6db7 |
663 | } |
664 | |
ad5e4650 |
665 | sub _load_dispatch_types { |
9e81ba44 |
666 | my ( $self, @types ) = @_; |
667 | |
668 | my @loaded; |
9e81ba44 |
669 | # Preload action types |
670 | for my $type (@types) { |
5d8129e9 |
671 | # first param is undef because we cannot get the appclass |
672 | my $class = Catalyst::Utils::resolve_namespace(undef, 'Catalyst::DispatchType', $type); |
2efad04b |
673 | |
e7399d8b |
674 | my ($success, $error) = try_load_class($class); |
675 | Catalyst::Exception->throw( message => $error ) if not $success; |
a2aac3b8 |
676 | push @{ $self->dispatch_types }, $class->new; |
9e81ba44 |
677 | |
678 | push @loaded, $class; |
679 | } |
680 | |
a13e21ab |
681 | return @loaded; |
9e81ba44 |
682 | } |
683 | |
e995c634 |
684 | =head2 $self->dispatch_type( $type ) |
685 | |
686 | Get the DispatchType object of the relevant type, i.e. passing C<$type> of |
687 | C<Chained> would return a L<Catalyst::DispatchType::Chained> object (assuming |
b0ad47c1 |
688 | of course it's being used.) |
e995c634 |
689 | |
690 | =cut |
691 | |
7ffc9d9d |
692 | sub dispatch_type { |
693 | my ($self, $name) = @_; |
196932de |
694 | |
5d8129e9 |
695 | # first param is undef because we cannot get the appclass |
696 | $name = Catalyst::Utils::resolve_namespace(undef, 'Catalyst::DispatchType', $name); |
7ffc9d9d |
697 | |
a2aac3b8 |
698 | for (@{ $self->dispatch_types }) { |
7ffc9d9d |
699 | return $_ if ref($_) eq $name; |
700 | } |
701 | return undef; |
702 | } |
703 | |
6931f972 |
704 | sub _check_deprecated_dispatch_type { |
5070f111 |
705 | my ($self, $key, $load_failed) = @_; |
706 | |
707 | return unless $key =~ /^(Local)?Regexp?/; |
708 | |
709 | # TODO: Should these throw an exception rather than just warning? |
710 | if ($load_failed) { |
711 | warn( "Attempt to use deprecated $key dispatch type.\n" |
712 | . " Use Chained methods or install the standalone\n" |
713 | . " Catalyst::DispatchType::Regex if necessary.\n" ); |
714 | } elsif ( !defined $Catalyst::DispatchType::Regex::VERSION |
28054612 |
715 | || $Catalyst::DispatchType::Regex::VERSION le '5.90020' ) { |
5070f111 |
716 | # We loaded the old core version of the Regex module this will break |
717 | warn( "The $key DispatchType has been removed from Catalyst core.\n" |
718 | . " An old version of the core Catalyst::DispatchType::Regex\n" |
719 | . " has been loaded and will likely fail. Please remove\n" |
8503ab9b |
720 | . " $INC{'Catalyst/DispatchType/Regex.pm'}\n" |
5070f111 |
721 | . " and use Chained methods or install the standalone\n" |
722 | . " Catalyst::DispatchType::Regex if necessary.\n" ); |
723 | } |
724 | } |
725 | |
c41cfce3 |
726 | use Moose; |
727 | |
728 | # 5.70 backwards compatibility hacks. |
729 | |
730 | # Various plugins (e.g. Plugin::Server and Plugin::Authorization::ACL) |
731 | # need the methods here which *should* be private.. |
732 | |
2efad04b |
733 | # You should be able to use get_actions or get_containers appropriately |
734 | # instead of relying on these methods which expose implementation details |
735 | # of the dispatcher.. |
736 | # |
737 | # IRC backlog included below, please come ask if this doesn't work for you. |
738 | # |
739 | # <@t0m> 5.80, the state of. There are things in the dispatcher which have |
740 | # been deprecated, that we yell at anyone for using, which there isn't |
741 | # a good alternative for yet.. |
742 | # <@mst> er, get_actions/get_containers provides that doesn't it? |
743 | # <@mst> DispatchTypes are loaded on demand anyway |
744 | # <@t0m> I'm thinking of things like _tree which is aliased to 'tree' with |
745 | # warnings otherwise shit breaks.. We're issuing warnings about the |
746 | # correct set of things which you shouldn't be calling.. |
747 | # <@mst> right |
748 | # <@mst> basically, I don't see there's a need for a replacement for anything |
749 | # <@mst> it was never a good idea to call ->tree |
750 | # <@mst> nothingmuch was the only one who did AFAIK |
751 | # <@mst> and he admitted it was a hack ;) |
752 | |
c41cfce3 |
753 | # See also t/lib/TestApp/Plugin/AddDispatchTypes.pm |
754 | |
755 | # Alias _method_name to method_name, add a before modifier to warn.. |
b0ad47c1 |
756 | foreach my $public_method_name (qw/ |
757 | tree |
b0ad47c1 |
758 | registered_dispatch_types |
759 | method_action_class |
760 | action_hash |
c41cfce3 |
761 | container_hash |
762 | /) { |
763 | my $private_method_name = '_' . $public_method_name; |
764 | my $meta = __PACKAGE__->meta; # Calling meta method here fine as we happen at compile time. |
765 | $meta->add_method($public_method_name, $meta->get_method($private_method_name)); |
766 | { |
767 | my %package_hash; # Only warn once per method, per package. These are infrequent enough that |
768 | # I haven't provided a way to disable them, patches welcome. |
769 | $meta->add_before_method_modifier($public_method_name, sub { |
3cd3bc6a |
770 | my $class = caller(2); |
771 | chomp($class); |
b3f7d10b |
772 | $package_hash{$class}++ || do { |
3cd3bc6a |
773 | warn("Class $class is calling the deprecated method\n" |
774 | . " Catalyst::Dispatcher::$public_method_name,\n" |
dacd8b0e |
775 | . " this will be removed in Catalyst 5.9\n"); |
c41cfce3 |
776 | }; |
777 | }); |
778 | } |
779 | } |
780 | # End 5.70 backwards compatibility hacks. |
781 | |
e5ecd5bc |
782 | __PACKAGE__->meta->make_immutable; |
783 | |
059c085b |
784 | =head2 meta |
785 | |
786 | Provided by Moose |
787 | |
2f381252 |
788 | =head1 AUTHORS |
1abd6db7 |
789 | |
2f381252 |
790 | Catalyst Contributors, see Catalyst.pm |
1abd6db7 |
791 | |
792 | =head1 COPYRIGHT |
793 | |
536bee89 |
794 | This library is free software. You can redistribute it and/or modify it under |
1abd6db7 |
795 | the same terms as Perl itself. |
796 | |
797 | =cut |
798 | |
799 | 1; |