[Catalyst-commits] r8000 - in Catalyst-Runtime/5.70/trunk: . lib t

marcus at dev.catalyst.perl.org marcus at dev.catalyst.perl.org
Wed Jun 25 20:08:09 BST 2008


Author: marcus
Date: 2008-06-25 20:08:09 +0100 (Wed, 25 Jun 2008)
New Revision: 8000

Modified:
   Catalyst-Runtime/5.70/trunk/
   Catalyst-Runtime/5.70/trunk/Changes
   Catalyst-Runtime/5.70/trunk/lib/Catalyst.pm
   Catalyst-Runtime/5.70/trunk/t/unit_core_component.t
   Catalyst-Runtime/5.70/trunk/t/unit_core_mvc.t
Log:
merge compres branch


Property changes on: Catalyst-Runtime/5.70/trunk
___________________________________________________________________
Name: svk:merge
   - 1c72fc7c-9ce4-42af-bf25-3bfe470ff1e8:/local/Catalyst/trunk/Catalyst-Runtime:9763
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-ChildOf:4443
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-Runtime-jrockway:5857
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-component-setup:4320
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-docs:4325
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/current/Catalyst-Runtime:5142
4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst-Runtime:6165
d7608cd0-831c-0410-93c0-e5b306c3c028:/local/Catalyst/Catalyst-Runtime:8339
d7608cd0-831c-0410-93c0-e5b306c3c028:/local/Catalyst/Catalyst-Runtime-jrockway:8342
e56d974f-7718-0410-8b1c-b347a71765b2:/local/Catalyst-Runtime:6511
e56d974f-7718-0410-8b1c-b347a71765b2:/local/Catalyst-Runtime-current:10442
   + 1c72fc7c-9ce4-42af-bf25-3bfe470ff1e8:/local/Catalyst/trunk/Catalyst-Runtime:9763
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.70/branches/compres:7999
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-ChildOf:4443
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-Runtime-jrockway:5857
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-component-setup:4320
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-docs:4325
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/current/Catalyst-Runtime:5142
4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst:4483
4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst-Runtime:6165
d7608cd0-831c-0410-93c0-e5b306c3c028:/local/Catalyst/Catalyst-Runtime:8339
d7608cd0-831c-0410-93c0-e5b306c3c028:/local/Catalyst/Catalyst-Runtime-jrockway:8342
e56d974f-7718-0410-8b1c-b347a71765b2:/local/Catalyst-Runtime:6511
e56d974f-7718-0410-8b1c-b347a71765b2:/local/Catalyst-Runtime-current:10442

Modified: Catalyst-Runtime/5.70/trunk/Changes
===================================================================
--- Catalyst-Runtime/5.70/trunk/Changes	2008-06-25 19:06:44 UTC (rev 7999)
+++ Catalyst-Runtime/5.70/trunk/Changes	2008-06-25 19:08:09 UTC (rev 8000)
@@ -1,6 +1,14 @@
 # This file documents the revision history for Perl extension Catalyst.
 
 5.7xxx  xxx
+        - Refactored component resolution (component(), models(), model(), et al). We now
+          throw warnings for two reasons:
+          1) model() or view() was called with no arguments, and two results are returned
+             -- set default_(model|view), current_(model|view) or current_(model|view)_instance
+             instead
+          2) you call a component resolution method with a string, and it resorts to a regexp 
+             fallback wherein a result is returned -- if you really want to search, call the
+             method with a regex as the argument
         - remove 0-length query string components so warnings aren't thrown (RT #36428)
         - Update HTTP::Body dep so that the uploadtmp config value will work (RT #22540)
         - Fix for LocalRegex when used in the Root controller

Modified: Catalyst-Runtime/5.70/trunk/lib/Catalyst.pm
===================================================================
--- Catalyst-Runtime/5.70/trunk/lib/Catalyst.pm	2008-06-25 19:06:44 UTC (rev 7999)
+++ Catalyst-Runtime/5.70/trunk/lib/Catalyst.pm	2008-06-25 19:08:09 UTC (rev 8000)
@@ -414,87 +414,60 @@
     $c->error(0);
 }
 
