1 package Catalyst::DispatchType::ChildOf;
4 use base qw/Catalyst::DispatchType/;
6 use Catalyst::ActionChain;
11 Catalyst::DispatchType::Path - Path DispatchType
21 =head2 $self->list($c)
23 Debug output for Path Part dispatch points
25 Matt is an idiot and hasn't implemented this yet.
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" );
38 # $c->log->debug( "Loaded Path actions:\n" . $paths->draw )
39 # if ( keys %{ $self->{paths} } );
42 =head2 $self->match( $c, $path )
44 Matt is an idiot and hasn't documented this yet.
49 my ( $self, $c, $path ) = @_;
51 return 0 if @{$c->req->args};
53 my @parts = split('/', $path);
55 my ($chain, $captures) = $self->recurse_match($c, '/', \@parts);
57 return 0 unless $chain;
59 my $action = Catalyst::ActionChain->from_chain($chain);
61 $c->req->action("/${action}");
62 $c->req->match("/${action}");
63 $c->req->captures($captures);
65 $c->namespace( $action->namespace );
70 =head2 $self->recurse_match( $c, $parent, \@path_parts )
72 Matt is an idiot and hasn't documented this yet.
77 my ( $self, $c, $parent, $path_parts ) = @_;
78 my $children = $self->{children_of}{$parent};
79 return () unless $children;
81 TRY: foreach my $try_part (sort length, keys %$children) {
82 my @parts = @$path_parts;
83 if (length $try_part) { # test and strip PathPart
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))
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?
95 local $c->req->{arguments} = [ @{$c->req->args}, @parts ];
96 next TRY_ACTION unless $action->match($c);
98 push(@{$c->req->args}, @parts);
99 return [ $action ], [ ];
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]));
107 # try the remaining parts against children of this action
108 my ($actions, $captures) = $self->recurse_match(
109 $c, '/'.$action->reverse, \@parts
112 return [ $action, @$actions ], [ @captures, @$captures ];
120 =head2 $self->register( $c, $action )
122 Matt is an idiot and hasn't documented this yet.
127 my ( $self, $c, $action ) = @_;
129 my @child_of_attr = @{ $action->attributes->{ChildOf} || [] };
131 return 0 unless @child_of_attr;
133 if (@child_of_attr > 2) {
134 Catalyst::Exception->throw(
135 "Multiple ChildOf attributes not supported registering ${action}"
139 my $parent = $child_of_attr[0];
141 if (defined($parent) && length($parent)) {
142 unless ($parent =~ m/^\//) {
143 $parent = '/'.join('/', $action->namespace, $parent);
146 $parent = '/'.$action->namespace;
149 my $children = ($self->{children_of}{$parent} ||= {});
151 my @path_part = @{ $action->attributes->{PathPart} || [] };
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}"
163 unshift(@{ $children->{$part} ||= [] }, $action);
167 =head2 $self->uri_for_action($action, $captures)
169 Matt is an idiot and hasn't documented this yet.
174 my ( $self, $action, $captures ) = @_;
176 return undef if @$captures;
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;
192 Sebastian Riedel, C<sri@cpan.org>
196 This program is free software, you can redistribute it and/or modify it under
197 the same terms as Perl itself.