Merge 'trunk' into 'index_default_fuckage'
Rafael Kitover [Sun, 21 Jun 2009 00:19:34 +0000 (00:19 +0000)]
r5579@hlagh (orig r10594):  t0m | 2009-06-19 13:35:41 -0700
Just initialize a logger which only logs fatal errors, no mock needed
r5580@hlagh (orig r10595):  t0m | 2009-06-19 13:39:19 -0700
Remove Test::MockObject
r5581@hlagh (orig r10596):  t0m | 2009-06-19 13:39:58 -0700
Remove Test::MockObject
r5582@hlagh (orig r10597):  t0m | 2009-06-19 13:41:46 -0700
Remove MockObject
r5583@hlagh (orig r10598):  t0m | 2009-06-19 13:44:15 -0700
1 more MockObject removal
r5584@hlagh (orig r10599):  t0m | 2009-06-19 13:46:45 -0700
Remove MockObject from Makefile.PL
r5596@hlagh (orig r10605):  t0m | 2009-06-20 16:23:19 -0700
Re-add lukes specific regression test from r10498
r5597@hlagh (orig r10606):  t0m | 2009-06-20 16:25:31 -0700
Update test to be TODO, and note this case used to fail on 5.7

lib/Catalyst/Action.pm
lib/Catalyst/DispatchType/Index.pm
lib/Catalyst/DispatchType/Path.pm
lib/Catalyst/Dispatcher.pm
t/aggregate/live_component_controller_action_index_or_default.t [new file with mode: 0644]
t/lib/TestAppIndexDefault.pm [new file with mode: 0644]
t/lib/TestAppIndexDefault/Controller/IndexChained.pm [new file with mode: 0644]
t/lib/TestAppIndexDefault/Controller/IndexPrivate.pm [new file with mode: 0644]
t/lib/TestAppIndexDefault/Controller/Root.pm [new file with mode: 0644]

index e2e78b6..97b9235 100644 (file)
@@ -18,8 +18,9 @@ L<Catalyst::Controller> subclasses.
 =cut
 
 use Moose;
-
+use Scalar::Util 'looks_like_number';
 with 'MooseX::Emulate::Class::Accessor::Fast';
+use namespace::clean -except => 'meta';
 
 has class => (is => 'rw');
 has namespace => (is => 'rw');
@@ -28,8 +29,6 @@ has attributes => (is => 'rw');
 has name => (is => 'rw');
 has code => (is => 'rw');
 
