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=3b7502e95dceb08d61522d3d02be63d9b21ea3d7;hp=dccd3c9ef8e88a8170b9ebc5d9edc8adefe1786a;hb=ae29b412955743885e80350085167b54b69672da;hpb=e16a6c4e6c4d49e73b5286b3186616af14f3f554 diff --git a/lib/Catalyst/DispatchType/Chained.pm b/lib/Catalyst/DispatchType/Chained.pm index dccd3c9..3b7502e 100644 --- a/lib/Catalyst/DispatchType/Chained.pm +++ b/lib/Catalyst/DispatchType/Chained.pm @@ -1,12 +1,36 @@ package Catalyst::DispatchType::Chained; -use strict; -use base qw/Catalyst::DispatchType/; +use Moose; +extends 'Catalyst::DispatchType'; + use Text::SimpleTable; use Catalyst::ActionChain; use Catalyst::Utils; use URI; +has _endpoints => ( + is => 'rw', + isa => 'ArrayRef', + required => 1, + default => sub{ [] }, + ); + +has _actions => ( + is => 'rw', + isa => 'HashRef', + required => 1, + default => sub{ {} }, + ); + +has _children_of => ( + is => 'rw', + isa => 'HashRef', + required => 1, + default => sub{ {} }, + ); + +no Moose; + # please don't perltidy this. hairy code within. =head1 NAME @@ -42,16 +66,21 @@ Debug output for Path Part dispatch points sub list { my ( $self, $c ) = @_; - return unless $self->{endpoints}; + return unless $self->_endpoints; my $column_width = Catalyst::Utils::term_width() - 35 - 9; my $paths = Text::SimpleTable->new( - [ 35, 'Path Spec' ], [ 36, 'Private' ], [ $column_width, 'Private' ] + [ 35, 'Path Spec' ], [ $column_width, '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} } + @{ $self->_endpoints } ) { my $args = $endpoint->attributes->{Args}->[0]; my @parts = (defined($args) ? (("*") x $args) : '...'); @@ -67,10 +96,14 @@ sub list { if (defined $pp->[0] && length $pp->[0]); } $parent = $curr->attributes->{Chained}->[0]; - $curr = $self->{actions}{$parent}; + $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}"; @@ -88,6 +121,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 ) @@ -99,20 +134,21 @@ Calls C to see if a chain matches the C<$path>. sub match { my ( $self, $c, $path ) = @_; - return 0 if @{$c->req->args}; + my $request = $c->request; + return 0 if @{$request->args}; my @parts = split('/', $path); my ($chain, $captures, $parts) = $self->recurse_match($c, '/', \@parts); - push @{$c->req->args}, @$parts if $parts && @$parts; + push @{$request->args}, @$parts if $parts && @$parts; return 0 unless $chain; my $action = Catalyst::ActionChain->from_chain($chain); - $c->req->action("/${action}"); - $c->req->match("/${action}"); - $c->req->captures($captures); + $request->action("/${action}"); + $request->match("/${action}"); + $request->captures($captures); $c->action($action); $c->namespace( $action->namespace ); @@ -127,7 +163,7 @@ Recursive search for a matching chain. sub recurse_match { my ( $self, $c, $parent, $path_parts ) = @_; - my $children = $self->{children_of}{$parent}; + my $children = $self->_children_of->{$parent}; return () unless $children; my $best_action; my @captures; @@ -223,7 +259,7 @@ sub register { ); } - my $children = ($self->{children_of}{ $chained_attr[0] } ||= {}); + my $children = ($self->_children_of->{ $chained_attr[0] } ||= {}); my @path_part = @{ $action->attributes->{PathPart} || [] }; @@ -233,13 +269,13 @@ sub register { $part = $path_part[0]; } elsif (@path_part > 1) { Catalyst::Exception->throw( - "Multiple PathPart attributes not supported registering ${action}" + "Multiple PathPart attributes not supported registering " . $action->reverse() ); } if ($part =~ m(^/)) { Catalyst::Exception->throw( - "Absolute parameters to PathPart not allowed registering ${action}" + "Absolute parameters to PathPart not allowed registering " . $action->reverse() ); } @@ -247,10 +283,10 @@ sub register { unshift(@{ $children->{$part} ||= [] }, $action); - ($self->{actions} ||= {})->{'/'.$action->reverse} = $action; + $self->_actions->{'/'.$action->reverse} = $action; unless ($action->attributes->{CaptureArgs}) { - unshift(@{ $self->{endpoints} ||= [] }, $action); + unshift(@{ $self->_endpoints }, $action); } return 1; @@ -285,7 +321,7 @@ sub uri_for_action { if (defined($pp->[0]) && length($pp->[0])); } $parent = $curr->attributes->{Chained}->[0]; - $curr = $self->{actions}{$parent}; + $curr = $self->_actions->{$parent}; } return undef unless $parent eq '/'; # fail for dangling action @@ -315,12 +351,14 @@ sub expand_action { while ($curr) { push @chain, $curr; my $parent = $curr->attributes->{Chained}->[0]; - $curr = $self->{'actions'}{$parent}; + $curr = $self->_actions->{$parent}; } return Catalyst::ActionChain->from_chain([reverse @chain]); } +__PACKAGE__->meta->make_immutable; + =head1 USAGE =head2 Introduction