first cut at :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 =head1 NAME
10
11 Catalyst::DispatchType::Path - Path DispatchType
12
13 =head1 SYNOPSIS
14
15 See L<Catalyst>.
16
17 =head1 DESCRIPTION
18
19 =head1 METHODS
20
21 =head2 $self->list($c)
22
23 Debug output for Path Part dispatch points
24
25 Matt is an idiot and hasn't implemented this yet.
26
27 =cut
28
29 #sub list {
30 #    my ( $self, $c ) = @_;
31 #    my $paths = Text::SimpleTable->new( [ 35, 'Path' ], [ 36, 'Private' ] );
32 #    foreach my $path ( sort keys %{ $self->{paths} } ) {
33 #        foreach my $action ( @{ $self->{paths}->{$path} } ) {
34 #            $path = "/$path" unless $path eq '/';
35 #            $paths->row( "$path", "/$action" );
36 #        }
37 #    }
38 #    $c->log->debug( "Loaded Path actions:\n" . $paths->draw )
39 #      if ( keys %{ $self->{paths} } );
40 #}
41
42 =head2 $self->match( $c, $path )
43
44 Matt is an idiot and hasn't documented this yet.
45
46 =cut
47
48 sub match {
49     my ( $self, $c, $path ) = @_;
50
51     return 0 if @{$c->req->args};
52
53     my @parts = split('/', $path);
54
55     my ($chain, $captures) = $self->recurse_match($c, '/', \@parts);
56
57     return 0 unless $chain;
58
59     my $action = Catalyst::ActionChain->from_chain($chain);
60
61     $c->req->action("/${action}");
62     $c->req->match("/${action}");
63     $c->req->captures($captures);
64     $c->action($action);
65     $c->namespace( $action->namespace );
66
67     return 1;
68 }
69
70 =head2 $self->recurse_match( $c, $parent, \@path_parts )
71
72 Matt is an idiot and hasn't documented this yet.
73
74 =cut
75
76 sub recurse_match {
77     my ( $self, $c, $parent, $path_parts ) = @_;
78     my $children = $self->{children_of}{$parent};
79     return () unless $children;
80     my @captures;
81     TRY: foreach my $try_part (sort length, keys %$children) {
82         my @parts = @$path_parts;
83         if (length $try_part) { # test and strip PathPart
84             next TRY unless
85               ($try_part eq join('/', # assemble equal number of parts
86                               splice( # and strip them off @parts as well
87                                 @parts, 0, scalar(split('/', $try_part))
88                               )));
89         }
90         my @try_actions = @{$children->{$try_part}};
91         TRY_ACTION: foreach my $action (@try_actions) {
92             if (my $args_attr = $action->attributes->{Args}) {
93                 # XXX alternative non-Args way to identify an endpoint?
94                 {
95                     local $c->req->{arguments} = [ @{$c->req->args}, @parts ];
96                     next TRY_ACTION unless $action->match($c);
97                 }
98                 push(@{$c->req->args}, @parts);
99                 return [ $action ], [ ];
100             } else {
101                 my @captures;
102                 my @parts = @parts; # localise
103                 if (my $capture_attr = $action->attributes->{Captures}) {
104                     # strip Captures into list
105                     push(@captures, splice(@parts, 0, $capture_attr->[0]));
106                 }
107                 # try the remaining parts against children of this action
108                 my ($actions, $captures) = $self->recurse_match(
109                                              $c, '/'.$action->reverse, \@parts
110                                            );
111                 if ($actions) {
112                     return [ $action, @$actions ], [ @captures, @$captures ];
113                 }
114             }
115         }
116     }
117     return ();
118 }
119
120 =head2 $self->register( $c, $action )
121
122 Matt is an idiot and hasn't documented this yet.
123
124 =cut
125
126 sub register {
127     my ( $self, $c, $action ) = @_;
128
129     my @child_of_attr = @{ $action->attributes->{ChildOf} || [] };
130
131     return 0 unless @child_of_attr;
132
133     if (@child_of_attr > 2) {
134         Catalyst::Exception->throw(
135           "Multiple ChildOf attributes not supported registering ${action}"
136         );
137     }
138
139     my $parent = $child_of_attr[0];
140
141     if (defined($parent) && length($parent)) {
142         unless ($parent =~ m/^\//) {
143             $parent = '/'.join('/', $action->namespace, $parent);
144         }
145     } else {
146         $parent = '/'.$action->namespace;
147     }
148
149     my $children = ($self->{children_of}{$parent} ||= {});
150
151     my @path_part = @{ $action->attributes->{PathPart} || [] };
152
153     my $part = '';
154
155     if (@path_part == 1) {
156         $part = (defined $path_part[0] ? $path_part[0] : $action->name);
157     } elsif (@path_part > 1) {
158         Catalyst::Exception->throw(
159           "Multiple PathPart attributes not supported registering ${action}"
160         );
161     }
162
163     unshift(@{ $children->{$part} ||= [] }, $action);
164
165 }
166
167 =head2 $self->uri_for_action($action, $captures)
168
169 Matt is an idiot and hasn't documented this yet.
170
171 =cut
172
173 sub uri_for_action {
174     my ( $self, $action, $captures ) = @_;
175
176     return undef if @$captures;
177
178     if (my $paths = $action->attributes->{Path}) {
179         my $path = $paths->[0];
180         $path = '/' unless length($path);
181         $path = "/${path}" unless ($path =~ m/^\//);
182         $path = URI->new($path)->canonical;
183         return $path;
184     } else {
185         return undef;
186     }
187 }
188
189 =head1 AUTHOR
190
191 Matt S Trout
192 Sebastian Riedel, C<sri@cpan.org>
193
194 =head1 COPYRIGHT
195
196 This program is free software, you can redistribute it and/or modify it under
197 the same terms as Perl itself.
198
199 =cut
200
201 1;