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