fixed ConstructorInjection
André Walker [Thu, 28 Jul 2011 22:07:37 +0000 (19:07 -0300)]
lib/Catalyst/IOC/ConstructorInjection.pm
lib/Catalyst/IOC/Container.pm

index e43b1cc..7a92087 100644 (file)
@@ -2,7 +2,10 @@ package Catalyst::IOC::ConstructorInjection;
 use Moose;
 extends 'Bread::Board::ConstructorInjection';
 
-with 'Catalyst::IOC::Service::WithAcceptContext';
+with 'Bread::Board::Service::WithClass',
+     'Bread::Board::Service::WithDependencies',
+     'Bread::Board::Service::WithParameters',
+     'Catalyst::IOC::Service::WithAcceptContext';
 
 sub _build_constructor_name { 'COMPONENT' }
 
@@ -10,11 +13,12 @@ sub get {
     my $self = shift;
 
     my $constructor = $self->constructor_name;
-    my $config      = $self->param('config')->{ $self->params->{suffix} } || {};
-    my $class       = $self->param('class');
     my $component   = $self->class;
+    my $params      = $self->params;
+    my $config      = $params->{config}->{ $self->param('suffix') } || {};
+    my $app_name    = $params->{application_name};
 
-    unless ( $self->class->can( $constructor ) ) {
+    unless ( $component->can( $constructor ) ) {
         # FIXME - make some deprecation warnings
         return $component;
     }
@@ -22,9 +26,9 @@ sub get {
     # 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} = $self->class;
+    local $config->{catalyst_component_name} = $component;
 
-    my $instance = eval { $component->$constructor( $class, $config ) };
+    my $instance = eval { $component->$constructor( $app_name, $config ) };
 
     if ( my $error = $@ ) {
         chomp $error;
index 9963633..6c9b3e5 100644 (file)
@@ -616,7 +616,6 @@ sub get_all_components {
 sub add_component {
     my ( $self, $component, $class ) = @_;
     my ( $type, $name ) = _get_component_type_name($component);
-    my $suffix = Catalyst::Utils::class2classsuffix( $component );
 
     return unless $type;
 
@@ -625,12 +624,15 @@ sub add_component {
             lifecycle => 'Singleton', # FIXME?
             name      => $name,
             class     => $component,
-            dependencies => {
-                application_name => Bread::Board::Dependency->new( service_path => '/application_name' ),
-                config => Bread::Board::Dependency->new( service_path => '/config' ),
-            },
-            params => {
-                suffix => $suffix,
+            dependencies => [
+                depends_on( '/application_name' ),
+                depends_on( '/config' ),
+            ],
+            parameters => {
+                suffix => {
+                    isa => 'Str',
+                    default => Catalyst::Utils::class2classsuffix( $component ),
+                },
             },
         )
     );
@@ -658,47 +660,6 @@ 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 );