fixed lurking buglet (thanks to jshirley)
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / ChildOf.pm
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
9 # please don't perltidy this. hairy code within.
10
11 =head1 NAME
12
13 Catalyst::DispatchType::ChildOf - Path Part DispatchType
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
27 =cut
28
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) : '...');
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 }
63
64 =head2 $self->match( $c, $path )
65
66 Matt is an idiot and hasn't documented this yet.
67
68 =cut
69
70 sub 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
94 Matt is an idiot and hasn't documented this yet.
95
96 =cut
97
98 sub recurse_match {
99     my ( $self, $c, $parent, $path_parts ) = @_;
100     my $children = $self->{children_of}{$parent};
101     return () unless $children;
102     my @captures;
103     TRY: foreach my $try_part (sort { length($a) <=> length($b) }
104                                    keys %$children) {
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
110                                 @parts, 0, scalar(@{[split('/', $try_part)]})
111                               ))); # @{[]} to avoid split to @_
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
145 Matt is an idiot and hasn't documented this yet.
146
147 =cut
148
149 sub 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
172     $action->attributes->{ChildOf} = [ $parent ];
173
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
188     $action->attributes->{PartPath} = [ $part ];
189
190     unshift(@{ $children->{$part} ||= [] }, $action);
191
192     ($self->{actions} ||= {})->{'/'.$action->reverse} = $action;
193
194     if ($action->attributes->{Args}) {
195         unshift(@{ $self->{endpoints} ||= [] }, $action);
196     }
197
198     return 1;
199 }
200
201 =head2 $self->uri_for_action($action, $captures)
202
203 Matt is an idiot and hasn't documented this yet.
204
205 =cut
206
207 sub uri_for_action {
208     my ( $self, $action, $captures ) = @_;
209
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};
228     }
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    
236 }
237
238 =head1 AUTHOR
239
240 Matt S Trout <mst@shadowcatsystems.co.uk>
241
242 =head1 COPYRIGHT
243
244 This program is free software, you can redistribute it and/or modify it under
245 the same terms as Perl itself.
246
247 =cut
248
249 1;