fixed lurking buglet (thanks to jshirley)
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / ChildOf.pm
CommitLineData
141459fa 1package Catalyst::DispatchType::ChildOf;
2
3use strict;
4use base qw/Catalyst::DispatchType/;
5use Text::SimpleTable;
6use Catalyst::ActionChain;
7use URI;
8
792b40ac 9# please don't perltidy this. hairy code within.
10
141459fa 11=head1 NAME
12
792b40ac 13Catalyst::DispatchType::ChildOf - Path Part DispatchType
141459fa 14
15=head1 SYNOPSIS
16
17See L<Catalyst>.
18
19=head1 DESCRIPTION
20
21=head1 METHODS
22
23=head2 $self->list($c)
24
25Debug output for Path Part dispatch points
26
141459fa 27=cut
28
792b40ac 29sub list {
30 my ( $self, $c ) = @_;
31
32 return unless $self->{endpoints};
33
34 my $paths = Text::SimpleTable->new(
35 [ 35, 'Path Spec' ], [ 36, 'Private' ]
36 );
37
38 ENDPOINT: foreach my $endpoint (
39 sort { $a->reverse cmp $b->reverse }
40 @{ $self->{endpoints} }
41 ) {
42 my $args = $endpoint->attributes->{Args}->[0];
43 my @parts = (defined($args) ? (("*") x $args) : '...');
44 my $parent = "DUMMY";
45 my $curr = $endpoint;
46 while ($curr) {
47 if (my $cap = $curr->attributes->{Captures}) {
48 unshift(@parts, (("*") x $cap->[0]));
49 }
50 if (my $pp = $curr->attributes->{PartPath}) {
51 unshift(@parts, $pp->[0])
52 if (defined $pp->[0] && length $pp->[0]);
53 }
54 $parent = $curr->attributes->{ChildOf}->[0];
55 $curr = $self->{actions}{$parent};
56 }
57 next ENDPOINT unless $parent eq '/'; # skip dangling action
58 $paths->row(join('/', '', @parts), "/$endpoint");
59 }
60
61 $c->log->debug( "Loaded Path Part actions:\n" . $paths->draw );
62}
141459fa 63
64=head2 $self->match( $c, $path )
65
66Matt is an idiot and hasn't documented this yet.
67
68=cut
69
70sub match {
71 my ( $self, $c, $path ) = @_;
72
73 return 0 if @{$c->req->args};
74
75 my @parts = split('/', $path);
76
77 my ($chain, $captures) = $self->recurse_match($c, '/', \@parts);
78
79 return 0 unless $chain;
80
81 my $action = Catalyst::ActionChain->from_chain($chain);
82
83 $c->req->action("/${action}");
84 $c->req->match("/${action}");
85 $c->req->captures($captures);
86 $c->action($action);
87 $c->namespace( $action->namespace );
88
89 return 1;
90}
91
92=head2 $self->recurse_match( $c, $parent, \@path_parts )
93
94Matt is an idiot and hasn't documented this yet.
95
96=cut
97
98sub recurse_match {
99 my ( $self, $c, $parent, $path_parts ) = @_;
100 my $children = $self->{children_of}{$parent};
101 return () unless $children;
102 my @captures;
cdc97b63 103 TRY: foreach my $try_part (sort { length($a) <=> length($b) }
104 keys %$children) {
141459fa 105 my @parts = @$path_parts;
106 if (length $try_part) { # test and strip PathPart
107 next TRY unless
108 ($try_part eq join('/', # assemble equal number of parts
109 splice( # and strip them off @parts as well
792b40ac 110 @parts, 0, scalar(@{[split('/', $try_part)]})
111 ))); # @{[]} to avoid split to @_
141459fa 112 }
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?
117 {
118 local $c->req->{arguments} = [ @{$c->req->args}, @parts ];
119 next TRY_ACTION unless $action->match($c);
120 }
121 push(@{$c->req->args}, @parts);
122 return [ $action ], [ ];
123 } else {
124 my @captures;
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]));
129 }
130 # try the remaining parts against children of this action
131 my ($actions, $captures) = $self->recurse_match(
132 $c, '/'.$action->reverse, \@parts
133 );
134 if ($actions) {
135 return [ $action, @$actions ], [ @captures, @$captures ];
136 }
137 }
138 }
139 }
140 return ();
141}
142
143=head2 $self->register( $c, $action )
144
145Matt is an idiot and hasn't documented this yet.
146
147=cut
148
149sub register {
150 my ( $self, $c, $action ) = @_;
151
152 my @child_of_attr = @{ $action->attributes->{ChildOf} || [] };
153
154 return 0 unless @child_of_attr;
155
156 if (@child_of_attr > 2) {
157 Catalyst::Exception->throw(
158 "Multiple ChildOf attributes not supported registering ${action}"
159 );
160 }
161
162 my $parent = $child_of_attr[0];
163
164 if (defined($parent) && length($parent)) {
165 unless ($parent =~ m/^\//) {
166 $parent = '/'.join('/', $action->namespace, $parent);
167 }
168 } else {
169 $parent = '/'.$action->namespace;
170 }
171
792b40ac 172 $action->attributes->{ChildOf} = [ $parent ];
173
141459fa 174 my $children = ($self->{children_of}{$parent} ||= {});
175
176 my @path_part = @{ $action->attributes->{PathPart} || [] };
177
178 my $part = '';
179
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}"
185 );
186 }
187
792b40ac 188 $action->attributes->{PartPath} = [ $part ];
189
141459fa 190 unshift(@{ $children->{$part} ||= [] }, $action);
191
792b40ac 192 ($self->{actions} ||= {})->{'/'.$action->reverse} = $action;
193
194 if ($action->attributes->{Args}) {
195 unshift(@{ $self->{endpoints} ||= [] }, $action);
196 }
197
198 return 1;
141459fa 199}
200
201=head2 $self->uri_for_action($action, $captures)
202
203Matt is an idiot and hasn't documented this yet.
204
205=cut
206
207sub uri_for_action {
208 my ( $self, $action, $captures ) = @_;
209
792b40ac 210 return undef unless ($action->attributes->{ChildOf}
211 && $action->attributes->{Args});
212
213 my @parts = ();
214 my @captures = @$captures;
215 my $parent = "DUMMY";
216 my $curr = $action;
217 while ($curr) {
218 if (my $cap = $curr->attributes->{Captures}) {
219 return undef unless @captures >= $cap->[0]; # not enough captures
220 unshift(@parts, splice(@captures, -$cap->[0]));
221 }
222 if (my $pp = $curr->attributes->{PartPath}) {
223 unshift(@parts, $pp->[0])
224 if (defined $pp->[0] && length $pp->[0]);
225 }
226 $parent = $curr->attributes->{ChildOf}->[0];
227 $curr = $self->{actions}{$parent};
141459fa 228 }
792b40ac 229
230 return undef unless $parent eq '/'; # fail for dangling action
231
232 return undef if @captures; # fail for too many captures
233
234 return join('/', '', @parts);
235
141459fa 236}
237
238=head1 AUTHOR
239
792b40ac 240Matt S Trout <mst@shadowcatsystems.co.uk>
141459fa 241
242=head1 COPYRIGHT
243
244This program is free software, you can redistribute it and/or modify it under
245the same terms as Perl itself.
246
247=cut
248
2491;