fixed ConstructorInjection
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC / Container.pm
index 6a1b489..6c9b3e5 100644 (file)
@@ -9,6 +9,7 @@ use Hash::Util qw/lock_hash/;
 use MooseX::Types::LoadableClass qw/ LoadableClass /;
 use Moose::Util;
 use Catalyst::IOC::BlockInjection;
+use Catalyst::IOC::ConstructorInjection;
 use Module::Pluggable::Object ();
 use namespace::autoclean;
 
@@ -404,6 +405,48 @@ sub build_locate_components_service {
     );
 }
 
+sub setup_components {
+    my $self = shift;
+    my $class = $self->resolve( service => 'application_name' );
+    my @comps = @{ $self->resolve( service => 'locate_components' ) };
+    my %comps = map { $_ => 1 } @comps;
+    my $deprecatedcatalyst_component_names = 0;
+
+    for my $component ( @comps ) {
+
+        # We pass ignore_loaded here so that overlay files for (e.g.)
+        # Model::DBI::Schema sub-classes are loaded - if it's in @comps
+        # we know M::P::O found a file on disk so this is safe
+
+        Catalyst::Utils::ensure_class_loaded( $component, { ignore_loaded => 1 } );
+    }
+
+    for my $component (@comps) {
+        $self->add_component( $component, $class );
+        # FIXME - $instance->expand_modules() is broken
+        my @expanded_components = $self->expand_component_module( $component );
+
+        if (
+            !$deprecatedcatalyst_component_names &&
+            ($deprecatedcatalyst_component_names = $component =~ m/::[CMV]::/) ||
+            ($deprecatedcatalyst_component_names = grep { /::[CMV]::/ } @expanded_components)
+        ) {
+            # FIXME - should I be calling warn here?
+            $class->log->warn(qq{Your application is using the deprecated ::[MVC]:: type naming scheme.\n}.
+                qq{Please switch your class names to ::Model::, ::View:: and ::Controller: as appropriate.\n}
+            );
+        }
+
+        for my $component (@expanded_components) {
+            $self->add_component( $component, $class )
+                unless $comps{$component};
+        }
+    }
+
+    $self->get_sub_container('model')->make_single_default;
+    $self->get_sub_container('view')->make_single_default;
+}
+
 sub _fix_syntax {
     my $config     = shift;
     my @components = (
@@ -577,10 +620,20 @@ sub add_component {
     return unless $type;
 
     $self->get_sub_container($type)->add_service(
-        Catalyst::IOC::BlockInjection->new(
+        Catalyst::IOC::ConstructorInjection->new(
             lifecycle => 'Singleton', # FIXME?
             name      => $name,
-            block     => sub { $self->setup_component( $component, $class ) },
+            class     => $component,
+            dependencies => [
+                depends_on( '/application_name' ),
+                depends_on( '/config' ),
+            ],
+            parameters => {
+                suffix => {
+                    isa => 'Str',
+                    default => Catalyst::Utils::class2classsuffix( $component ),
+                },
+            },
         )
     );
 }
@@ -607,94 +660,11 @@ sub _get_component_type_name {
     return (undef, $component);
 }
 
-# FIXME ugly and temporary
-# Just moved it here the way it was, so we can work on it here in the container
-sub setup_component {
-    my ( $self, $component, $class ) = @_;
-
-    unless ( $component->can( 'COMPONENT' ) ) {
-        return $component;
-    }
-
-    # FIXME I know this isn't the "Dependency Injection" way of doing things,
-    # its just temporary
-    my $suffix = Catalyst::Utils::class2classsuffix( $component );
-    my $config = $self->resolve(service => 'config')->{ $suffix } || {};
-
-    # Stash catalyst_component_name in the config here, so that custom COMPONENT
-    # methods also pass it. local to avoid pointlessly shitting in config
-    # for the debug screen, as $component is already the key name.
-    local $config->{catalyst_component_name} = $component;
-
-    my $instance = eval { $component->COMPONENT( $class, $config ); };
-
-    if ( my $error = $@ ) {
-        chomp $error;
-        Catalyst::Exception->throw(
-            message => qq/Couldn't instantiate component "$component", "$error"/
-        );
-    }
-    elsif (!blessed $instance) {
-        my $metaclass = Moose::Util::find_meta($component);
-        my $method_meta = $metaclass->find_method_by_name('COMPONENT');
-        my $component_method_from = $method_meta->associated_metaclass->name;
-        my $value = defined($instance) ? $instance : 'undef';
-        Catalyst::Exception->throw(
-            message =>
-            qq/Couldn't instantiate component "$component", COMPONENT() method (from $component_method_from) didn't return an object-like value (value was $value)./
-        );
-    }
-
-    return $instance;
-}
-
 sub expand_component_module {
     my ( $class, $module ) = @_;
     return Devel::InnerPackage::list_packages( $module );
 }
 
-sub setup_components {
-    my ( $self, $class ) = @_;
-
-    my @comps = @{ $self->resolve( service => 'locate_components' ) };
-    my %comps = map { $_ => 1 } @comps;
-    my $deprecatedcatalyst_component_names = 0;
-
-    for my $component ( @comps ) {
-
-        # We pass ignore_loaded here so that overlay files for (e.g.)
-        # Model::DBI::Schema sub-classes are loaded - if it's in @comps
-        # we know M::P::O found a file on disk so this is safe
-
-        Catalyst::Utils::ensure_class_loaded( $component, { ignore_loaded => 1 } );
-    }
-
-    for my $component (@comps) {
-        $self->add_component( $component, $class );
-        # FIXME - $instance->expand_modules() is broken
-        my @expanded_components = $self->expand_component_module( $component );
-
-        if (
-            !$deprecatedcatalyst_component_names &&
-            ($deprecatedcatalyst_component_names = $component =~ m/::[CMV]::/) ||
-            ($deprecatedcatalyst_component_names = grep { /::[CMV]::/ } @expanded_components)
-        ) {
-            # FIXME - should I be calling warn here?
-            $class->log->warn(qq{Your application is using the deprecated ::[MVC]:: type naming scheme.\n}.
-                qq{Please switch your class names to ::Model::, ::View:: and ::Controller: as appropriate.\n}
-            );
-        }
-
-        for my $component (@expanded_components) {
-            $self->add_component( $component, $class )
-                unless $comps{$component};
-        }
-    }
-
-    $self->get_sub_container('model')->make_single_default;
-    $self->get_sub_container('view')->make_single_default;
-}
-
 1;
 
 __END__
@@ -852,6 +822,8 @@ setup for the application.  By default, it will use L<Module::Pluggable>.
 Specify a C<setup_components> config option to pass additional options directly
 to L<Module::Pluggable>.
 
+=head2 build_setup_components_service
+
 =head1 AUTHORS
 
 Catalyst Contributors, see Catalyst.pm