X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FIOC%2FConstructorInjection.pm;h=dce5deff5fe0083d5db9b0f5990eb2476b85f3ce;hb=96c9f5d4c9cebf815970a5ea94b90ad693ebaa93;hp=e43b1cc09392fba3c4c787c425157f9687c7a28f;hpb=b7da37bd2fb5dc56de30f2f4c0579f470625b7d1;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/IOC/ConstructorInjection.pm b/lib/Catalyst/IOC/ConstructorInjection.pm index e43b1cc..dce5def 100644 --- a/lib/Catalyst/IOC/ConstructorInjection.pm +++ b/lib/Catalyst/IOC/ConstructorInjection.pm @@ -1,49 +1,81 @@ package Catalyst::IOC::ConstructorInjection; use Moose; +use Bread::Board::Dependency; +use Try::Tiny; +use Catalyst::Utils (); + extends 'Bread::Board::ConstructorInjection'; -with 'Catalyst::IOC::Service::WithAcceptContext'; +sub BUILD { + my $self = shift; + $self->add_dependency( + __catalyst_config => Bread::Board::Dependency->new( + service_path => '/config' + ) + ); +} -sub _build_constructor_name { 'COMPONENT' } +has catalyst_component_name => ( + is => 'ro', +); + +has config => ( + init_arg => undef, + is => 'ro', + isa => 'HashRef', + writer => '_set_config', + clearer => '_clear_config', +); + +around resolve_dependencies => sub { + my ($orig, $self, @args) = @_; + my %deps = $self->$orig(@args); + my $app_config = delete $deps{__catalyst_config}; + my $conf_key = Catalyst::Utils::class2classsuffix($self->catalyst_component_name); + $self->_set_config($app_config->{$conf_key} || {}); + return %deps; +}; 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; - unless ( $self->class->can( $constructor ) ) { - # FIXME - make some deprecation warnings - return $component; - } + my $params = $self->params; + my %config = (%{ $self->config || {} }, %{ $params }); + $self->_clear_config; + + my $app = $self->param('catalyst_application'); # 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; + # methods also pass it. + $config{catalyst_component_name} = $self->catalyst_component_name; - my $instance = eval { $component->$constructor( $class, $config ) }; + unless ( $component->can( 'COMPONENT' ) ) { + # FIXME - make some deprecation warnings + return $component; + } - if ( my $error = $@ ) { - chomp $error; - Catalyst::Exception->throw( - message => qq/Couldn't instantiate component "$component", "$error"/ - ); + my $instance; + try { + $instance = $component->COMPONENT( $app, \%config ); } - 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'; + catch { 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)./ + message => qq/Couldn't instantiate component "$component", "$_"/ ); - } - - return $instance; + }; + + return $instance + if 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)./ + ); } __PACKAGE__->meta->make_immutable; @@ -56,7 +88,11 @@ __END__ =head1 NAME -Catalyst::IOC::BlockInjection +Catalyst::IOC::ConstructorInjection + +=head1 SYNOPSIS + +=head1 DESCRIPTION =head1 AUTHORS