created get_component_from_sub_container
André Walker [Fri, 8 Jul 2011 14:10:16 +0000 (11:10 -0300)]
lib/Catalyst.pm
lib/Catalyst/IOC/Container.pm
lib/Catalyst/IOC/SubContainer.pm

index 2963722..46d0e8b 100644 (file)
@@ -557,15 +557,9 @@ If you want to search for controllers, pass in a regexp as the argument.
 
 sub controller {
     my ( $c, $name, @args ) = @_;
-    my $container = $c->container->get_sub_container('controller');
 
-    if( $name ) {
-        # Direct component hash lookup to avoid costly regexps
-        return $container->get_component( $name, $c, @args )
-            if $container->has_service($name) && !ref $name;
-
-        return $container->get_component_regexp( $name, $c, @args );
-    }
+    return $c->container->get_component_from_sub_container( 'controller', $name, $c, @args)
+        if( $name );
 
     return $c->component( $c->action->class );
 }
@@ -596,13 +590,8 @@ sub model {
     my $appclass = ref($c) || $c;
     my $container = $c->container->get_sub_container('model');
 
-    if( $name ) {
-        # Direct component hash lookup to avoid costly regexps
-        return $container->get_component( $name, $c, @args )
-            if $container->has_service($name) && !ref $name;
-
-        return $container->get_component_regexp( $name, $c, @args );
-    }
+    return $c->container->get_component_from_sub_container( 'model', $name, $c, @args)
+        if( $name );
 
     if (ref $c) {
         return $c->stash->{current_model_instance}
@@ -654,15 +643,8 @@ sub view {
     my $appclass = ref($c) || $c;
     my $container = $c->container->get_sub_container('view');
 
-    if( $name ) {
-        # Direct component hash lookup to avoid costly regexps
-        return $container->get_component( $name, $c, @args )
-            if !ref $name && $container->has_service($name);
-
-        $c->log->warn( "Attempted to use view '$name', but does not exist" );
-
-        return $container->get_component_regexp( $name, $c, @args );
-    }
+    return $c->container->get_component_from_sub_container( 'view', $name, $c, @args)
+        if( $name );
 
     if (ref $c) {
         return $c->stash->{current_view_instance}
index 14b9486..75da32b 100644 (file)
@@ -380,6 +380,25 @@ sub _config_substitutions {
     return $arg;
 }
 
+sub get_component_from_sub_container {
+    my ( $self, $sub_container_name, $name, $c, @args ) = @_;
+
+    my $sub_container = $self->get_sub_container( $sub_container_name );
+
+    return $sub_container->get_component_regexp( $name, $c, @args )
+        if ref $name;
+
+    return $sub_container->get_component( $name, $c, @args )
+        if $sub_container->has_service( $name );
+
+    $c->log->warn(
+        "Attempted to use $sub_container_name '$name', " .
+        "but it does not exist"
+    );
+
+    return;
+}
+
 1;
 
 __END__
index 6d4f08f..068f379 100644 (file)
@@ -17,11 +17,6 @@ sub get_component {
 sub get_component_regexp {
     my ( $self, $query, $c, @args ) = @_;
 
-    if (!ref $query) {
-        $c->log->warn("Looking for '$query', but nothing was found.");
-        return;
-    }
-
     my @result = map {
         $self->get_component( $_, $c, @args )
     } grep { m/$query/ } $self->get_service_list;