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, keys %$children) {
104 my @parts = @$path_parts;
105 if (length $try_part) { # test and strip PathPart
107 ($try_part eq join('/', # assemble equal number of parts
108 splice( # and strip them off @parts as well
109 @parts, 0, scalar(@{[split('/', $try_part)]})
110 ))); # @{[]} to avoid split to @_
112 my @try_actions = @{$children->{$try_part}};
113 TRY_ACTION: foreach my $action (@try_actions) {
114 if (my $args_attr = $action->attributes->{Args}) {
115 # XXX alternative non-Args way to identify an endpoint?
117 local $c->req->{arguments} = [ @{$c->req->args}, @parts ];
118 next TRY_ACTION unless $action->match($c);
120 push(@{$c->req->args}, @parts);
121 return [ $action ], [ ];
124 my @parts = @parts; # localise
125 if (my $capture_attr = $action->attributes->{Captures}) {
126 # strip Captures into list
127 push(@captures, splice(@parts, 0, $capture_attr->[0]));
129 # try the remaining parts against children of this action
130 my ($actions, $captures) = $self->recurse_match(
131 $c, '/'.$action->reverse, \@parts
134 return [ $action, @$actions ], [ @captures, @$captures ];
142 =head2 $self->register( $c, $action )
144 Matt is an idiot and hasn't documented this yet.
149 my ( $self, $c, $action ) = @_;
151 my @child_of_attr = @{ $action->attributes->{ChildOf} || [] };
153 return 0 unless @child_of_attr;
155 if (@child_of_attr > 2) {
156 Catalyst::Exception->throw(
157 "Multiple ChildOf attributes not supported registering ${action}"
161 my $parent = $child_of_attr[0];
163 if (defined($parent) && length($parent)) {
164 unless ($parent =~ m/^\//) {
165 $parent = '/'.join('/', $action->namespace, $parent);
168 $parent = '/'.$action->namespace;
171 $action->attributes->{ChildOf} = [ $parent ];
173 my $children = ($self->{children_of}{$parent} ||= {});
175 my @path_part = @{ $action->attributes->{PathPart} || [] };
179 if (@path_part == 1) {
180 $part = (defined $path_part[0] ? $path_part[0] : $action->name);
181 } elsif (@path_part > 1) {
182 Catalyst::Exception->throw(
183 "Multiple PathPart attributes not supported registering ${action}"
187 $action->attributes->{PartPath} = [ $part ];
189 unshift(@{ $children->{$part} ||= [] }, $action);
191 ($self->{actions} ||= {})->{'/'.$action->reverse} = $action;
193 if ($action->attributes->{Args}) {
194 unshift(@{ $self->{endpoints} ||= [] }, $action);
200 =head2 $self->uri_for_action($action, $captures)
202 Matt is an idiot and hasn't documented this yet.
207 my ( $self, $action, $captures ) = @_;
209 return undef unless ($action->attributes->{ChildOf}
210 && $action->attributes->{Args});
213 my @captures = @$captures;
214 my $parent = "DUMMY";
217 if (my $cap = $curr->attributes->{Captures}) {
218 return undef unless @captures >= $cap->[0]; # not enough captures
219 unshift(@parts, splice(@captures, -$cap->[0]));
221 if (my $pp = $curr->attributes->{PartPath}) {
222 unshift(@parts, $pp->[0])
223 if (defined $pp->[0] && length $pp->[0]);
225 $parent = $curr->attributes->{ChildOf}->[0];
226 $curr = $self->{actions}{$parent};
229 return undef unless $parent eq '/'; # fail for dangling action
231 return undef if @captures; # fail for too many captures
233 return join('/', '', @parts);
239 Matt S Trout <mst@shadowcatsystems.co.uk>
243 This program is free software, you can redistribute it and/or modify it under
244 the same terms as Perl itself.