making $c->comp(qr/::M::/) work
André Walker [Thu, 21 Jul 2011 14:21:46 +0000 (11:21 -0300)]
lib/Catalyst.pm
lib/Catalyst/IOC/Container.pm
t/aggregate/unit_core_component.t

index 92a888b..f71cac6 100644 (file)
@@ -712,6 +712,11 @@ sub component {
 
     my @result = $c->container->find_component( $component, $c, @args );
 
+    # one last search for things like $c->comp(qr/::M::/)
+    @result = $c->container->find_component_regexp(
+        $c->components, $component, $c, @args
+    ) if !@result and ref $component;
+
     # list context for regexp searches
     return @result if ref $component;
 
index b2766ab..ffee749 100644 (file)
@@ -452,6 +452,25 @@ sub find_component {
     return @result;
 }
 
+sub find_component_regexp {
+    my ( $self, $components, $component, @args ) = @_;
+    my @result;
+
+    my @components = grep { m{$component} } keys %{ $components };
+
+    for (@components) {
+        # FIXME this is naughty enough being called inside Catalyst.pm
+        # find some alternative for this sub and remember to delete here
+        my ($type, $name) = Catalyst::_get_component_type_name($_);
+
+        push @result, $self->get_component_from_sub_container(
+            $type, $name, @args
+        ) if $type;
+    }
+
+    return @result;
+}
+
 
 1;
 
@@ -505,6 +524,8 @@ Catalyst::Container - IOC for Catalyst components
 
 =head2 find_component
 
+=head2 find_component_regexp
+
 =head2 _fix_syntax
 
 =head2 _config_substitutions
index 005015a..2875f0c 100644 (file)
@@ -45,11 +45,6 @@ is_deeply([ MyApp->comp('Foo') ], \@complist, 'Fallthrough return ok');
 
 # multiple returns
 {
-# FIXME: this cannot be found by looking only in the container.
-# either the test must be changed, or the regexp must be run against
-# $c->components() in Catalyst.pm
-    diag('this test will not work by searching the container');
-    diag('check the source of this file for more info');
     my @expected = sort qw( MyApp::C::Controller MyApp::M::Model );
     my @got = sort MyApp->comp( qr{::[MC]::} );
     is_deeply( \@got, \@expected, 'multiple results from regexp ok' );