+# search components given a name and some prefixes
+sub _comp_search_prefixes {
+    my ( $c, $name, @prefixes ) = @_;
+    my $appclass = ref $c || $c;
+    my $filter   = "^${appclass}::(" . join( '|', @prefixes ) . ')::';
 
-# search via regex
-sub _comp_search {
-    my ( $c, @names ) = @_;
+    # map the original component name to the sub part that we will search against
+    my %eligible = map { my $n = $_; $n =~ s{^$appclass\::[^:]+::}{}; $_ => $n; }
+        grep { /$filter/ } keys %{ $c->components };
 
-    foreach my $name (@names) {
-        foreach my $component ( keys %{ $c->components } ) {
-            return $c->components->{$component} if $component =~ /$name/i;
-        }
-    }
+    # undef for a name will return all
+    return keys %eligible if !defined $name;
 
-    return undef;
-}
+    my $query  = ref $name ? $name : qr/^$name$/i;
+    my @result = grep { $eligible{$_} =~ m{$query} } keys %eligible;
 
-# try explicit component names
-sub _comp_explicit {
-    my ( $c, @names ) = @_;
+    return map { $c->components->{ $_ } } @result if @result;
 
-    foreach my $try (@names) {
-        return $c->components->{$try} if ( exists $c->components->{$try} );
-    }
+    # if we were given a regexp to search against, we're done.
+    return if ref $name;
 
-    return undef;
-}
+    # regexp fallback
+    $query  = qr/$name/i;
+    @result = grep { $eligible{ $_ } =~ m{$query} } keys %eligible;
 
-# like component, but try just these prefixes before regex searching,
-#  and do not try to return "sort keys %{ $c->components }"
-sub _comp_prefixes {
-    my ( $c, $name, @prefixes ) = @_;
+    # don't warn if we didn't find any results, it just might not exist
+    if( @result ) {
+        $c->log->warn( 'Relying on the regexp fallback behavior for component resolution is unreliable and unsafe.' );
+        $c->log->warn( 'If you really want to search, pass in a regexp as the argument.' );
+    }
 
-    my $appclass = ref $c || $c;
-
-    my @names = map { "${appclass}::${_}::${name}" } @prefixes;
-
-    my $comp = $c->_comp_explicit(@names);
-    return $comp if defined($comp);
-    $comp = $c->_comp_search($name);
-    return $comp;
+    return @result;
 }
 
 # Find possible names for a prefix 
-
 sub _comp_names {
     my ( $c, @prefixes ) = @_;
-
     my $appclass = ref $c || $c;
 
-    my @pre = map { "${appclass}::${_}::" } @prefixes;
+    my $filter = "^${appclass}::(" . join( '|', @prefixes ) . ')::';
 
-    my @names;
-
-    COMPONENT: foreach my $comp ($c->component) {
-        foreach my $p (@pre) {
-            if ($comp =~ s/^$p//) {
-                push(@names, $comp);
-                next COMPONENT;
-            }
-        }
-    }
-
+    my @names = map { s{$filter}{}; $_; } $c->_comp_search_prefixes( undef, @prefixes );
     return @names;
 }
 
-# Return a component if only one matches.
-sub _comp_singular {
-    my ( $c, @prefixes ) = @_;
-
-    my $appclass = ref $c || $c;
-
-    my ( $comp, $rest ) =
-      map { $c->_comp_search("^${appclass}::${_}::") } @prefixes;
-    return $comp unless $rest;
-}
-
 # Filter a component before returning by calling ACCEPT_CONTEXT if available
 sub _filter_component {
     my ( $c, $comp, @args ) = @_;
+
     if ( eval { $comp->can('ACCEPT_CONTEXT'); } ) {
         return $comp->ACCEPT_CONTEXT( $c, @args );
     }
-    else { return $comp }
+    
+    return $comp;
 }
 
 =head2 COMPONENT ACCESSORS
@@ -508,13 +481,23 @@
 If the name is omitted, will return the controller for the dispatched
 action.
 
+If you want to search for controllers, pass in a regexp as the argument.
+
+    # find all controllers that start with Foo
+    my @foo_controllers = $c->controller(qr{^Foo});
+
+
 =cut
 
 sub controller {
     my ( $c, $name, @args ) = @_;
-    return $c->_filter_component( $c->_comp_prefixes( $name, qw/Controller C/ ),
-        @args )
-      if ($name);
+
+    if( $name ) {
+        my @result = $c->_comp_search_prefixes( $name, qw/Controller C/ );
+        return map { $c->_filter_component( $_, @args ) } @result if ref $name;
+        return $c->_filter_component( $result[ 0 ], @args );
+    }
+
     return $c->component( $c->action->class );
 }
 
@@ -532,13 +515,22 @@
  - a config setting 'default_model', or
  - check if there is only one model, and return it if that's the case.
 
+If you want to search for models, pass in a regexp as the argument.
+
+    # find all models that start with Foo
+    my @foo_models = $c->model(qr{^Foo});
+
 =cut
 
 sub model {
     my ( $c, $name, @args ) = @_;
-    return $c->_filter_component( $c->_comp_prefixes( $name, qw/Model M/ ),
-        @args )
-      if $name;
+
+    if( $name ) {
+        my @result = $c->_comp_search_prefixes( $name, qw/Model M/ );
+        return map { $c->_filter_component( $_, @args ) } @result if ref $name;
+        return $c->_filter_component( $result[ 0 ], @args );
+    }
+
     if (ref $c) {
         return $c->stash->{current_model_instance} 
           if $c->stash->{current_model_instance};
@@ -547,19 +539,18 @@
     }
     return $c->model( $c->config->{default_model} )
       if $c->config->{default_model};
-    return $c->_filter_component( $c->_comp_singular(qw/Model M/) );
 
-}
+    my( $comp, $rest ) = $c->_comp_search_prefixes( undef, qw/Model M/);
 
-=head2 $c->controllers
+    if( $rest ) {
+        $c->log->warn( 'Calling $c->model() will return a random model unless you specify one of:' );
+        $c->log->warn( '* $c->config->{default_model} # the name of the default model to use' );
+        $c->log->warn( '* $c->stash->{current_model} # the name of the model to use for this request' );
+        $c->log->warn( '* $c->stash->{current_model_instance} # the instance of the model to use for this request' );
+        $c->log->warn( 'NB: in version 5.80, the "random" behavior will not work at all.' );
+    }
 
-Returns the available names which can be passed to $c->controller
-
-=cut
-
-sub controllers {
-    my ( $c ) = @_;
-    return $c->_comp_names(qw/Controller C/);
+    return $c->_filter_component( $comp );
 }
 
 
@@ -577,13 +568,22 @@
  - a config setting 'default_view', or
  - check if there is only one view, and return it if that's the case.
 
+If you want to search for views, pass in a regexp as the argument.
+
+    # find all views that start with Foo
+    my @foo_views = $c->view(qr{^Foo});
+
 =cut
 
 sub view {
     my ( $c, $name, @args ) = @_;
-    return $c->_filter_component( $c->_comp_prefixes( $name, qw/View V/ ),
-        @args )
-      if $name;
+
+    if( $name ) {
+        my @result = $c->_comp_search_prefixes( $name, qw/View V/ );
+        return map { $c->_filter_component( $_, @args ) } @result if ref $name;
+        return $c->_filter_component( $result[ 0 ], @args );
+    }
+
     if (ref $c) {
         return $c->stash->{current_view_instance} 
           if $c->stash->{current_view_instance};
@@ -592,9 +592,31 @@
     }
     return $c->view( $c->config->{default_view} )
       if $c->config->{default_view};
-    return $c->_filter_component( $c->_comp_singular(qw/View V/) );
+
+    my( $comp, $rest ) = $c->_comp_search_prefixes( undef, qw/View V/);
+
+    if( $rest ) {
+        $c->log->warn( 'Calling $c->view() will return a random view unless you specify one of:' );
+        $c->log->warn( '* $c->config->{default_view} # the name of the default view to use' );
+        $c->log->warn( '* $c->stash->{current_view} # the name of the view to use for this request' );
+        $c->log->warn( '* $c->stash->{current_view_instance} # the instance of the view to use for this request' );
+        $c->log->warn( 'NB: in version 5.80, the "random" behavior will not work at all.' );
+    }
+
+    return $c->_filter_component( $comp );
 }
 
+=head2 $c->controllers
+
+Returns the available names which can be passed to $c->controller
+
+=cut
+
+sub controllers {
+    my ( $c ) = @_;
+    return $c->_comp_names(qw/Controller C/);
+}
+
 =head2 $c->models
 
 Returns the available names which can be passed to $c->model
@@ -627,35 +649,49 @@
 class. C<< $c->controller >>, C<< $c->model >>, and C<< $c->view >>
 should be used instead.
 
+If C<$name> is a regexp, a list of components matched against the full
+component name will be returned.
+
 =cut
 
 sub component {
-    my $c = shift;
+    my ( $c, $name, @args ) = @_;
 
-    if (@_) {
+    if( $name ) {
+        my $comps = $c->components;
 
-        my $name = shift;
+        if( !ref $name ) {
+            # is it the exact name?
+            return $comps->{ $name } if exists $comps->{ $name };
 
-        my $appclass = ref $c || $c;
+            # perhaps we just omitted "MyApp"?
+            my $composed = ( ref $c || $c ) . "::${name}";
+            return $comps->{ $composed } if exists $comps->{ $composed };
 
-        my @names = (
-            $name, "${appclass}::${name}",
-            map { "${appclass}::${_}::${name}" }
-              qw/Model M Controller C View V/
-        );
+            # search all of the models, views and controllers
+            my( $comp ) = $c->_comp_search_prefixes( $name, qw/Model M Controller C View V/ );
+            return $c->_filter_component( $comp, @args ) if $comp;
+        }
 
-        my $comp = $c->_comp_explicit(@names);
-        return $c->_filter_component( $comp, @_ ) if defined($comp);
+        # This is here so $c->comp( '::M::' ) works
+        my $query = ref $name ? $name : qr{$name}i;
 
-        $comp = $c->_comp_search($name);
-        return $c->_filter_component( $comp, @_ ) if defined($comp);
+        my @result = grep { m{$query} } keys %{ $c->components };
+        return @result if ref $name;
+
+        if( $result[ 0 ] ) {
+            $c->log->warn( 'Relying on the regexp fallback behavior for component resolution' );
+            $c->log->warn( 'is unreliable and unsafe. You have been warned' );
+            return $result[ 0 ];
+        }
+
+        # I would expect to return an empty list here, but that breaks back-compat
     }
 
+    # fallback
     return sort keys %{ $c->components };
 }
 
-
-
 =head2 CLASS DATA AND HELPER CLASSES
 
 =head2 $c->config

Modified: Catalyst-Runtime/5.70/trunk/t/unit_core_component.t
===================================================================
--- Catalyst-Runtime/5.70/trunk/t/unit_core_component.t	2008-06-25 19:06:44 UTC (rev 7999)
+++ Catalyst-Runtime/5.70/trunk/t/unit_core_component.t	2008-06-25 19:08:09 UTC (rev 8000)
@@ -1,4 +1,4 @@
-use Test::More tests => 7;
+use Test::More tests => 11;
 use strict;
 use warnings;
 
@@ -12,6 +12,9 @@
   use base qw/Catalyst/;
 
   __PACKAGE__->components({ map { ($_, $_) } @complist });
+
+  # this is so $c->log->warn will work
+  __PACKAGE__->setup_log;
 }
 
 is(MyApp->comp('MyApp::V::View'), 'MyApp::V::View', 'Explicit return ok');
@@ -20,9 +23,34 @@
 
 is(MyApp->comp('Model'), 'MyApp::M::Model', 'Single part return ok');
 
-is(MyApp->comp('::M::'), 'MyApp::M::Model', 'Regex return ok');
+# regexp fallback
+{
+    my $warnings = 0;
+    no warnings 'redefine';
+    local *Catalyst::Log::warn = sub { $warnings++ };
 
+    is(MyApp->comp('::M::'), 'MyApp::M::Model', 'Regex return ok');
+    ok( $warnings, 'regexp fallback for comp() warns' );
+}
+
 is_deeply([ MyApp->comp() ], \@complist, 'Empty return ok');
 
+# Is this desired behaviour?
 is_deeply([ MyApp->comp('Foo') ], \@complist, 'Fallthrough return ok');
-  # Is this desired behaviour?
+
+# regexp behavior
+{
+    is_deeply( [ MyApp->comp( qr{Model} ) ], [ 'MyApp::M::Model'], 'regexp ok' );
+}
+
+# multiple returns
+{
+    my @expected = qw( MyApp::C::Controller MyApp::M::Model );
+    is_deeply( [ MyApp->comp( qr{::[MC]::} ) ], \@expected, 'multiple results fro regexp ok' );
+}
+
+# failed search
+{
+    is_deeply( scalar MyApp->comp( qr{DNE} ), 0, 'no results for failed search' );
+}
+

Modified: Catalyst-Runtime/5.70/trunk/t/unit_core_mvc.t
===================================================================
--- Catalyst-Runtime/5.70/trunk/t/unit_core_mvc.t	2008-06-25 19:06:44 UTC (rev 7999)
+++ Catalyst-Runtime/5.70/trunk/t/unit_core_mvc.t	2008-06-25 19:08:09 UTC (rev 8000)
@@ -1,4 +1,4 @@
-use Test::More tests => 27;
+use Test::More tests => 37;
 use strict;
 use warnings;
 
@@ -18,6 +18,9 @@
     use base qw/Catalyst/;
 
     __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) } @complist } );
