Commit | Line | Data |
68a748b9 |
1 | package Catalyst::Dispatcher; |
1abd6db7 |
2 | |
059c085b |
3 | use Moose; |
068c0898 |
4 | use Class::MOP; |
531f1ab6 |
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; |
17 | |
c41cfce3 |
18 | # Refactoring note: |
19 | # do these belong as package vars or should we build these via a builder method? |
20 | # See Catalyst-Plugin-Server for them being added to, which should be much less ugly. |
21 | |
6d030e6f |
22 | # Preload these action types |
61a9002d |
23 | our @PRELOAD = qw/Index Path Regex/; |
1abd6db7 |
24 | |
2d1d8f91 |
25 | # Postload these action types |
61a9002d |
26 | our @POSTLOAD = qw/Default/; |
2d1d8f91 |
27 | |
c41cfce3 |
28 | # Note - see back-compat methods at end of file. |
29 | has _tree => (is => 'rw'); |
30 | has _dispatch_types => (is => 'rw', default => sub { [] }, required => 1, lazy => 1); |
31 | has _registered_dispatch_types => (is => 'rw', default => sub { {} }, required => 1, lazy => 1); |
32 | has _method_action_class => (is => 'rw', default => 'Catalyst::Action'); |
33 | has _action_hash => (is => 'rw', required => 1, lazy => 1, default => sub { {} }); |
34 | has _container_hash => (is => 'rw', required => 1, lazy => 1, default => sub { {} }); |
5fb12dbb |
35 | has preload_dispatch_types => (is => 'rw', required => 1, lazy => 1, default => sub { [@PRELOAD] }); |
7ffc9d9d |
36 | |
5fb12dbb |
37 | has postload_dispatch_types => (is => 'rw', required => 1, lazy => 1, default => sub { [@POSTLOAD] }); |
059c085b |
38 | |
083ee5d9 |
39 | # Wrap accessors so you can assign a list and it will capture a list ref. |
40 | around qw/preload_dispatch_types postload_dispatch_types/ => sub { |
41 | my $orig = shift; |
42 | my $self = shift; |
43 | return $self->$orig([@_]) if (scalar @_ && ref $_[0] ne 'ARRAY'); |
44 | return $self->$orig(@_); |
45 | }; |
46 | |
059c085b |
47 | no Moose; |
48 | |
1abd6db7 |
49 | =head1 NAME |
50 | |
9c053379 |
51 | Catalyst::Dispatcher - The Catalyst Dispatcher |
1abd6db7 |
52 | |
53 | =head1 SYNOPSIS |
54 | |
55 | See L<Catalyst>. |
56 | |
57 | =head1 DESCRIPTION |
58 | |
4ab87e27 |
59 | This is the class that maps public urls to actions in your Catalyst |
60 | application based on the attributes you set. |
61 | |
1abd6db7 |
62 | =head1 METHODS |
63 | |
ac5c933b |
64 | =head2 new |
4ab87e27 |
65 | |
66 | Construct a new dispatcher. |
67 | |
e7bb8d33 |
68 | =cut |
69 | |
059c085b |
70 | sub BUILD { |
71 | my ($self, $params) = @_; |
9e81ba44 |
72 | |
068c0898 |
73 | my $container = |
059c085b |
74 | Catalyst::ActionContainer->new( { part => '/', actions => {} } ); |
a13e21ab |
75 | |
c41cfce3 |
76 | $self->_tree( Tree::Simple->new( $container, Tree::Simple->ROOT ) ); |
e7bb8d33 |
77 | } |
78 | |
79 | =head2 $self->preload_dispatch_types |
80 | |
81 | An arrayref of pre-loaded dispatchtype classes |
82 | |
83 | Entries are considered to be available as C<Catalyst::DispatchType::CLASS> |
84 | To use a custom class outside the regular C<Catalyst> namespace, prefix |
85 | it with a C<+>, like so: |
86 | |
87 | +My::Dispatch::Type |
88 | |
89 | =head2 $self->postload_dispatch_types |
90 | |
91 | An arrayref of post-loaded dispatchtype classes |
92 | |
93 | Entries are considered to be available as C<Catalyst::DispatchType::CLASS> |
94 | To use a custom class outside the regular C<Catalyst> namespace, prefix |
95 | it with a C<+>, like so: |
96 | |
97 | +My::Dispatch::Type |
98 | |
b5ecfcf0 |
99 | =head2 $self->dispatch($c) |
1abd6db7 |
100 | |
4ab87e27 |
101 | Delegate the dispatch to the action that matched the url, or return a |
102 | message about unknown resource |
103 | |
1abd6db7 |
104 | =cut |
105 | |
106 | sub dispatch { |
fbcc39ad |
107 | my ( $self, $c ) = @_; |
e63bdf38 |
108 | if ( my $action = $c->action ) { |
109 | $c->forward( join( '/', '', $action->namespace, '_DISPATCH' ) ); |
fbcc39ad |
110 | } |
fbcc39ad |
111 | else { |
1abd6db7 |
112 | my $path = $c->req->path; |
113 | my $error = $path |
114 | ? qq/Unknown resource "$path"/ |
115 | : "No default action defined"; |
116 | $c->log->error($error) if $c->debug; |
117 | $c->error($error); |
118 | } |
119 | } |
120 | |
2f381252 |
121 | # $self->_command2action( $c, $command [, \@arguments ] ) |
b456f8f3 |
122 | # $self->_command2action( $c, $command [, \@captures, \@arguments ] ) |
123 | # Search for an action, from the command and returns C<($action, $args, $captures)> on |
2f381252 |
124 | # success. Returns C<(0)> on error. |
1abd6db7 |
125 | |
2f381252 |
126 | sub _command2action { |
e72f8f51 |
127 | my ( $self, $c, $command, @extra_params ) = @_; |
99fe1710 |
128 | |
1abd6db7 |
129 | unless ($command) { |
2f381252 |
130 | $c->log->debug('Nothing to go to') if $c->debug; |
1abd6db7 |
131 | return 0; |
132 | } |
99fe1710 |
133 | |
b456f8f3 |
134 | my (@args, @captures); |
135 | |
136 | if ( ref( $extra_params[-2] ) eq 'ARRAY' ) { |
137 | @captures = @{ pop @extra_params }; |
138 | } |
068c0898 |
139 | |
e72f8f51 |
140 | if ( ref( $extra_params[-1] ) eq 'ARRAY' ) { |
141 | @args = @{ pop @extra_params } |
142 | } else { |
2f381252 |
143 | # this is a copy, it may take some abuse from |
144 | # ->_invoke_as_path if the path had trailing parts |
e72f8f51 |
145 | @args = @{ $c->request->arguments }; |
146 | } |
147 | |
148 | my $action; |
149 | |
2f381252 |
150 | # go to a string path ("/foo/bar/gorch") |
e31b525c |
151 | # or action object |
7e95ba12 |
152 | if (blessed($command) && $command->isa('Catalyst::Action')) { |
e31b525c |
153 | $action = $command; |
154 | } |
155 | else { |
156 | $action = $self->_invoke_as_path( $c, "$command", \@args ); |
157 | } |
99fe1710 |
158 | |
2f381252 |
159 | # go to a component ( "MyApp::*::Foo" or $c->component("...") |
160 | # - a path or an object) |
e72f8f51 |
161 | unless ($action) { |
162 | my $method = @extra_params ? $extra_params[0] : "process"; |
163 | $action = $self->_invoke_as_component( $c, $command, $method ); |
164 | } |
99fe1710 |
165 | |
b456f8f3 |
166 | return $action, \@args, \@captures; |
2f381252 |
167 | } |
168 | |
ae0e35ee |
169 | =head2 $self->visit( $c, $command [, \@arguments ] ) |
2f381252 |
170 | |
171 | Documented in L<Catalyst> |
172 | |
173 | =cut |
174 | |
ae0e35ee |
175 | sub visit { |
2f381252 |
176 | my $self = shift; |
ae0e35ee |
177 | $self->_do_visit('visit', @_); |
178 | } |
179 | |
180 | sub _do_visit { |
181 | my $self = shift; |
182 | my $opname = shift; |
2f381252 |
183 | my ( $c, $command ) = @_; |
b456f8f3 |
184 | my ( $action, $args, $captures ) = $self->_command2action(@_); |
ae0e35ee |
185 | my $error = qq/Couldn't $opname("$command"): /; |
2f381252 |
186 | |
ae0e35ee |
187 | if (!$action) { |
3ea37672 |
188 | $error .= qq/Couldn't $opname to command "$command": / |
189 | .qq/Invalid action or component./; |
ae0e35ee |
190 | } |
191 | elsif (!defined $action->namespace) { |
192 | $error .= qq/Action has no namespace: cannot $opname() to a plain / |
382d317c |
193 | .qq/method or component, must be an :Action of some sort./ |
ae0e35ee |
194 | } |
195 | elsif (!$action->class->can('_DISPATCH')) { |
196 | $error .= qq/Action cannot _DISPATCH. / |
197 | .qq/Did you try to $opname() a non-controller action?/; |
198 | } |
199 | else { |
200 | $error = q(); |
201 | } |
202 | |
203 | if($error) { |
2f381252 |
204 | $c->error($error); |
205 | $c->log->debug($error) if $c->debug; |
206 | return 0; |
207 | } |
208 | |
52f71256 |
209 | $action = $self->expand_action($action); |
210 | |
2f381252 |
211 | local $c->request->{arguments} = $args; |
b456f8f3 |
212 | local $c->request->{captures} = $captures; |
ae0e35ee |
213 | local $c->{namespace} = $action->{'namespace'}; |
214 | local $c->{action} = $action; |
215 | |
2f381252 |
216 | $self->dispatch($c); |
ae0e35ee |
217 | } |
218 | |
219 | =head2 $self->go( $c, $command [, \@arguments ] ) |
220 | |
221 | Documented in L<Catalyst> |
222 | |
223 | =cut |
2f381252 |
224 | |
ae0e35ee |
225 | sub go { |
226 | my $self = shift; |
227 | $self->_do_visit('go', @_); |
2f381252 |
228 | die $Catalyst::GO; |
229 | } |
230 | |
231 | =head2 $self->forward( $c, $command [, \@arguments ] ) |
232 | |
233 | Documented in L<Catalyst> |
234 | |
235 | =cut |
236 | |
237 | sub forward { |
238 | my $self = shift; |
3ea37672 |
239 | $self->_do_forward(forward => @_); |
240 | } |
241 | |
242 | sub _do_forward { |
243 | my $self = shift; |
244 | my $opname = shift; |
2f381252 |
245 | my ( $c, $command ) = @_; |
b456f8f3 |
246 | my ( $action, $args, $captures ) = $self->_command2action(@_); |
99fe1710 |
247 | |
3ea37672 |
248 | if (!$action) { |
249 | my $error .= qq/Couldn't $opname to command "$command": / |
250 | .qq/Invalid action or component./; |
e540158b |
251 | $c->error($error); |
252 | $c->log->debug($error) if $c->debug; |
253 | return 0; |
254 | } |
bd7d2e94 |
255 | |
059c085b |
256 | no warnings 'recursion'; |
257 | |
12f0342e |
258 | local $c->request->{arguments} = $args; |
b8f669f3 |
259 | $action->dispatch( $c ); |
3ea37672 |
260 | |
1abd6db7 |
261 | return $c->state; |
262 | } |
263 | |
3ea37672 |
264 | =head2 $self->detach( $c, $command [, \@arguments ] ) |
265 | |
266 | Documented in L<Catalyst> |
267 | |
268 | =cut |
269 | |
270 | sub detach { |
271 | my ( $self, $c, $command, @args ) = @_; |
272 | $self->_do_forward(detach => $c, $command, @args ) if $command; |
273 | die $Catalyst::DETACH; |
274 | } |
275 | |
adb53907 |
276 | sub _action_rel2abs { |
e540158b |
277 | my ( $self, $c, $path ) = @_; |
278 | |
279 | unless ( $path =~ m#^/# ) { |
280 | my $namespace = $c->stack->[-1]->namespace; |
281 | $path = "$namespace/$path"; |
282 | } |
283 | |
284 | $path =~ s#^/##; |
285 | return $path; |
adb53907 |
286 | } |
287 | |
288 | sub _invoke_as_path { |
e540158b |
289 | my ( $self, $c, $rel_path, $args ) = @_; |
290 | |
e540158b |
291 | my $path = $self->_action_rel2abs( $c, $rel_path ); |
292 | |
293 | my ( $tail, @extra_args ); |
294 | while ( ( $path, $tail ) = ( $path =~ m#^(?:(.*)/)?(\w+)?$# ) ) |
295 | { # allow $path to be empty |
296 | if ( my $action = $c->get_action( $tail, $path ) ) { |
297 | push @$args, @extra_args; |
298 | return $action; |
299 | } |
300 | else { |
301 | return |
302 | unless $path |
303 | ; # if a match on the global namespace failed then the whole lookup failed |
304 | } |
305 | |
306 | unshift @extra_args, $tail; |
307 | } |
adb53907 |
308 | } |
309 | |
310 | sub _find_component_class { |
e540158b |
311 | my ( $self, $c, $component ) = @_; |
adb53907 |
312 | |
e540158b |
313 | return ref($component) |
314 | || ref( $c->component($component) ) |
315 | || $c->component($component); |
adb53907 |
316 | } |
317 | |
318 | sub _invoke_as_component { |
e540158b |
319 | my ( $self, $c, $component, $method ) = @_; |
320 | |
321 | my $class = $self->_find_component_class( $c, $component ) || return 0; |
e540158b |
322 | |
382d317c |
323 | if (my $code = $component_instance->can('action_for')) { |
324 | my $possible_action = $component_instance->$code($method); |
84c28acb |
325 | return $possible_action if $possible_action; |
326 | } |
327 | |
e540158b |
328 | if ( my $code = $class->can($method) ) { |
c41cfce3 |
329 | return $self->_method_action_class->new( |
e540158b |
330 | { |
331 | name => $method, |
332 | code => $code, |
333 | reverse => "$class->$method", |
334 | class => $class, |
335 | namespace => Catalyst::Utils::class2prefix( |
336 | $class, $c->config->{case_sensitive} |
337 | ), |
338 | } |
339 | ); |
340 | } |
341 | else { |
342 | my $error = |
343 | qq/Couldn't forward to "$class". Does not implement "$method"/; |
344 | $c->error($error); |
345 | $c->log->debug($error) |
346 | if $c->debug; |
347 | return 0; |
348 | } |
adb53907 |
349 | } |
350 | |
b5ecfcf0 |
351 | =head2 $self->prepare_action($c) |
fbcc39ad |
352 | |
4ab87e27 |
353 | Find an dispatch type that matches $c->req->path, and set args from it. |
354 | |
fbcc39ad |
355 | =cut |
356 | |
357 | sub prepare_action { |
358 | my ( $self, $c ) = @_; |
e63bdf38 |
359 | my $req = $c->req; |
360 | my $path = $req->path; |
361 | my @path = split /\//, $req->path; |
362 | $req->args( \my @args ); |
fbcc39ad |
363 | |
61a9002d |
364 | unshift( @path, '' ); # Root action |
78d760bb |
365 | |
b96f127f |
366 | DESCEND: while (@path) { |
fbcc39ad |
367 | $path = join '/', @path; |
61a9002d |
368 | $path =~ s#^/##; |
fbcc39ad |
369 | |
61a9002d |
370 | $path = '' if $path eq '/'; # Root action |
78d760bb |
371 | |
22f3a8dd |
372 | # Check out dispatch types to see if any will handle the path at |
373 | # this level |
374 | |
c41cfce3 |
375 | foreach my $type ( @{ $self->_dispatch_types } ) { |
2633d7dc |
376 | last DESCEND if $type->match( $c, $path ); |
66e28e3f |
377 | } |
b96f127f |
378 | |
22f3a8dd |
379 | # If not, move the last part path to args |
4082e678 |
380 | my $arg = pop(@path); |
381 | $arg =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; |
382 | unshift @args, $arg; |
fbcc39ad |
383 | } |
384 | |
e63bdf38 |
385 | s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg for grep { defined } @{$req->captures||[]}; |
66d7ad40 |
386 | |
e63bdf38 |
387 | $c->log->debug( 'Path is "' . $req->match . '"' ) |
076bfad3 |
388 | if ( $c->debug && defined $req->match && length $req->match ); |
e3a13771 |
389 | |
fbcc39ad |
390 | $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' ) |
391 | if ( $c->debug && @args ); |
392 | } |
393 | |
b5ecfcf0 |
394 | =head2 $self->get_action( $action, $namespace ) |
1abd6db7 |
395 | |
4ab87e27 |
396 | returns a named action from a given namespace. |
397 | |
1abd6db7 |
398 | =cut |
399 | |
400 | sub get_action { |
bcd1002b |
401 | my ( $self, $name, $namespace ) = @_; |
79a3189a |
402 | return unless $name; |
3d0d6d21 |
403 | |
2f381252 |
404 | $namespace = join( "/", grep { length } split '/', ( defined $namespace ? $namespace : "" ) ); |
99fe1710 |
405 | |
c41cfce3 |
406 | return $self->_action_hash->{"${namespace}/${name}"}; |
1abd6db7 |
407 | } |
408 | |
ac5c933b |
409 | =head2 $self->get_action_by_path( $path ); |
068c0898 |
410 | |
ac5c933b |
411 | Returns the named action by its full path. |
3d0d6d21 |
412 | |
068c0898 |
413 | =cut |
3d0d6d21 |
414 | |
415 | sub get_action_by_path { |
416 | my ( $self, $path ) = @_; |
ea0e58d9 |
417 | $path =~ s/^\///; |
28928de9 |
418 | $path = "/$path" unless $path =~ /\//; |
c41cfce3 |
419 | $self->_action_hash->{$path}; |
3d0d6d21 |
420 | } |
421 | |
b5ecfcf0 |
422 | =head2 $self->get_actions( $c, $action, $namespace ) |
a9dc674c |
423 | |
424 | =cut |
425 | |
426 | sub get_actions { |
427 | my ( $self, $c, $action, $namespace ) = @_; |
428 | return [] unless $action; |
3d0d6d21 |
429 | |
28928de9 |
430 | $namespace = join( "/", grep { length } split '/', $namespace || "" ); |
a9dc674c |
431 | |
432 | my @match = $self->get_containers($namespace); |
433 | |
684d10ed |
434 | return map { $_->get_action($action) } @match; |
a9dc674c |
435 | } |
436 | |
b5ecfcf0 |
437 | =head2 $self->get_containers( $namespace ) |
cfd04b0c |
438 | |
4ab87e27 |
439 | Return all the action containers for a given namespace, inclusive |
440 | |
cfd04b0c |
441 | =cut |
442 | |
443 | sub get_containers { |
444 | my ( $self, $namespace ) = @_; |
a13e21ab |
445 | $namespace ||= ''; |
446 | $namespace = '' if $namespace eq '/'; |
cfd04b0c |
447 | |
a13e21ab |
448 | my @containers; |
cfd04b0c |
449 | |
7f23827b |
450 | if ( length $namespace ) { |
451 | do { |
c41cfce3 |
452 | push @containers, $self->_container_hash->{$namespace}; |
7f23827b |
453 | } while ( $namespace =~ s#/[^/]+$## ); |
454 | } |
90ce41ba |
455 | |
c41cfce3 |
456 | return reverse grep { defined } @containers, $self->_container_hash->{''}; |
90ce41ba |
457 | |
e63bdf38 |
458 | #return (split '/', $namespace); # isnt this more clear? |
a13e21ab |
459 | my @parts = split '/', $namespace; |
cfd04b0c |
460 | } |
461 | |
ea0e58d9 |
462 | =head2 $self->uri_for_action($action, \@captures) |
463 | |
464 | Takes a Catalyst::Action object and action parameters and returns a URI |
465 | part such that if $c->req->path were this URI part, this action would be |
466 | dispatched to with $c->req->captures set to the supplied arrayref. |
467 | |
468 | If the action object is not available for external dispatch or the dispatcher |
469 | cannot determine an appropriate URI, this method will return undef. |
470 | |
471 | =cut |
472 | |
473 | sub uri_for_action { |
474 | my ( $self, $action, $captures) = @_; |
475 | $captures ||= []; |
c41cfce3 |
476 | foreach my $dispatch_type ( @{ $self->_dispatch_types } ) { |
ea0e58d9 |
477 | my $uri = $dispatch_type->uri_for_action( $action, $captures ); |
81e75875 |
478 | return( $uri eq '' ? '/' : $uri ) |
479 | if defined($uri); |
ea0e58d9 |
480 | } |
481 | return undef; |
482 | } |
483 | |
8f5a2bd9 |
484 | =head2 expand_action |
ae0e35ee |
485 | |
486 | expand an action into a full representation of the dispatch. |
487 | mostly useful for chained, other actions will just return a |
488 | single action. |
489 | |
490 | =cut |
491 | |
52f71256 |
492 | sub expand_action { |
493 | my ($self, $action) = @_; |
494 | |
c41cfce3 |
495 | foreach my $dispatch_type (@{ $self->_dispatch_types }) { |
52f71256 |
496 | my $expanded = $dispatch_type->expand_action($action); |
497 | return $expanded if $expanded; |
498 | } |
499 | |
500 | return $action; |
501 | } |
502 | |
b5ecfcf0 |
503 | =head2 $self->register( $c, $action ) |
aad72cc9 |
504 | |
4ab87e27 |
505 | Make sure all required dispatch types for this action are loaded, then |
506 | pass the action to our dispatch types so they can register it if required. |
507 | Also, set up the tree with the action containers. |
508 | |
aad72cc9 |
509 | =cut |
510 | |
79a3189a |
511 | sub register { |
512 | my ( $self, $c, $action ) = @_; |
513 | |
c41cfce3 |
514 | my $registered = $self->_registered_dispatch_types; |
694d15f1 |
515 | |
e63bdf38 |
516 | #my $priv = 0; #seems to be unused |
694d15f1 |
517 | foreach my $key ( keys %{ $action->attributes } ) { |
9a6ecf4f |
518 | next if $key eq 'Private'; |
694d15f1 |
519 | my $class = "Catalyst::DispatchType::$key"; |
520 | unless ( $registered->{$class} ) { |
c41cfce3 |
521 | # FIXME - Some error checking and re-throwing needed here, as |
522 | # we eat exceptions loading dispatch types. |
068c0898 |
523 | eval { Class::MOP::load_class($class) }; |
c41cfce3 |
524 | push( @{ $self->_dispatch_types }, $class->new ) unless $@; |
694d15f1 |
525 | $registered->{$class} = 1; |
526 | } |
527 | } |
528 | |
529 | # Pass the action to our dispatch types so they can register it if reqd. |
c41cfce3 |
530 | foreach my $type ( @{ $self->_dispatch_types } ) { |
9a6ecf4f |
531 | $type->register( $c, $action ); |
694d15f1 |
532 | } |
533 | |
79a3189a |
534 | my $namespace = $action->namespace; |
a13e21ab |
535 | my $name = $action->name; |
c7116517 |
536 | |
ad5e4650 |
537 | my $container = $self->_find_or_create_action_container($namespace); |
15e9b5dd |
538 | |
539 | # Set the method value |
a13e21ab |
540 | $container->add_action($action); |
c7116517 |
541 | |
c41cfce3 |
542 | $self->_action_hash->{"$namespace/$name"} = $action; |
543 | $self->_container_hash->{$namespace} = $container; |
15e9b5dd |
544 | } |
545 | |
ad5e4650 |
546 | sub _find_or_create_action_container { |
a13e21ab |
547 | my ( $self, $namespace ) = @_; |
548 | |
c41cfce3 |
549 | my $tree ||= $self->_tree; |
99fe1710 |
550 | |
a13e21ab |
551 | return $tree->getNodeValue unless $namespace; |
78d760bb |
552 | |
a13e21ab |
553 | my @namespace = split '/', $namespace; |
554 | return $self->_find_or_create_namespace_node( $tree, @namespace ) |
555 | ->getNodeValue; |
8505565b |
556 | } |
90ce41ba |
557 | |
8505565b |
558 | sub _find_or_create_namespace_node { |
a13e21ab |
559 | my ( $self, $parent, $part, @namespace ) = @_; |
78d760bb |
560 | |
a13e21ab |
561 | return $parent unless $part; |
8505565b |
562 | |
a13e21ab |
563 | my $child = |
564 | ( grep { $_->getNodeValue->part eq $part } $parent->getAllChildren )[0]; |
8505565b |
565 | |
a13e21ab |
566 | unless ($child) { |
567 | my $container = Catalyst::ActionContainer->new($part); |
568 | $parent->addChild( $child = Tree::Simple->new($container) ); |
569 | } |
99fe1710 |
570 | |
a13e21ab |
571 | $self->_find_or_create_namespace_node( $child, @namespace ); |
1abd6db7 |
572 | } |
573 | |
4ab87e27 |
574 | =head2 $self->setup_actions( $class, $context ) |
575 | |
8f5a2bd9 |
576 | Loads all of the preload dispatch types, registers their actions and then |
8f59bbe2 |
577 | loads all of the postload dispatch types, and iterates over the tree of |
578 | actions, displaying the debug information if appropriate. |
1abd6db7 |
579 | |
580 | =cut |
581 | |
582 | sub setup_actions { |
11bd4e3e |
583 | my ( $self, $c ) = @_; |
99fe1710 |
584 | |
9e81ba44 |
585 | my @classes = |
ad5e4650 |
586 | $self->_load_dispatch_types( @{ $self->preload_dispatch_types } ); |
c41cfce3 |
587 | @{ $self->_registered_dispatch_types }{@classes} = (1) x @classes; |
b96f127f |
588 | |
49070d25 |
589 | foreach my $comp ( values %{ $c->components } ) { |
590 | $comp->register_actions($c) if $comp->can('register_actions'); |
1abd6db7 |
591 | } |
e494bd6b |
592 | |
ad5e4650 |
593 | $self->_load_dispatch_types( @{ $self->postload_dispatch_types } ); |
6d030e6f |
594 | |
11bd4e3e |
595 | return unless $c->debug; |
2eb2c42f |
596 | $self->_display_action_tables($c); |
597 | } |
598 | |
599 | sub _display_action_tables { |
600 | my ($self, $c) = @_; |
99fe1710 |
601 | |
39fc2ce1 |
602 | my $column_width = Catalyst::Utils::term_width() - 20 - 36 - 12; |
684d10ed |
603 | my $privates = Text::SimpleTable->new( |
39fc2ce1 |
604 | [ 20, 'Private' ], [ 36, 'Class' ], [ $column_width, 'Method' ] |
684d10ed |
605 | ); |
99fe1710 |
606 | |
87b85407 |
607 | my $has_private = 0; |
1abd6db7 |
608 | my $walker = sub { |
609 | my ( $walker, $parent, $prefix ) = @_; |
610 | $prefix .= $parent->getNodeValue || ''; |
611 | $prefix .= '/' unless $prefix =~ /\/$/; |
b7aebc12 |
612 | my $node = $parent->getNodeValue->actions; |
99fe1710 |
613 | |
78d760bb |
614 | for my $action ( keys %{$node} ) { |
b7aebc12 |
615 | my $action_obj = $node->{$action}; |
b0bb11ec |
616 | next |
617 | if ( ( $action =~ /^_.*/ ) |
618 | && ( !$c->config->{show_internal_actions} ) ); |
684d10ed |
619 | $privates->row( "$prefix$action", $action_obj->class, $action ); |
87b85407 |
620 | $has_private = 1; |
1abd6db7 |
621 | } |
99fe1710 |
622 | |
1abd6db7 |
623 | $walker->( $walker, $_, $prefix ) for $parent->getAllChildren; |
624 | }; |
99fe1710 |
625 | |
c41cfce3 |
626 | $walker->( $walker, $self->_tree, '' ); |
1cf0345b |
627 | $c->log->debug( "Loaded Private actions:\n" . $privates->draw . "\n" ) |
628 | if $has_private; |
99fe1710 |
629 | |
a9cbd748 |
630 | # List all public actions |
c41cfce3 |
631 | $_->list($c) for @{ $self->_dispatch_types }; |
1abd6db7 |
632 | } |
633 | |
ad5e4650 |
634 | sub _load_dispatch_types { |
9e81ba44 |
635 | my ( $self, @types ) = @_; |
636 | |
637 | my @loaded; |
638 | |
639 | # Preload action types |
640 | for my $type (@types) { |
641 | my $class = |
642 | ( $type =~ /^\+(.*)$/ ) ? $1 : "Catalyst::DispatchType::${type}"; |
0fc2d522 |
643 | |
068c0898 |
644 | eval { Class::MOP::load_class($class) }; |
9e81ba44 |
645 | Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ ) |
646 | if $@; |
c41cfce3 |
647 | push @{ $self->_dispatch_types }, $class->new; |
9e81ba44 |
648 | |
649 | push @loaded, $class; |
650 | } |
651 | |
a13e21ab |
652 | return @loaded; |
9e81ba44 |
653 | } |
654 | |
7ffc9d9d |
655 | # Dont document this until someone else is happy with beaviour. Ash 2009/03/16 |
656 | sub dispatch_type { |
657 | my ($self, $name) = @_; |
658 | |
659 | unless ($name =~ s/^\+//) { |
660 | $name = "Catalyst::DispatchType::" . $name; |
661 | } |
662 | |
663 | for (@{ $self->_dispatch_types }) { |
664 | return $_ if ref($_) eq $name; |
665 | } |
666 | return undef; |
667 | } |
668 | |
c41cfce3 |
669 | use Moose; |
670 | |
671 | # 5.70 backwards compatibility hacks. |
672 | |
673 | # Various plugins (e.g. Plugin::Server and Plugin::Authorization::ACL) |
674 | # need the methods here which *should* be private.. |
675 | |
676 | # However we can't really take them away until there is a sane API for |
677 | # building actions and configuring / introspecting the dispatcher. |
678 | # In 5.90, we should build that infrastructure, port the plugins which |
679 | # use it, and then take the crap below away. |
680 | # See also t/lib/TestApp/Plugin/AddDispatchTypes.pm |
681 | |
682 | # Alias _method_name to method_name, add a before modifier to warn.. |
683 | foreach my $public_method_name (qw/ |
684 | tree |
685 | dispatch_types |
686 | registered_dispatch_types |
687 | method_action_class |
688 | action_hash |
689 | container_hash |
690 | /) { |
691 | my $private_method_name = '_' . $public_method_name; |
692 | my $meta = __PACKAGE__->meta; # Calling meta method here fine as we happen at compile time. |
693 | $meta->add_method($public_method_name, $meta->get_method($private_method_name)); |
694 | { |
695 | my %package_hash; # Only warn once per method, per package. These are infrequent enough that |
696 | # I haven't provided a way to disable them, patches welcome. |
697 | $meta->add_before_method_modifier($public_method_name, sub { |
7e95ba12 |
698 | my $class = blessed(shift); |
c41cfce3 |
699 | $package_hash{$class}++ || do { |
700 | warn("Class $class is calling the deprecated method Catalyst::Dispatcher::$public_method_name,\n" |
701 | . "this will be removed in Catalyst 5.9X"); |
702 | }; |
703 | }); |
704 | } |
705 | } |
706 | # End 5.70 backwards compatibility hacks. |
707 | |
6680c772 |
708 | no Moose; |
e5ecd5bc |
709 | __PACKAGE__->meta->make_immutable; |
710 | |
059c085b |
711 | =head2 meta |
712 | |
713 | Provided by Moose |
714 | |
2f381252 |
715 | =head1 AUTHORS |
1abd6db7 |
716 | |
2f381252 |
717 | Catalyst Contributors, see Catalyst.pm |
1abd6db7 |
718 | |
719 | =head1 COPYRIGHT |
720 | |
721 | This program is free software, you can redistribute it and/or modify it under |
722 | the same terms as Perl itself. |
723 | |
724 | =cut |
725 | |
726 | 1; |