From: Rafael Kitover Date: Fri, 19 Jun 2009 23:47:02 +0000 (+0000) Subject: prevent actions named index from registering as an index dispatchtype if they can... X-Git-Tag: 5.80006~46 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=1315d253bbf854cbd9740ef4323a9930411b9fa0 prevent actions named index from registering as an index dispatchtype if they can be registered with another dispatchtype --- diff --git a/lib/Catalyst/DispatchType/Index.pm b/lib/Catalyst/DispatchType/Index.pm index 86e45ee..d55ca00 100644 --- a/lib/Catalyst/DispatchType/Index.pm +++ b/lib/Catalyst/DispatchType/Index.pm @@ -2,7 +2,7 @@ package Catalyst::DispatchType::Index; use Moose; extends 'Catalyst::DispatchType'; -no Moose; +use namespace::clean -except => 'meta'; =head1 NAME @@ -25,6 +25,12 @@ dispatch types, see: =back +=cut + +has _actions => ( + is => 'rw', isa => 'HashRef', default => sub { +{} } +); + =head1 METHODS =head2 $self->match( $c, $path ) @@ -40,6 +46,8 @@ sub match { return if @{ $c->req->args }; my $result = $c->get_action( 'index', $path ); + return 0 unless $result && exists $self->_actions->{ $result->reverse }; + if ($result && $result->match($c)) { $c->action($result); $c->namespace( $result->namespace ); @@ -50,6 +58,20 @@ sub match { return 0; } +=head2 $self->register( $c, $action ) + +Register an action with this DispatchType. + +=cut + +sub register { + my ( $self, $c, $action ) = @_; + + $self->_actions->{ $action->reverse } = $action; + + return 1; +} + =head2 $self->uri_for_action( $action, $captures ) get a URI part for an action; always returns undef is $captures is set diff --git a/lib/Catalyst/Dispatcher.pm b/lib/Catalyst/Dispatcher.pm index 3a97c87..cb22e63 100644 --- a/lib/Catalyst/Dispatcher.pm +++ b/lib/Catalyst/Dispatcher.pm @@ -531,9 +531,29 @@ sub register { } } + my @dtypes = @{ $self->_dispatch_types }; + my @normal_dtypes; + my @low_precedence_dtypes; + + while (my $type = shift @dtypes) { + if ($type->isa('Catalyst::DispatchType::Index') || + $type->isa('Catalyst::DispatchType::Default')) { + push @low_precedence_dtypes, $type; + } else { + push @normal_dtypes, $type; + } + } + # Pass the action to our dispatch types so they can register it if reqd. - foreach my $type ( @{ $self->_dispatch_types } ) { - $type->register( $c, $action ); + my $was_registered = 0; + foreach my $type ( @normal_dtypes ) { + $was_registered = 1 if $type->register( $c, $action ); + } + + if (not $was_registered) { + foreach my $type ( @low_precedence_dtypes ) { + $type->register( $c, $action ); + } } my $namespace = $action->namespace; diff --git a/t/aggregate/live_component_controller_action_index_or_default_names.t b/t/aggregate/live_component_controller_action_named_index.t similarity index 90% rename from t/aggregate/live_component_controller_action_index_or_default_names.t rename to t/aggregate/live_component_controller_action_named_index.t index e5889bb..393e030 100644 --- a/t/aggregate/live_component_controller_action_index_or_default_names.t +++ b/t/aggregate/live_component_controller_action_named_index.t @@ -12,7 +12,7 @@ BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 1; } use Test::More tests => 1*$iters; -use Catalyst::Test 'TestAppIndexDefault'; +use Catalyst::Test 'TestAppIndexActionName'; if ( $ENV{CAT_BENCHMARK} ) { require Benchmark; diff --git a/t/lib/TestAppIndexDefault.pm b/t/lib/TestAppIndexActionName.pm similarity index 67% rename from t/lib/TestAppIndexDefault.pm rename to t/lib/TestAppIndexActionName.pm index 9a129cb..977693e 100644 --- a/t/lib/TestAppIndexDefault.pm +++ b/t/lib/TestAppIndexActionName.pm @@ -1,4 +1,4 @@ -package TestAppIndexDefault; +package TestAppIndexActionName; use strict; use warnings; use Catalyst; diff --git a/t/lib/TestAppIndexDefault/Controller/IndexChained.pm b/t/lib/TestAppIndexActionName/Controller/IndexChained.pm similarity index 80% rename from t/lib/TestAppIndexDefault/Controller/IndexChained.pm rename to t/lib/TestAppIndexActionName/Controller/IndexChained.pm index 18a8034..5dfb013 100644 --- a/t/lib/TestAppIndexDefault/Controller/IndexChained.pm +++ b/t/lib/TestAppIndexActionName/Controller/IndexChained.pm @@ -1,4 +1,4 @@ -package TestAppIndexDefault::Controller::IndexChained; +package TestAppIndexActionName::Controller::IndexChained; use base 'Catalyst::Controller';