- Fixed Index, cleaned up get_action
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Dispatcher.pm
index a9ad5a4..66e8721 100644 (file)
@@ -20,6 +20,9 @@ __PACKAGE__->mk_accessors(qw/tree dispatch_types/);
 # Preload these action types
 our @PRELOAD = qw/Path Regex/;
 
+# Postload these action types
+our @POSTLOAD = qw/Index Default/;
+
 =head1 NAME
 
 Catalyst::Dispatcher - The Catalyst Dispatcher
@@ -251,25 +254,31 @@ sub get_action {
     my ( $self, $c, $action, $namespace, $inherit ) = @_;
     return [] unless $action;
     $namespace ||= '';
-    $inherit   ||= 0;
+    $namespace = '' if $namespace eq '/';
+    $inherit ||= 0;
 
     my @match = $self->get_containers($namespace);
 
-    my @results;
-
-    foreach my $child ( $inherit ? @match : $match[-1] ) {
-        my $node = $child->actions;
-        unless ($inherit) {
-            $namespace = '' if $namespace eq '/';
-            my $reverse = $node->{$action}->reverse;
-            my $name    = $namespace
-              ? $namespace =~ /\/$/ ? "$namespace$action" : "$namespace/$action"
-              : $action;
-            last unless $name eq $reverse;
+    if ($inherit) {    # Return [ [ $act_obj ], ... ] for valid containers
+        return [
+            map    { [ $_->{$action} ] }        # Make [ $action_obj ]
+              grep { defined $_->{$action} }    # If it exists in the container
+              map  { $_->actions }              # Get action hash for container
+              @match
+        ];
+    }
+    else {
+        my $node = $match[-1]->actions;    # Only bother looking at the last one
+
+        if ( defined $node->{$action}
+            && ( $node->{$action}->prefix eq $namespace ) )
+        {
+            return [ [ $node->{$action} ] ];
+        }
+        else {
+            return [];
         }
-        push( @results, [ $node->{$action} ] ) if defined $node->{$action};
     }
-    return \@results;
 }
 
 =item $self->get_containers( $namespace )
@@ -464,9 +473,14 @@ sub setup_actions {
         }
     }
 
-    # Default actions are always last in the chain
-    push @{ $self->dispatch_types }, Catalyst::DispatchType::Index->new;
-    push @{ $self->dispatch_types }, Catalyst::DispatchType::Default->new;
+    # Postload action types
+    for my $type (@POSTLOAD) {
+        my $class = "Catalyst::DispatchType::$type";
+        eval "require $class";
+        Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
+          if $@;
+        push @{ $self->dispatch_types }, $class->new;
+    }
 
     return unless $class->debug;