implemented list and uri_for_action for ChildOf
[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, keys %$children) {
104         my @parts = @$path_parts;
105         if (length $try_part) { # test and strip PathPart
106             next TRY unless
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 @_
111         }
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?
116                 {
117                     local $c->req->{arguments} = [ @{$c->req->args}, @parts ];
118                     next TRY_ACTION unless $action->match($c);
119                 }
120                 push(@{$c->req->args}, @parts);
121                 return [ $action ], [ ];
122             } else {
123                 my @captures;
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]));
128                 }
129                 # try the remaining parts against children of this action
130                 my ($actions, $captures) = $self->recurse_match(
131                                              $c, '/'.$action->reverse, \@parts
132                                            );
133                 if ($actions) {
134                     return [ $action, @$actions ], [ @captures, @$captures ];
135                 }
136             }
137         }
138     }
139     return ();
140 }
141
142 =head2 $self->register( $c, $action )
143
144 Matt is an idiot and hasn't documented this yet.
145
146 =cut
147
148 sub register {
149     my ( $self, $c, $action ) = @_;
150
151     my @child_of_attr = @{ $action->attributes->{ChildOf} || [] };
152
153     return 0 unless @child_of_attr;
154
155     if (@child_of_attr > 2) {
156         Catalyst::Exception->throw(
157           "Multiple ChildOf attributes not supported registering ${action}"
158         );
159     }
160
161     my $parent = $child_of_attr[0];
162
163     if (defined($parent) && length($parent)) {
164         unless ($parent =~ m/^\//) {
165             $parent = '/'.join('/', $action->namespace, $parent);
166         }
167     } else {
168         $parent = '/'.$action->namespace;
169     }
170
171     $action->attributes->{ChildOf} = [ $parent ];
172
173     my $children = ($self->{children_of}{$parent} ||= {});
174
175     my @path_part = @{ $action->attributes->{PathPart} || [] };
176
177     my $part = '';
178
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}"
184         );
185     }
186
187     $action->attributes->{PartPath} = [ $part ];
188
189     unshift(@{ $children->{$part} ||= [] }, $action);
190
191     ($self->{actions} ||= {})->{'/'.$action->reverse} = $action;
192
193     if ($action->attributes->{Args}) {
194         unshift(@{ $self->{endpoints} ||= [] }, $action);
195     }
196
197     return 1;
198 }
199
200 =head2 $self->uri_for_action($action, $captures)
201
202 Matt is an idiot and hasn't documented this yet.
203
204 =cut
205
206 sub uri_for_action {
207     my ( $self, $action, $captures ) = @_;
208
209     return undef unless ($action->attributes->{ChildOf}
210                            && $action->attributes->{Args});
211
212     my @parts = ();
213     my @captures = @$captures;
214     my $parent = "DUMMY";
215     my $curr = $action;
216     while ($curr) {
217         if (my $cap = $curr->attributes->{Captures}) {
218             return undef unless @captures >= $cap->[0]; # not enough captures
219             unshift(@parts, splice(@captures, -$cap->[0]));
220         }
221         if (my $pp = $curr->attributes->{PartPath}) {
222             unshift(@parts, $pp->[0])
223                 if (defined $pp->[0] && length $pp->[0]);
224         }
225         $parent = $curr->attributes->{ChildOf}->[0];
226         $curr = $self->{actions}{$parent};
227     }
228
229     return undef unless $parent eq '/'; # fail for dangling action
230
231     return undef if @captures; # fail for too many captures
232
233     return join('/', '', @parts);
234    
235 }
236
237 =head1 AUTHOR
238
239 Matt S Trout <mst@shadowcatsystems.co.uk>
240
241 =head1 COPYRIGHT
242
243 This program is free software, you can redistribute it and/or modify it under
244 the same terms as Perl itself.
245
246 =cut
247
248 1;