From: André Walker Date: Thu, 28 Jul 2011 22:07:37 +0000 (-0300) Subject: fixed ConstructorInjection X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=bf142143c2745aa06234058ade60505984b314fb fixed ConstructorInjection --- diff --git a/lib/Catalyst/IOC/ConstructorInjection.pm b/lib/Catalyst/IOC/ConstructorInjection.pm index e43b1cc..7a92087 100644 --- a/lib/Catalyst/IOC/ConstructorInjection.pm +++ b/lib/Catalyst/IOC/ConstructorInjection.pm @@ -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; diff --git a/lib/Catalyst/IOC/Container.pm b/lib/Catalyst/IOC/Container.pm index 9963633..6c9b3e5 100644 --- a/lib/Catalyst/IOC/Container.pm +++ b/lib/Catalyst/IOC/Container.pm @@ -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 );