-no Moose;
-
 use overload (
 
     # Stringify to reverse for debug output etc.
@@ -38,6 +37,10 @@ use overload (
     # Codulate to execute to invoke the encapsulated action coderef
     '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; },
 
+    # Which action takes precedence
+    'cmp' => 'compare',
+    '<=>' => 'compare',
+
     # Make general $stuff still work
     fallback => 1,
 
@@ -70,6 +73,22 @@ sub match {
     return scalar( @{ $c->req->args } ) == $args;
 }
 
+sub sort_order {
+    my $self = shift;
+
+    my ($args) = @{ $self->attributes->{Args} || [] };
+
+    return $args if looks_like_number($args);
+
+    return ~0;
+}
+
+sub compare {
+    my ($a1, $a2) = @_;
+
+    return $a1->sort_order <=> $a2->sort_order;
+}
+
 __PACKAGE__->meta->make_immutable;
 
 1;
@@ -105,6 +124,14 @@ context and arguments
 Check Args attribute, and makes sure number of args matches the setting.
 Always returns true if Args is omitted.
 
+=head2 sort_order
+
+Returns the value of the C<Args> attribute, or C<~0> if it has no value.
+
+=head2 compare
+
+Returns C<< $a->sort_order <=> $b->sort_order >> .
+
 =head2 namespace
 
 Returns the private namespace this action lives in.
index 86e45ee..d55ca00 100644 (file)
@@ -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
index 6be3893..5c1e4ed 100644 (file)
@@ -6,7 +6,6 @@ extends 'Catalyst::DispatchType';
 use Text::SimpleTable;
 use Catalyst::Utils;
 use URI;
-use Scalar::Util ();
 
 has _paths => (
                is => 'rw',
@@ -62,16 +61,6 @@ sub list {
       if ( keys %{ $self->_paths } );
 }
 
-sub _action_args_sort_order {
-    my ( $self, $action ) = @_;
-
-    my ($args) = @{ $action->attributes->{Args} || [] };
-
-    return $args if Scalar::Util::looks_like_number($args);
-
-    return ~0;
-}
-
 =head2 $self->match( $c, $path )
 
 For each action registered to this exact path, offers the action a chance to
@@ -85,10 +74,7 @@ sub match {
 
     $path = '/' if !defined $path || !length $path;
 
-    # sort from least args to most
-    my @actions = sort { $self->_action_args_sort_order($a) <=>
-                         $self->_action_args_sort_order($b) }
-            @{ $self->_paths->{$path} || [] };
+    my @actions = @{ $self->_paths->{$path} || [] };
 
     foreach my $action ( @actions ) {
         next unless $action->match($c);
@@ -131,7 +117,9 @@ sub register_path {
     $path = '/' unless length $path;
     $path = URI->new($path)->canonical;
 
-    unshift( @{ $self->_paths->{$path} ||= [] }, $action);
+    $self->_paths->{$path} = [
+        sort { $a <=> $b } ($action, @{ $self->_paths->{$path} || [] })
+    ];
 
     return 1;
 }
index 3a97c87..08a4248 100644 (file)
@@ -531,9 +531,29 @@ sub register {
         }
     }
 
+    my @dtypes = @{ $self->_dispatch_types };
+    my @normal_dtypes;
+    my @low_precedence_dtypes;
+
+    for my $type ( @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.t b/t/aggregate/live_component_controller_action_index_or_default.t
new file mode 100644 (file)
index 0000000..fada74b
--- /dev/null
@@ -0,0 +1,31 @@
+#!perl
+
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/../lib";
+
+our $iters;
+
+BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 1; }
+
+use Test::More tests => 3*$iters;
+
+use Catalyst::Test 'TestAppIndexDefault';
+
+if ( $ENV{CAT_BENCHMARK} ) {
+    require Benchmark;
+    Benchmark::timethis( $iters, \&run_tests );
+}
+else {
+    for ( 1 .. $iters ) {
+        run_tests();
+    }
+}
+
+sub run_tests {
+    is(get('/indexchained'), 'index_chained', ':Chained overrides index');
+    is(get('/indexprivate'), 'index_private', 'index : Private still works');
+    is(get('/one_arg'), 'path_one_arg', ':Path overrides default');
+}
diff --git a/t/lib/TestAppIndexDefault.pm b/t/lib/TestAppIndexDefault.pm
new file mode 100644 (file)
index 0000000..9a129cb
--- /dev/null
@@ -0,0 +1,8 @@
+package TestAppIndexDefault;
+use strict;
+use warnings;
+use Catalyst;
+
+__PACKAGE__->setup;
+
+1;
diff --git a/t/lib/TestAppIndexDefault/Controller/IndexChained.pm b/t/lib/TestAppIndexDefault/Controller/IndexChained.pm
new file mode 100644 (file)
index 0000000..18a8034
--- /dev/null
@@ -0,0 +1,12 @@
+package TestAppIndexDefault::Controller::IndexChained;
+
+use base 'Catalyst::Controller';
+
+sub index : Chained('/') PathPart('indexchained') CaptureArgs(0) {}
+
+sub index_endpoint : Chained('index') PathPart('') Args(0) {
+    my ($self, $c) = @_;
+    $c->res->body('index_chained');
+}
+
+1;
diff --git a/t/lib/TestAppIndexDefault/Controller/IndexPrivate.pm b/t/lib/TestAppIndexDefault/Controller/IndexPrivate.pm
new file mode 100644 (file)
index 0000000..08367ff
--- /dev/null
@@ -0,0 +1,10 @@
+package TestAppIndexDefault::Controller::IndexPrivate;
+
+use base 'Catalyst::Controller';
+
+sub index : Private {
+    my ($self, $c) = @_;
+    $c->res->body('index_private');
+}
+
+1;
diff --git a/t/lib/TestAppIndexDefault/Controller/Root.pm b/t/lib/TestAppIndexDefault/Controller/Root.pm
new file mode 100644 (file)
index 0000000..8acebf8
--- /dev/null
@@ -0,0 +1,17 @@
+package TestAppIndexDefault::Controller::Root;
+
+use base 'Catalyst::Controller';
+
+__PACKAGE__->config->{namespace} = '';
+
+sub default : Private {
+    my ($self, $c) = @_;
+    $c->res->body('default');
+}
+
+sub path_one_arg : Path('/') Args(1) {
+    my ($self, $c) = @_;
+    $c->res->body('path_one_arg');
+}
+
+1;