+
+    # allow $c->log->warn to work
+    __PACKAGE__->setup_log;
 }
 
 is( MyApp->view('View'), 'MyApp::V::View', 'V::View ok' );
@@ -39,6 +42,11 @@
 
 is( MyApp->model('M'), 'MyApp::Model::M', 'Model::M ok' );
 
+# failed search
+{
+    is( MyApp->model('DNE'), undef, 'undef for invalid search' );
+}
+
 is_deeply( [ sort MyApp->views ],
            [ qw/V View/ ],
            'views ok' );
@@ -51,8 +59,15 @@
            [ qw/Dummy::Model M Model Test::Object/ ],
            'models ok');
 
-is (MyApp->view , 'MyApp::V::View', 'view() with no defaults ok');
+{
+    my $warnings = 0;
+    no warnings 'redefine';
+    local *Catalyst::Log::warn = sub { $warnings++ };
 
+    like (MyApp->view , qr/^MyApp\::(V|View)\::/ , 'view() with no defaults returns *something*');
+    ok( $warnings, 'view() w/o a default is random, warnings thrown' );
+}
+
 is ( bless ({stash=>{current_view=>'V'}}, 'MyApp')->view , 'MyApp::View::V', 'current_view ok');
 
 my $view = bless {} , 'MyApp::View::V'; 
