X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst%2FDispatchType%2FChained.pm;h=a27f6ba4e5b6589212076fff25bbae3f9a4117f6;hp=5bf3853c10ff68695be34675f15c869124e5d459;hb=007a7ca0e2c93f24d55019aaedf27121f91b8cbc;hpb=f3414019f472b55682ef3af53f761b6db7955887 diff --git a/lib/Catalyst/DispatchType/Chained.pm b/lib/Catalyst/DispatchType/Chained.pm index 5bf3853..a27f6ba 100644 --- a/lib/Catalyst/DispatchType/Chained.pm +++ b/lib/Catalyst/DispatchType/Chained.pm @@ -1,6 +1,5 @@ package Catalyst::DispatchType::Chained; -use Class::C3; use Moose; extends 'Catalyst::DispatchType'; @@ -72,6 +71,11 @@ sub list { [ 35, 'Path Spec' ], [ 36, 'Private' ] ); + my $has_unattached_actions; + my $unattached_actions = Text::SimpleTable->new( + [ 35, 'Private' ], [ 36, 'Missing parent' ], + ); + ENDPOINT: foreach my $endpoint ( sort { $a->reverse cmp $b->reverse } @{ $self->_endpoints } @@ -93,7 +97,11 @@ sub list { $curr = $self->_actions->{$parent}; unshift(@parents, $curr) if $curr; } - next ENDPOINT unless $parent eq '/'; # skip dangling action + if ($parent ne '/') { + $has_unattached_actions = 1; + $unattached_actions->row('/'.$parents[0]->reverse, $parent); + next ENDPOINT; + } my @rows; foreach my $p (@parents) { my $name = "/${p}"; @@ -111,6 +119,8 @@ sub list { } $c->log->debug( "Loaded Chained actions:\n" . $paths->draw . "\n" ); + $c->log->debug( "Unattached Chained actions:\n", $unattached_actions->draw . "\n" ) + if $has_unattached_actions; } =head2 $self->match( $c, $path ) @@ -183,7 +193,14 @@ sub recurse_match { my ($actions, $captures, $action_parts) = $self->recurse_match( $c, '/'.$action->reverse, \@parts ); - if ($actions && (!$best_action || $#$action_parts < $#{$best_action->{parts}})){ + # No best action currently + # OR The action has less parts + # OR The action has equal parts but less captured data (ergo more defined) + if ($actions && + (!$best_action || + $#$action_parts < $#{$best_action->{parts}} || + ($#$action_parts == $#{$best_action->{parts}} && + $#$captures < $#{$best_action->{captures}}))){ $best_action = { actions => [ $action, @$actions ], captures=> [ @captures, @$captures ], @@ -234,31 +251,13 @@ sub register { return 0 unless @chained_attr; - if (@chained_attr > 2) { + if (@chained_attr > 1) { Catalyst::Exception->throw( "Multiple Chained attributes not supported registering ${action}" ); } - my $parent = $chained_attr[0]; - - if (defined($parent) && length($parent)) { - if ($parent eq '.') { - $parent = '/'.$action->namespace; - } elsif ($parent !~ m/^\//) { - if ($action->namespace) { - $parent = '/'.join('/', $action->namespace, $parent); - } else { - $parent = '/'.$parent; # special case namespace '' (root) - } - } - } else { - $parent = '/' - } - - $action->attributes->{Chained} = [ $parent ]; - - my $children = ($self->_children_of->{$parent} ||= {}); + my $children = ($self->_children_of->{ $chained_attr[0] } ||= {}); my @path_part = @{ $action->attributes->{PathPart} || [] }; @@ -331,6 +330,23 @@ sub uri_for_action { } +sub expand_action { + my ($self, $action) = @_; + + return unless $action->attributes && $action->attributes->{Chained}; + + my @chain; + my $curr = $action; + + while ($curr) { + push @chain, $curr; + my $parent = $curr->attributes->{Chained}->[0]; + $curr = $self->_actions->{$parent}; + } + + return Catalyst::ActionChain->from_chain([reverse @chain]); +} + __PACKAGE__->meta->make_immutable; =head1 USAGE @@ -508,13 +524,18 @@ with C would bind to C. If you don't specify C<:PathPart> it has the same effect as using C<:PathPart>, it would default to the action name. +=item PathPrefix + +Sets PathPart to the path_prefix of the current controller. + =item Chained Has to be specified for every child in the chain. Possible values are -absolute and relative private action paths, with the relatives pointing -to the current controller, or a single slash C to tell Catalyst that -this is the root of a chain. The attribute C<:Chained> without arguments -also defaults to the C behavior. +absolute and relative private action paths or a single slash C to +tell Catalyst that this is the root of a chain. The attribute +C<:Chained> without arguments also defaults to the C behavior. +Relative action paths may use C<../> to refer to actions in parent +controllers. Because you can specify an absolute path to the parent action, it doesn't matter to Catalyst where that parent is located. So, if your @@ -537,6 +558,19 @@ with the path of the current controller namespace, namely C. That action chains directly to C, so the C chain comes out as the end product. +=item ChainedParent + +Chains an action to another action with the same name in the parent +controller. For Example: + + # in MyApp::Controller::Foo + sub bar : Chained CaptureArgs(1) { ... } + + # in MyApp::Controller::Foo::Moo + sub bar : ChainedParent Args(1) { ... } + +This builds a chain like C. + =item CaptureArgs Must be specified for every part of the chain that is not an @@ -583,9 +617,9 @@ The Cing to other actions does just what you would expect. But if you C out of a chain, the rest of the chain will not get called after the C. -=head1 AUTHOR +=head1 AUTHORS -Matt S Trout +Catalyst Contributors, see Catalyst.pm =head1 COPYRIGHT