isolating repeated code
[catagits/Catalyst-Runtime.git] / lib / Catalyst.pm
index 749d177..31f193f 100644 (file)
@@ -536,6 +536,34 @@ sub clear_errors {
     $c->error(0);
 }
 
+sub _find_component_regexp {
+    my ( $c, $container, $name, $args ) = @_;
+
+    return
+        if $c->config->{disable_component_resolution_regex_fallback} && !ref $name;
+
+    my $appclass = ref $c || $c;
+    my $prefix   = ucfirst $container->name;
+    my $p        = substr $prefix, 0, 1;
+
+    my $query = ref $name ? $name : qr{$name}i;
+    $query =~ s/^${appclass}::($p|$prefix):://i;
+
+    my @comps  = $container->get_service_list;
+    my @result = map {
+        $container->resolve( service => $_, parameters => { context => $args } )
+    } grep { m/$query/ } @comps;
+
+    if (!ref $name && $result[0]) {
+        $c->log->warn( Carp::shortmess(qq(Found results for "${name}" using regexp fallback)) );
+        $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];
+    }
+
+    return @result;
+}
+
 =head2 COMPONENT ACCESSORS
 
 =head2 $c->controller($name)
@@ -558,29 +586,13 @@ 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');
+    unshift @args, $c;
 
-    my $appclass = ref $c || $c;
     if( $name ) {
-        if ( !ref $name ) { # Direct component hash lookup to avoid costly regexps
-            return $container->resolve(service => $name, parameters => { context => [ $c, @args ] } )
-                if $container->has_service($name);
-        }
-        elsif ( ref $name eq 'Regexp' ) {
-            my @comps = $container->get_service_list;
-            my @result;
-            for (@comps) {
-                push @result, $container->resolve( service => $_, parameters => { context => [ $c, @args ] } )
-                    if m/$name/;
-            }
-            return @result;
-        }
-        else {
-            my $short_name = ref $name;
-            $short_name =~ s/^${appclass}::(C|Controller)//;
-            return $container->resolve( service => $short_name, parameters => { context => [ $c, @args ] } )
-                if $container->has_service($short_name);
-        }
-        return;
+        return $c->container->get_component('controller', $name, \@args)
+            if $container->has_service($name) && !ref $name;
+
+        return $c->_find_component_regexp( $container, $name, \@args );
     }
 
     return $c->component( $c->action->class );
@@ -611,28 +623,14 @@ sub model {
     my ( $c, $name, @args ) = @_;
     my $appclass = ref($c) || $c;
     my $container = $c->container->get_sub_container('model');
+    unshift @args, $c;
 
     if( $name ) {
-        if ( !ref $name ) { # Direct component hash lookup to avoid costly regexps
-            return $container->resolve( service => $name, parameters => { context => [ $c, @args ] } )
-                if $container->has_service($name);
-        }
-        elsif ( ref $name eq 'Regexp' ) {
-            my @comps = $container->get_service_list;
-            my @result;
-            for (@comps) {
-                push @result, $container->resolve( service => $_, parameters => { context => [ $c, @args ] } )
-                    if m/$name/;
-            }
-            return @result;
-        }
-        else {
-            my $short_name = ref $name;
-            $short_name =~ s/^${appclass}::(M|Model)//;
-            return $container->resolve( service => $short_name, parameters => { context => [ $c, @args ] } )
-                if $container->has_service($short_name);
-        }
-        return;
+        # Direct component hash lookup to avoid costly regexps
+        return $c->container->get_component('model', $name, \@args)
+            if ( !ref $name && $container->has_service($name));
+
+        return $c->_find_component_regexp( $container, $name, \@args );
     }
 
     if (ref $c) {
@@ -655,7 +653,7 @@ sub model {
         $c->log->warn( 'NB: in version 5.81, the "random" behavior will not work at all.' );
     }
 
-    return $container->resolve( service => $comp, parameters => { context => [ $c, @args ] } );
+    return $container->resolve( service => $comp, parameters => { context => \@args } );
 }
 
 
@@ -684,33 +682,19 @@ sub view {
     my ( $c, $name, @args ) = @_;
     my $appclass = ref($c) || $c;
     my $container = $c->container->get_sub_container('view');
+    unshift @args, $c;
 
     if( $name ) {
         if ( !ref $name ) { # Direct component hash lookup to avoid costly regexps
             if ( $container->has_service($name) ) {
-                return $container->resolve( service => $name, parameters => { context => [ $c, @args ] } );
+                return $c->container->get_component('view', $name, \@args);
             }
             else {
                 $c->log->warn( "Attempted to use view '$name', but does not exist" );
             }
         }
-        elsif ( ref $name eq 'Regexp' ) {
-            my @comps = $container->get_service_list;
-            my @result;
-            for (@comps) {
-                push @result, $container->resolve( service => $_, parameters => { context => [ $c, @args ] } )
-                    if m/$name/;
-            }
-            return @result;
-        }
-        else {
-            my $short_name = ref $name;
-            $short_name =~ s/^${appclass}::(V|View)//;
-            return $container->resolve( service => $short_name, parameters => { context => [ $c, @args ] } )
-                if $container->has_service($short_name);
-        }
 
-        return;
+        return $c->_find_component_regexp( $container, $name, \@args );
     }
 
     if (ref $c) {
@@ -732,7 +716,7 @@ sub view {
         $c->log->warn( 'NB: in version 5.81, the "random" behavior will not work at all.' );
     }
 
-    return $container->resolve( service => $comp, parameters => { context => [ $c, @args ] } );
+    return $container->resolve( service => $comp, parameters => { context => \@args } );
 }
 
 =head2 $c->controllers
@@ -801,10 +785,25 @@ sub component {
             if( !ref $component && $container->has_service($name) ) {
                 return $container->resolve( service => $name, parameters => { context => [ $c, @args ] } );
             }
+
+            return
+                if $c->config->{disable_component_resolution_regex_fallback};
+
+            my $query      = qr{$name}i;
+            my @components = $container->get_service_list;
+            my @result     = grep { m{$query} } @components;
+
+            if (@result) {
+                $c->log->warn( Carp::shortmess(qq(Found results for "${component}" using regexp fallback)) );
+                $c->log->warn( 'Relying on the regexp fallback behavior for component resolution' );
+                $c->log->warn( 'is unreliable and unsafe. You have been warned' );
+
+                return $container->resolve( service => $result[0], parameters => { context => [$c, @args] } );
+            }
         }
 
         return
-            if $c->config->{disable_component_resolution_regex_fallback};
+            if $c->config->{disable_component_resolution_regex_fallback} && !ref $component;
 
         # This is here so $c->comp( '::M::' ) works
         my $query = ref $component ? $component : qr{$component}i;