@@ -61,8 +76,15 @@
 is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyApp::V::View' }}, 'MyApp')->view , $view, 
   'current_view_instance precedes current_view ok');
 
-is (MyApp->model , 'MyApp::M::Model', 'model() with no defaults ok');
+{
+    my $warnings = 0;
+    no warnings 'redefine';
+    local *Catalyst::Log::warn = sub { $warnings++ };
 
+    like (MyApp->model , qr/^MyApp\::(M|Model)\::/ , 'model() with no defaults returns *something*');
+    ok( $warnings, 'model() w/o a default is random, warnings thrown' );
+}
+
 is ( bless ({stash=>{current_model=>'M'}}, 'MyApp')->model , 'MyApp::Model::M', 'current_model ok');
 
 my $model = bless {} , 'MyApp::Model::M'; 
@@ -79,6 +101,34 @@
 is ( bless ({stash=>{}}, 'MyApp')->model , 'MyApp::Model::M', 'default_model ok');
 is ( MyApp->model , 'MyApp::Model::M', 'default_model in class method ok');
 
+# regexp behavior tests
+{
+    # is_deeply is used because regexp behavior means list context
+    is_deeply( [ MyApp->view( qr{^V[ie]+w$} ) ], [ 'MyApp::V::View' ], 'regexp view ok' );
+    is_deeply( [ MyApp->controller( qr{Dummy\::Model$} ) ], [ 'MyApp::Controller::Model::Dummy::Model' ], 'regexp controller ok' );
+    is_deeply( [ MyApp->model( qr{Dum{2}y} ) ], [ 'MyApp::Model::Dummy::Model' ], 'regexp model ok' );
+}
+
+{
+    my @expected = qw( MyApp::C::Controller MyApp::Controller::C );
+    is_deeply( [ sort MyApp->controller( qr{^C} ) ], \@expected, 'multiple controller returns from regexp search' );
+}
+
+{
+    my @expected = qw( MyApp::V::View MyApp::View::V );
+    is_deeply( [ sort MyApp->view( qr{^V} ) ], \@expected, 'multiple view returns from regexp search' );
+}
+
+{
+    my @expected = qw( MyApp::M::Model MyApp::Model::M );
+    is_deeply( [ sort MyApp->model( qr{^M} ) ], \@expected, 'multiple model returns from regexp search' );
+}
+
+# failed search
+{
+    is( scalar MyApp->controller( qr{DNE} ), 0, '0 results for failed search' );
+}
+
 #checking @args passed to ACCEPT_CONTEXT
 my $args;
 {
@@ -90,3 +140,4 @@
 is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok');
 MyApp->view('V', qw/baz moo/);
 is_deeply($args, [qw/baz moo/], '$c->view args passed to ACCEPT_CONTEXT ok');
+




More information about the Catalyst-commits mailing list