improved forward()
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine.pm
index 7b07f5d..98766fb 100644 (file)
@@ -74,105 +74,6 @@ sub action {
     return 1;
 }
 
-=item $c->get_action( $action, $namespace )
-
-Get an action in a given namespace.
-
-=cut
-
-sub get_action {
-    my ( $c, $action, $namespace ) = @_;
-    $namespace ||= '';
-    if ( $action =~ /^\!(.*)/ ) {
-        $action = $1;
-        my $parent = $c->tree;
-        my @results;
-        my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
-        push @results, [$result] if $result;
-        my $visitor = Tree::Simple::Visitor::FindByPath->new;
-        for my $part ( split '/', $namespace ) {
-            $visitor->setSearchPath($part);
-            $parent->accept($visitor);
-            my $child = $visitor->getResult;
-            my $uid   = $child->getUID if $child;
-            my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
-            push @results, [$match] if $match;
-            $parent = $child if $child;
-        }
-        return \@results;
-    }
-    elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
-    elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
-    else {
-        for my $regex ( keys %{ $c->actions->{compiled} } ) {
-            my $name = $c->actions->{compiled}->{$regex};
-            if ( $action =~ $regex ) {
-                my @snippets;
-                for my $i ( 1 .. 9 ) {
-                    no strict 'refs';
-                    last unless ${$i};
-                    push @snippets, ${$i};
-                }
-                return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
-            }
-        }
-    }
-    return [];
-}
-
-=item $c->set_action( $action, $code, $namespace )
-
-Set an action in a given namespace.
-
-=cut
-
-sub set_action {
-    my ( $c, $action, $code, $namespace ) = @_;
-
-    my $prefix = '';
-    if ( $action =~ /^\?(.*)$/ ) {
-        my $prefix = $1 || '';
-        $action = $2;
-        $action = $prefix . _prefix( $namespace, $action );
-        $c->actions->{plain}->{$action} = [ $namespace, $code ];
-    }
-    if ( $action =~ /^\/(.*)\/$/ ) {
-        my $regex = $1;
-        $c->actions->{compiled}->{qr#$regex#} = $action;
-        $c->actions->{regex}->{$action} = [ $namespace, $code ];
-    }
-    elsif ( $action =~ /^\!(.*)$/ ) {
-        $action = $1;
-        my $parent  = $c->tree;
-        my $visitor = Tree::Simple::Visitor::FindByPath->new;
-        $prefix = _class2prefix($namespace);
-        for my $part ( split '/', $prefix ) {
-            $visitor->setSearchPath($part);
-            $parent->accept($visitor);
-            my $child = $visitor->getResult;
-            unless ($child) {
-                $child = $parent->addChild( Tree::Simple->new($part) );
-                $visitor->setSearchPath($part);
-                $parent->accept($visitor);
-                $child = $visitor->getResult;
-            }
-            $parent = $child;
-        }
-        my $uid = $parent->getUID;
-        $c->actions->{private}->{$uid}->{$action} = [ $namespace, $code ];
-        $action = "!$action";
-    }
-    else { 
-       $c->actions->{plain}->{$action} = [ $namespace, $code ];
-    }
-
-    my $reverse = $prefix ? "$action ($prefix)" : $action;
-    $c->actions->{reverse}->{"$code"} = $reverse;
-
-    $c->log->debug(qq/"$namespace" defined "$action" as "$code"/)
-      if $c->debug;
-}
-
 =item $c->benchmark($coderef)
 
 Takes a coderef with arguments and returns elapsed time as float.
@@ -385,6 +286,7 @@ If you define a class without method it will default to process().
 sub forward {
     my $c       = shift;
     my $command = shift;
+    $c->state(0);
     unless ($command) {
         $c->log->debug('Nothing to forward to') if $c->debug;
         return 0;
@@ -398,23 +300,15 @@ sub forward {
     if ( $command =~ /^\!/ ) {
         $namespace = _class2prefix($caller);
     }
-    if ( my $results = $c->get_action( $command, $namespace ) ) {
-        if ( $command =~ /^\!/ ) {
-            for my $result ( @{$results} ) {
-                my ( $class, $code ) = @{ $result->[0] };
-                $c->state( $c->process( $class, $code ) );
-            }
-        }
-        else {
-            return 0 unless my $result = $results->[0];
-            if ( $result->[2] ) {
+    my $results = $c->get_action( $command, $namespace );
+    if ( @{$results} ) {
+        unless ( $command =~ /^\!/ ) {
+            $results = [ pop @{$results} ];
+            if ( $results->[0]->[2] ) {
                 $c->log->debug(qq/Couldn't forward "$command" to regex action/)
                   if $c->debug;
                 return 0;
             }
-            my ( $class, $code ) = @{ $result->[0] };
-            $class = $c->components->{$class} || $class;
-            $c->state( $c->process( $class, $code ) );
         }
     }
     else {
@@ -426,7 +320,7 @@ sub forward {
         my $method = shift || 'process';
         if ( my $code = $class->can($method) ) {
             $c->actions->{reverse}->{"$code"} = "$class->$method";
-            $c->state( $c->process( $class, $code ) );
+            $results = [ [ [ $class, $code ] ] ];
         }
         else {
             $c->log->debug(qq/Couldn't forward to "$class->$method"/)
@@ -434,16 +328,72 @@ sub forward {
             return 0;
         }
     }
+    for my $result ( @{$results} ) {
+        my ( $class, $code ) = @{ $result->[0] };
+        $class = $c->comp->{$class} || $class;
+        $c->state( $c->process( $class, $code ) );
+    }
     return $c->state;
 }
 
-=item $c->handler($r)
+=item $c->get_action( $action, $namespace )
+
+Get an action in a given namespace.
+
+=cut
+
+sub get_action {
+    my ( $c, $action, $namespace ) = @_;
+    $namespace ||= '';
+    if ( $action =~ /^\!(.*)/ ) {
+        $action = $1;
+        my $parent = $c->tree;
+        my @results;
+        my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
+        push @results, [$result] if $result;
+        my $visitor = Tree::Simple::Visitor::FindByPath->new;
+        my $local;
+        for my $part ( split '/', $namespace ) {
+            $local = undef;
+            $visitor->setSearchPath($part);
+            $parent->accept($visitor);
+            my $child = $visitor->getResult;
+            my $uid   = $child->getUID if $child;
+            my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
+            return [ [$match] ] if ( $match && $match =~ /^?.*/ );
+            $local = $c->actions->{private}->{$uid}->{"?$action"} if $uid;
+            push @results, [$match] if $match;
+            $parent = $child if $child;
+        }
+        return [ [$local] ] if $local;
+        return \@results;
+    }
+    elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
+    elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
+    else {
+        for my $regex ( keys %{ $c->actions->{compiled} } ) {
+            my $name = $c->actions->{compiled}->{$regex};
+            if ( $action =~ $regex ) {
+                my @snippets;
+                for my $i ( 1 .. 9 ) {
+                    no strict 'refs';
+                    last unless ${$i};
+                    push @snippets, ${$i};
+                }
+                return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
+            }
+        }
+    }
+    return [];
+}
+
+=item $c->handler( $class, $r )
 
 Handles the request.
 
 =cut
 
-sub handler {
+sub handler ($$) {
     my ( $class, $r ) = @_;
 
     # Always expect worst case!
@@ -713,6 +663,53 @@ Returns a C<Catalyst::Response> object.
 
     my $res = $c->res;
 
+=item $c->set_action( $action, $code, $namespace )
+
+Set an action in a given namespace.
+
+=cut
+
+sub set_action {
+    my ( $c, $action, $code, $namespace ) = @_;
+    my $prefix = '';
+    if ( $action =~ /^\?(.*)$/ ) {
+        my $prefix = $1 || '';
+        $action = $2;
+        $action = $prefix . _prefix( $namespace, $action );
+        $c->actions->{plain}->{$action} = [ $namespace, $code ];
+    }
+    elsif ( $action =~ /^\/(.*)\/$/ ) {
+        my $regex = $1;
+        $c->actions->{compiled}->{qr#$regex#} = $action;
+        $c->actions->{regex}->{$action} = [ $namespace, $code ];
+    }
+    elsif ( $action =~ /^\!(.*)$/ ) {
+        $action = $1;
+        my $parent  = $c->tree;
+        my $visitor = Tree::Simple::Visitor::FindByPath->new;
+        $prefix = _class2prefix($namespace);
+        for my $part ( split '/', $prefix ) {
+            $visitor->setSearchPath($part);
+            $parent->accept($visitor);
+            my $child = $visitor->getResult;
+            unless ($child) {
+                $child = $parent->addChild( Tree::Simple->new($part) );
+                $visitor->setSearchPath($part);
+                $parent->accept($visitor);
+                $child = $visitor->getResult;
+            }
+            $parent = $child;
+        }
+        my $uid = $parent->getUID;
+        $c->actions->{private}->{$uid}->{$action} = [ $namespace, $code ];
+        $action = "!$action";
+    }
+    else { $c->actions->{plain}->{$action} = [ $namespace, $code ] }
+    my $reverse = $prefix ? "$action ($prefix)" : $action;
+    $c->actions->{reverse}->{"$code"} = $reverse;
+    $c->log->debug(qq/"$namespace" defined "$action" as "$code"/) if $c->debug;
+}
+
 =item $class->setup
 
 Setup.
@@ -789,6 +786,7 @@ sub stash {
 sub _prefix {
     my ( $class, $name ) = @_;
     my $prefix = _class2prefix($class);
+    warn "$class - $name - $prefix";
     $name = "$prefix/$name" if $prefix;
     return $name;
 }