1 package Catalyst::DispatchType::ChildOf;
4 use base qw/Catalyst::DispatchType/;
6 use Catalyst::ActionChain;
9 # please don't perltidy this. hairy code within.
13 Catalyst::DispatchType::ChildOf - 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) : '...');
47 if (my $cap = $curr->attributes->{Captures}) {
48 unshift(@parts, (("*") x $cap->[0]));
50 if (my $pp = $curr->attributes->{PartPath}) {
51 unshift(@parts, $pp->[0])
52 if (defined $pp->[0] && length $pp->[0]);
54 $parent = $curr->attributes->{ChildOf}->[0];
55 $curr = $self->{actions}{$parent};
57 next ENDPOINT unless $parent eq '/'; # skip dangling action
58 $paths->row(join('/', '', @parts), "/$endpoint");
61 $c->log->debug( "Loaded Path Part actions:\n" . $paths->draw );
64 =head2 $self->match( $c, $path )
66 Matt is an idiot and hasn't documented this yet.
71 my ( $self, $c, $path ) = @_;
73 return 0 if @{$c->req->args};
75 my @parts = split('/', $path);
77 my ($chain, $captures) = $self->recurse_match($c, '/', \@parts);
79 return 0 unless $chain;
81 my $action = Catalyst::ActionChain->from_chain($chain);
83 $c->req->action("/${action}");
84 $c->req->match("/${action}");
85 $c->req->captures($captures);
87 $c->namespace( $action->namespace );
92 =head2 $self->recurse_match( $c, $parent, \@path_parts )
94 Matt is an idiot and hasn't documented this yet.
99 my ( $self, $c, $parent, $path_parts ) = @_;
100 my $children = $self->{children_of}{$parent};
101 return () unless $children;
103 TRY: foreach my $try_part (sort { length($a) <=> length($b) }
105 my @parts = @$path_parts;
106 if (length $try_part) { # test and strip PathPart
108 ($try_part eq join('/', # assemble equal number of parts
109 splice( # and strip them off @parts as well
110 @parts, 0, scalar(@{[split('/', $try_part)]})
111 ))); # @{[]} to avoid split to @_
113 my @try_actions = @{$children->{$try_part}};
114 TRY_ACTION: foreach my $action (@try_actions) {
115 if (my $args_attr = $action->attributes->{Args}) {
116 # XXX alternative non-Args way to identify an endpoint?
118 local $c->req->{arguments} = [ @{$c->req->args}, @parts ];
119 next TRY_ACTION unless $action->match($c);
121 push(@{$c->req->args}, @parts);
122 return [ $action ], [ ];
125 my @parts = @parts; # localise
126 if (my $capture_attr = $action->attributes->{Captures}) {
127 # strip Captures into list
128 push(@captures, splice(@parts, 0, $capture_attr->[0]));
130 # try the remaining parts against children of this action
131 my ($actions, $captures) = $self->recurse_match(
132 $c, '/'.$action->reverse, \@parts
135 return [ $action, @$actions ], [ @captures, @$captures ];
143 =head2 $self->register( $c, $action )
145 Matt is an idiot and hasn't documented this yet.
150 my ( $self, $c, $action ) = @_;
152 my @child_of_attr = @{ $action->attributes->{ChildOf} || [] };
154 return 0 unless @child_of_attr;
156 if (@child_of_attr > 2) {
157 Catalyst::Exception->throw(
158 "Multiple ChildOf attributes not supported registering ${action}"
162 my $parent = $child_of_attr[0];
164 if (defined($parent) && length($parent)) {
165 unless ($parent =~ m/^\//) {
166 $parent = '/'.join('/', $action->namespace, $parent);
169 $parent = '/'.$action->namespace;
172 $action->attributes->{ChildOf} = [ $parent ];
174 my $children = ($self->{children_of}{$parent} ||= {});
176 my @path_part = @{ $action->attributes->{PathPart} || [] };
180 if (@path_part == 1) {
181 $part = (defined $path_part[0] ? $path_part[0] : $action->name);
182 } elsif (@path_part > 1) {
183 Catalyst::Exception->throw(
184 "Multiple PathPart attributes not supported registering ${action}"
188 $action->attributes->{PartPath} = [ $part ];
190 unshift(@{ $children->{$part} ||= [] }, $action);
192 ($self->{actions} ||= {})->{'/'.$action->reverse} = $action;
194 if ($action->attributes->{Args}) {
195 unshift(@{ $self->{endpoints} ||= [] }, $action);
201 =head2 $self->uri_for_action($action, $captures)
203 Matt is an idiot and hasn't documented this yet.
208 my ( $self, $action, $captures ) = @_;
210 return undef unless ($action->attributes->{ChildOf}
211 && $action->attributes->{Args});
214 my @captures = @$captures;
215 my $parent = "DUMMY";
218 if (my $cap = $curr->attributes->{Captures}) {
219 return undef unless @captures >= $cap->[0]; # not enough captures
220 unshift(@parts, splice(@captures, -$cap->[0]));
222 if (my $pp = $curr->attributes->{PartPath}) {
223 unshift(@parts, $pp->[0])
224 if (defined $pp->[0] && length $pp->[0]);
226 $parent = $curr->attributes->{ChildOf}->[0];
227 $curr = $self->{actions}{$parent};
230 return undef unless $parent eq '/'; # fail for dangling action
232 return undef if @captures; # fail for too many captures
234 return join('/', '', @parts);
240 Matt S Trout <mst@shadowcatsystems.co.uk>
244 This program is free software, you can redistribute it and/or modify it under
245 the same terms as Perl itself.