simplified Catalyst.pm
André Walker [Mon, 11 Jul 2011 16:48:56 +0000 (13:48 -0300)]
lib/Catalyst.pm
lib/Catalyst/IOC/Container.pm

index e071663..134f5b6 100644 (file)
@@ -595,33 +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');
-
-    return $c->container->get_component_from_sub_container( 'model', $name, $c, @args)
-        if( $name );
 
-    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};
-
-# FIXME: will this still be mantained?
-    my( $comp, $rest ) = $container->get_service_list;
+            if $c->stash->{current_model_instance};
 
-    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);
 }
 
 
@@ -648,32 +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');
-
-    return $c->container->get_component_from_sub_container( 'view', $name, $c, @args)
-        if( $name );
 
-    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
index 75da32b..7dd150e 100644 (file)
@@ -102,6 +102,25 @@ sub build_controller_subcontainer {
     );
 }
 
+sub build_default_model {
+    Bread::Board::BlockInjection->new(
+        block => sub {
+            shift->param('config')->{default_model};
+        },
+        dependencies => [ depends_on('config') ],
+    );
+}
+
+sub build_default_view {
+    Bread::Board::BlockInjection->new(
+        name => 'default_view',
+        block => sub {
+            shift->param('config')->{default_view};
+        },
+        dependencies => [ depends_on('config') ],
+    );
+}
+
 sub build_name_service {
     my $self = shift;
 
@@ -385,6 +404,21 @@ sub get_component_from_sub_container {
 
     my $sub_container = $self->get_sub_container( $sub_container_name );
 
+    if (!$name) {
+        my $default_name = 'default_' . $sub_container_name;
+        my $default      = $self->resolve( service => $default_name )
+            if $self->has_service($default_name);
+
+        return $sub_container->get_component( $default, $c, @args )
+            if $default && $sub_container->has_service( $default );
+
+        # this is never a controller, so this is safe
+        $c->log->warn( "Calling \$c->$sub_container_name() is not supported unless you specify one of:" );
+        $c->log->warn( "* \$c->config(default_$sub_container_name => 'the name of the default $sub_container_name to use')" );
+        $c->log->warn( "* \$c->stash->{current_$sub_container_name} # the name of the view to use for this request" );
+        $c->log->warn( "* \$c->stash->{current_${sub_container_name}_instance} # the instance of the $sub_container_name to use for this request" );
+    }
+
     return $sub_container->get_component_regexp( $name, $c, @args )
         if ref $name;