no :PathPart -> :PathPart('subname')
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / ChildOf.pm
index 094337d..2ab537e 100644 (file)
@@ -6,9 +6,11 @@ use Text::SimpleTable;
 use Catalyst::ActionChain;
 use URI;
 
+# please don't perltidy this. hairy code within.
+
 =head1 NAME
 
-Catalyst::DispatchType::Path - Path DispatchType
+Catalyst::DispatchType::ChildOf - Path Part DispatchType
 
 =head1 SYNOPSIS
 
@@ -22,22 +24,57 @@ See L<Catalyst>.
 
 Debug output for Path Part dispatch points
 
-Matt is an idiot and hasn't implemented this yet.
-
 =cut
 
-#sub list {
-#    my ( $self, $c ) = @_;
-#    my $paths = Text::SimpleTable->new( [ 35, 'Path' ], [ 36, 'Private' ] );
-#    foreach my $path ( sort keys %{ $self->{paths} } ) {
-#        foreach my $action ( @{ $self->{paths}->{$path} } ) {
-#            $path = "/$path" unless $path eq '/';
-#            $paths->row( "$path", "/$action" );
-#        }
-#    }
-#    $c->log->debug( "Loaded Path actions:\n" . $paths->draw )
-#      if ( keys %{ $self->{paths} } );
-#}
+sub list {
+    my ( $self, $c ) = @_;
+
+    return unless $self->{endpoints};
+
+    my $paths = Text::SimpleTable->new(
+                    [ 35, 'Path Spec' ], [ 36, 'Private' ]
+                );
+
+    ENDPOINT: foreach my $endpoint (
+                  sort { $a->reverse cmp $b->reverse }
+                           @{ $self->{endpoints} }
+                  ) {
+        my $args = $endpoint->attributes->{Args}->[0];
+        my @parts = (defined($args) ? (("*") x $args) : '...');
+        my @parents = ();
+        my $parent = "DUMMY";
+        my $curr = $endpoint;
+        while ($curr) {
+            if (my $cap = $curr->attributes->{Captures}) {
+                unshift(@parts, (("*") x $cap->[0]));
+            }
+            if (my $pp = $curr->attributes->{PartPath}) {
+                unshift(@parts, $pp->[0])
+                    if (defined $pp->[0] && length $pp->[0]);
+            }
+            $parent = $curr->attributes->{ChildOf}->[0];
+            $curr = $self->{actions}{$parent};
+            unshift(@parents, $curr) if $curr;
+        }
+        next ENDPOINT unless $parent eq '/'; # skip dangling action
+        my @rows;
+        foreach my $p (@parents) {
+            my $name = "/${p}";
+            if (my $cap = $p->attributes->{Captures}) {
+                $name .= ' ('.$cap->[0].')';
+            }
+            unless ($p eq $parents[0]) {
+                $name = "-> ${name}";
+            }
+            push(@rows, [ '', $name ]);
+        }
+        push(@rows, [ '', (@rows ? "=> " : '')."/${endpoint}" ]);
+        $rows[0][0] = join('/', '', @parts);
+        $paths->row(@$_) for @rows;
+    }
+
+    $c->log->debug( "Loaded Path Part actions:\n" . $paths->draw );
+}
 
 =head2 $self->match( $c, $path )
 
