make_single_default sub
[catagits/Catalyst-Runtime.git] / lib / Catalyst.pm
index 2963722..ad387c0 100644 (file)
@@ -557,17 +557,19 @@ 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;
+# FIXME: should this be a Catalyst::Utils method?
+    if (!$name) {
+        my $class  = $c->action->class;
 
-        return $container->get_component_regexp( $name, $c, @args );
+        my $prefix = length Catalyst::Utils::class2classprefix($class);
+
+        # MyApp::Controller::Foo becomes Foo
+        # the + 2 is because of the ::
+        $name = substr $class, $prefix + 2;
     }
 
-    return $c->component( $c->action->class );
+    return $c->container->get_component_from_sub_container( 'controller', $name, $c, @args);
 }
 
 =head2 $c->model($name)
@@ -593,38 +595,16 @@ If you want to search for models, pass in a regexp as the argument.
 
 sub model {
     my ( $c, $name, @args ) = @_;
-    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 );
-    }
 
-    if (ref $c) {
+    if (ref $c && !$name) {
         return $c->stash->{current_model_instance}
-          if $c->stash->{current_model_instance};
-        return $c->model( $c->stash->{current_model} )
-          if $c->stash->{current_model};
-    }
-    return $c->model( $appclass->config->{default_model} )
-      if $appclass->config->{default_model};
+            if $c->stash->{current_model_instance};
 
-# FIXME: will this still be mantained?
-    my( $comp, $rest ) = $container->get_service_list;
-
-    if( $rest ) {
-        $c->log->warn( Carp::shortmess('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.81, the "random" behavior will not work at all.' );
+        $name = $c->stash->{current_model}
+            if $c->stash->{current_model};
     }
 
-    return $container->get_component( $comp, $c, @args );
+    return $c->container->get_component_from_sub_container( 'model', $name, $c, @args);
 }
 
 
@@ -651,39 +631,16 @@ If you want to search for views, pass in a regexp as the argument.
 
 sub view {
     my ( $c, $name, @args ) = @_;
-    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 );
-    }
-
-    if (ref $c) {
+    if (ref $c && !$name) {
         return $c->stash->{current_view_instance}
-          if $c->stash->{current_view_instance};
-        return $c->view( $c->stash->{current_view} )
-          if $c->stash->{current_view};
-    }
-    return $c->view( $appclass->config->{default_view} )
-      if $appclass->config->{default_view};
-
-    my( $comp, $rest ) = $container->get_service_list;
+            if $c->stash->{current_view_instance};
 
-    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.81, the "random" behavior will not work at all.' );
+        $name = $c->stash->{current_view}
+            if $c->stash->{current_view};
     }
 
-    return $container->get_component( $comp, $c, @args );
+    return $c->container->get_component_from_sub_container( 'view', $name, $c, @args);
 }
 
 =head2 $c->controllers
@@ -1505,7 +1462,6 @@ around components => sub {
 
         my ($type, $name) = _get_component_type_name($component);
 
-# FIXME: shouldn't the service name be $name?
         $containers->{$type}->add_service(Catalyst::IOC::BlockInjection->new( name => $name, block => sub { return $class->setup_component($component) } ));
     }
 
@@ -2425,6 +2381,9 @@ sub setup_components {
             $class->components->{ $component } = $class->setup_component($component);
         }
     }
+
+    $containers->{model}->make_single_default;
+    $containers->{view}->make_single_default;
 }
 
 sub _get_component_type_name {