1 package Catalyst::DispatchType::Chained;
4 use base qw/Catalyst::DispatchType/;
6 use Catalyst::ActionChain;
9 # please don't perltidy this. hairy code within.
13 Catalyst::DispatchType::Chained - Path Part DispatchType
23 =head2 $self->list($c)
25 Debug output for Path Part dispatch points
30 my ( $self, $c ) = @_;
32 return unless $self->{endpoints};
34 my $paths = Text::SimpleTable->new(
35 [ 35, 'Path Spec' ], [ 36, 'Private' ]
38 ENDPOINT: foreach my $endpoint (
39 sort { $a->reverse cmp $b->reverse }
40 @{ $self->{endpoints} }
42 my $args = $endpoint->attributes->{Args}->[0];
43 my @parts = (defined($args) ? (("*") x $args) : '...');
48 if (my $cap = $curr->attributes->{Captures}) {
49 unshift(@parts, (("*") x $cap->[0]));
51 if (my $pp = $curr->attributes->{PartPath}) {
52 unshift(@parts, $pp->[0])
53 if (defined $pp->[0] && length $pp->[0]);
55 $parent = $curr->attributes->{Chained}->[0];
56 $curr = $self->{actions}{$parent};
57 unshift(@parents, $curr) if $curr;
59 next ENDPOINT unless $parent eq '/'; # skip dangling action
61 foreach my $p (@parents) {
63 if (my $cap = $p->attributes->{Captures}) {
64 $name .= ' ('.$cap->[0].')';
66 unless ($p eq $parents[0]) {
69 push(@rows, [ '', $name ]);
71 push(@rows, [ '', (@rows ? "=> " : '')."/${endpoint}" ]);
72 $rows[0][0] = join('/', '', @parts);
73 $paths->row(@$_) for @rows;
76 $c->log->debug( "Loaded Path Part actions:\n" . $paths->draw );
79 =head2 $self->match( $c, $path )
81 Matt is an idiot and hasn't documented this yet.
86 my ( $self, $c, $path ) = @_;
88 return 0 if @{$c->req->args};
90 my @parts = split('/', $path);
92 my ($chain, $captures) = $self->recurse_match($c, '/', \@parts);
94 return 0 unless $chain;
96 my $action = Catalyst::ActionChain->from_chain($chain);
98 $c->req->action("/${action}");
99 $c->req->match("/${action}");
100 $c->req->captures($captures);
102 $c->namespace( $action->namespace );
107 =head2 $self->recurse_match( $c, $parent, \@path_parts )
109 Matt is an idiot and hasn't documented this yet.
114 my ( $self, $c, $parent, $path_parts ) = @_;
115 my $children = $self->{children_of}{$parent};
116 return () unless $children;
118 TRY: foreach my $try_part (sort { length($a) <=> length($b) }
120 my @parts = @$path_parts;
121 if (length $try_part) { # test and strip PathPart
123 ($try_part eq join('/', # assemble equal number of parts
124 splice( # and strip them off @parts as well
125 @parts, 0, scalar(@{[split('/', $try_part)]})
126 ))); # @{[]} to avoid split to @_
128 my @try_actions = @{$children->{$try_part}};
129 TRY_ACTION: foreach my $action (@try_actions) {
130 if (my $capture_attr = $action->attributes->{Captures}) {
132 my @parts = @parts; # localise
134 # strip Captures into list
135 push(@captures, splice(@parts, 0, $capture_attr->[0]));
137 # try the remaining parts against children of this action
138 my ($actions, $captures) = $self->recurse_match(
139 $c, '/'.$action->reverse, \@parts
142 return [ $action, @$actions ], [ @captures, @$captures ];
146 local $c->req->{arguments} = [ @{$c->req->args}, @parts ];
147 next TRY_ACTION unless $action->match($c);
149 push(@{$c->req->args}, @parts);
150 return [ $action ], [ ];
157 =head2 $self->register( $c, $action )
159 Matt is an idiot and hasn't documented this yet.
164 my ( $self, $c, $action ) = @_;
166 my @chained_attr = @{ $action->attributes->{Chained} || [] };
168 return 0 unless @chained_attr;
170 if (@chained_attr > 2) {
171 Catalyst::Exception->throw(
172 "Multiple Chained attributes not supported registering ${action}"
176 my $parent = $chained_attr[0];
178 if (defined($parent) && length($parent)) {
179 if ($parent eq '.') {
180 $parent = '/'.$action->namespace;
181 } elsif ($parent !~ m/^\//) {
182 $parent = '/'.join('/', $action->namespace, $parent);
188 $action->attributes->{Chained} = [ $parent ];
190 my $children = ($self->{children_of}{$parent} ||= {});
192 my @path_part = @{ $action->attributes->{PathPart} || [] };
194 my $part = $action->name;
196 if (@path_part == 1 && defined $path_part[0]) {
197 $part = $path_part[0];
198 } elsif (@path_part > 1) {
199 Catalyst::Exception->throw(
200 "Multiple PathPart attributes not supported registering ${action}"
204 $action->attributes->{PartPath} = [ $part ];
206 unshift(@{ $children->{$part} ||= [] }, $action);
208 ($self->{actions} ||= {})->{'/'.$action->reverse} = $action;
210 unless ($action->attributes->{Captures}) {
211 unshift(@{ $self->{endpoints} ||= [] }, $action);
217 =head2 $self->uri_for_action($action, $captures)
219 Matt is an idiot and hasn't documented this yet.
224 my ( $self, $action, $captures ) = @_;
226 return undef unless ($action->attributes->{Chained}
227 && $action->attributes->{Args});
230 my @captures = @$captures;
231 my $parent = "DUMMY";
234 if (my $cap = $curr->attributes->{Captures}) {
235 return undef unless @captures >= $cap->[0]; # not enough captures
236 unshift(@parts, splice(@captures, -$cap->[0]));
238 if (my $pp = $curr->attributes->{PartPath}) {
239 unshift(@parts, $pp->[0])
240 if (defined $pp->[0] && length $pp->[0]);
242 $parent = $curr->attributes->{Chained}->[0];
243 $curr = $self->{actions}{$parent};
246 return undef unless $parent eq '/'; # fail for dangling action
248 return undef if @captures; # fail for too many captures
250 return join('/', '', @parts);
256 Matt S Trout <mst@shadowcatsystems.co.uk>
260 This program is free software, you can redistribute it and/or modify it under
261 the same terms as Perl itself.