- Added some docs to dispatcher modifications
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Dispatcher.pm
index 23e15a0..ea9a550 100644 (file)
@@ -45,41 +45,42 @@ sub detach {
 
 sub dispatch {
     my ( $self, $c ) = @_;
-    my $action    = $c->req->action;
-    my $namespace = '';
-    $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
-      if $action eq 'default';
-
-    unless ($namespace) {
-        if ( my $result = $c->get_action($action) ) {
-            $namespace =
-              Catalyst::Utils::class2prefix( $result->[0]->[0]->namespace,
-                $c->config->{case_sensitive} );
-        }
-    }
 
-    my $default = $action eq 'default' ? $namespace : undef;
-    my $results = $c->get_action( $action, $default, $default ? 1 : 0 );
-    $namespace ||= '/';
+    if ( $c->action ) {
+
+        my @containers = $self->get_containers( $c->namespace );
+        my %actions;
+        foreach my $name (qw/begin auto end/) {
 
-    if ( @{$results} ) {
+            # Go down the container list representing each part of the
+            # current namespace inheritance tree, grabbing the actions hash
+            # of the ActionContainer object and looking for actions of the
+            # appropriate name registered to the namespace
+
+            $actions{$name} = [
+                map { $_->{$name} }
+                grep { exists $_->{$name} }
+                map { $_->actions }
+                @containers
+            ];
+        }
 
         # Errors break the normal flow and the end action is instantly run
         my $error = 0;
 
         # Execute last begin
         $c->state(1);
-        if ( my $begin = @{ $c->get_action( 'begin', $namespace, 1 ) }[-1] ) {
-            $begin->[0]->execute($c);
+        if ( my $begin = @{ $actions{begin} }[-1] ) {
+            $begin->execute($c);
             $error++ if scalar @{ $c->error };
         }
 
         # Execute the auto chain
         my $autorun = 0;
-        for my $auto ( @{ $c->get_action( 'auto', $namespace, 1 ) } ) {
+        for my $auto ( @{ $actions{auto} } ) {
             last if $error;
             $autorun++;
-            $auto->[0]->execute($c);
+            $auto->execute($c);
             $error++ if scalar @{ $c->error };
             last unless $c->state;
         }
@@ -88,18 +89,14 @@ sub dispatch {
         my $mkay = $autorun ? $c->state ? 1 : 0 : 1;
         if ( ( my $action = $c->req->action ) && $mkay ) {
             unless ($error) {
-                if ( my $result =
-                    @{ $c->get_action( $action, $default, 1 ) }[-1] )
-                {
-                    $result->[0]->execute($c);
-                    $error++ if scalar @{ $c->error };
-                }
+                $c->action->execute($c);
+                $error++ if scalar @{ $c->error };
             }
         }
 
         # Execute last end
-        if ( my $end = @{ $c->get_action( 'end', $namespace, 1 ) }[-1] ) {
-            $end->[0]->execute($c);
+        if ( my $end = @{ $actions{end} }[-1] ) {
+            $end->execute($c);
         }
     }
 
@@ -179,6 +176,7 @@ qq/Couldn't forward to command "$command". Invalid action or component./;
                     code      => $code,
                     reverse   => "$class->$method",
                     namespace => $class,
+                    prefix    => $class,
                 }
             );
             $results = [ [$action] ];
@@ -240,14 +238,21 @@ sub prepare_action {
             }
 
             $c->req->match($path);
+            $c->action($result->[0]);
+            $c->namespace($result->[0]->prefix);
             last;
         }
         unshift @args, pop @path;
     }
 
     unless ( $c->req->action ) {
-        $c->req->action('default');
-        $c->req->match('');
+        my $result = @{$c->get_action('default', $c->req->path, 1) || []}[-1];
+        if ($result) {
+            $c->action( $result->[0] );
+            $c->namespace( $c->req->path );
+            $c->req->action('default');
+            $c->req->match('');
+        }
     }
 
     $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' )
@@ -266,40 +271,12 @@ sub get_action {
 
     if ($namespace) {
 
-        my $parent = $self->tree;
-        my @match;
-
-        if ($namespace ne '/') {
-
-            my $visitor = Tree::Simple::Visitor::FindByPath->new;
-            my @path = split('/', $namespace);
-            $visitor->setSearchPath( @path );
-            $parent->accept($visitor);
-
-            if ($inherit) {
-
-                @match = $visitor->getResults;
-                @match = ($parent) unless @match;
-
-                if (!defined $visitor->getResult) {
-                    my $extra = $path[(scalar @match) - 1];
-                    last unless $extra;
-                    $visitor->setSearchPath($extra);
-                    $match[-1]->accept($visitor);
-                    push(@match, $visitor->getResult) if defined $visitor->getResult;
-                }
-            } else {
-                @match = ($visitor->getResult) if $visitor->getResult;
-            }
-
-        }
-
-        @match = ($parent) unless @match;
+        my @match = $self->get_containers( $namespace );
 
         my @results;
 
-        foreach my $child (@match) {
-            my $node = $child->getNodeValue->actions;
+        foreach my $child ($inherit ? @match: $match[-1]) {
+            my $node = $child->actions;
             push(@results, [ $node->{$action} ]) if defined $node->{$action};
         }
         return \@results;
@@ -324,6 +301,47 @@ sub get_action {
     return [];
 }
 
+=item $self->get_containers( $namespace )
+
+=cut
+
+sub get_containers {
+    my ( $self, $namespace ) = @_;
+
+    # If the namespace is / just return the root ActionContainer
+
+    return ($self->tree->getNodeValue) if $namespace eq '/';
+
+    # Use a visitor to recurse down the tree finding the ActionContainers
+    # for each namespace in the chain.
+
+    my $visitor = Tree::Simple::Visitor::FindByPath->new;
+    my @path = split('/', $namespace);
+    $visitor->setSearchPath( @path );
+    $self->tree->accept($visitor);
+
+    my @match = $visitor->getResults;
+    @match = ($self->tree) unless @match;
+
+    if (!defined $visitor->getResult) {
+
+        # If we don't manage to match, the visitor doesn't return the last
+        # node is matched, so foo/bar/baz would only find the 'foo' node,
+        # not the foo and foo/bar nodes as it should. This does another
+        # single-level search to see if that's the case, and the 'last unless'
+        # should catch any failures - or short-circuit this if this *is* a
+        # bug in the visitor and gets fixed.
+
+        my $extra = $path[(scalar @match) - 1];
+        last unless $extra;
+        $visitor->setSearchPath($extra);
+        $match[-1]->accept($visitor);
+        push(@match, $visitor->getResult) if defined $visitor->getResult;
+    }
+
+    return map { $_->getNodeValue } @match;
+}
+
 =item $self->set_action( $c, $action, $code, $namespace, $attrs )
 
 =cut
@@ -367,6 +385,10 @@ sub set_action {
             my $child = $visitor->getResult;
     
             unless ($child) {
+
+                # Create a new tree node and an ActionContainer to form
+                # its value.
+
                 my $container = Catalyst::ActionContainer->new(
                                     { part => $part, actions => {} });
                 $child = $parent->addChild( Tree::Simple->new($container) );
@@ -386,9 +408,11 @@ sub set_action {
             code      => $code,
             reverse   => $reverse,
             namespace => $namespace,
+            prefix    => $prefix,
         }
     );
 
+    # Set the method value
     $parent->getNodeValue->actions->{$method} = $action;
 
     my @path;