@@ -78,32 +115,25 @@ sub recurse_match {
     my $children = $self->{children_of}{$parent};
     return () unless $children;
     my @captures;
-    TRY: foreach my $try_part (sort length, keys %$children) {
+    TRY: foreach my $try_part (sort { length($a) <=> length($b) }
+                                   keys %$children) {
         my @parts = @$path_parts;
         if (length $try_part) { # test and strip PathPart
             next TRY unless
               ($try_part eq join('/', # assemble equal number of parts
                               splice( # and strip them off @parts as well
-                                @parts, 0, scalar(split('/', $try_part))
-                              )));
+                                @parts, 0, scalar(@{[split('/', $try_part)]})
+                              ))); # @{[]} to avoid split to @_
         }
         my @try_actions = @{$children->{$try_part}};
         TRY_ACTION: foreach my $action (@try_actions) {
-            if (my $args_attr = $action->attributes->{Args}) {
-                # XXX alternative non-Args way to identify an endpoint?
-                {
-                    local $c->req->{arguments} = [ @{$c->req->args}, @parts ];
-                    next TRY_ACTION unless $action->match($c);
-                }
-                push(@{$c->req->args}, @parts);
-                return [ $action ], [ ];
-            } else {
+            if (my $capture_attr = $action->attributes->{Captures}) {
                 my @captures;
                 my @parts = @parts; # localise
-                if (my $capture_attr = $action->attributes->{Captures}) {
-                    # strip Captures into list
-                    push(@captures, splice(@parts, 0, $capture_attr->[0]));
-                }
+
+                # strip Captures into list
+                push(@captures, splice(@parts, 0, $capture_attr->[0]));
+
                 # try the remaining parts against children of this action
                 my ($actions, $captures) = $self->recurse_match(
                                              $c, '/'.$action->reverse, \@parts
@@ -111,6 +141,13 @@ sub recurse_match {
                 if ($actions) {
                     return [ $action, @$actions ], [ @captures, @$captures ];
                 }
+            } else {
+                {
+                    local $c->req->{arguments} = [ @{$c->req->args}, @parts ];
+                    next TRY_ACTION unless $action->match($c);
+                }
+                push(@{$c->req->args}, @parts);
+                return [ $action ], [ ];
             }
         }
     }
@@ -146,22 +183,33 @@ sub register {
         $parent = '/'.$action->namespace;
     }
 
+    $action->attributes->{ChildOf} = [ $parent ];
+
     my $children = ($self->{children_of}{$parent} ||= {});
 
     my @path_part = @{ $action->attributes->{PathPart} || [] };
 
-    my $part = '';
+    my $part = $action->name;
 
-    if (@path_part == 1) {
-        $part = (defined $path_part[0] ? $path_part[0] : $action->name);
+    if (@path_part == 1 && defined $path_part[0]) {
+        $part = $path_part[0];
     } elsif (@path_part > 1) {
         Catalyst::Exception->throw(
           "Multiple PathPart attributes not supported registering ${action}"
         );
     }
 
+    $action->attributes->{PartPath} = [ $part ];
+
     unshift(@{ $children->{$part} ||= [] }, $action);
 
+    ($self->{actions} ||= {})->{'/'.$action->reverse} = $action;
+
+    unless ($action->attributes->{Captures}) {
+        unshift(@{ $self->{endpoints} ||= [] }, $action);
+    }
+
+    return 1;
 }
 
 =head2 $self->uri_for_action($action, $captures)
@@ -173,23 +221,37 @@ Matt is an idiot and hasn't documented this yet.
 sub uri_for_action {
     my ( $self, $action, $captures ) = @_;
 
-    return undef if @$captures;
-
-    if (my $paths = $action->attributes->{Path}) {
-        my $path = $paths->[0];
-        $path = '/' unless length($path);
-        $path = "/${path}" unless ($path =~ m/^\//);
-        $path = URI->new($path)->canonical;
-        return $path;
-    } else {
-        return undef;
+    return undef unless ($action->attributes->{ChildOf}
+                           && $action->attributes->{Args});
+
+    my @parts = ();
+    my @captures = @$captures;
+    my $parent = "DUMMY";
+    my $curr = $action;
+    while ($curr) {
+        if (my $cap = $curr->attributes->{Captures}) {
+            return undef unless @captures >= $cap->[0]; # not enough captures
+            unshift(@parts, splice(@captures, -$cap->[0]));
+        }
+        if (my $pp = $curr->attributes->{PartPath}) {
+            unshift(@parts, $pp->[0])
+                if (defined $pp->[0] && length $pp->[0]);
+        }
+        $parent = $curr->attributes->{ChildOf}->[0];
+        $curr = $self->{actions}{$parent};
     }
+
+    return undef unless $parent eq '/'; # fail for dangling action
+
+    return undef if @captures; # fail for too many captures
+
+    return join('/', '', @parts);
+   
 }
 
 =head1 AUTHOR
 
-Matt S Trout
-Sebastian Riedel, C<sri@cpan.org>
+Matt S Trout <mst@shadowcatsystems.co.uk>
 
 =head1 COPYRIGHT