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