1 package Catalyst::IOC::ConstructorInjection;
3 use Bread::Board::Dependency;
5 use Catalyst::Utils ();
7 extends 'Bread::Board::ConstructorInjection';
11 $self->add_dependency(
12 __catalyst_config => Bread::Board::Dependency->new(
13 service_path => '/config'
18 has catalyst_component_name => (
26 writer => '_set_config',
27 clearer => '_clear_config',
30 around resolve_dependencies => sub {
31 my ($orig, $self, @args) = @_;
32 my %deps = $self->$orig(@args);
33 my $app_config = delete $deps{__catalyst_config};
34 my $conf_key = Catalyst::Utils::class2classsuffix($self->catalyst_component_name);
35 $self->_set_config($app_config->{$conf_key} || {});
41 my $component = $self->class;
43 my $params = $self->params;
44 my %config = (%{ $self->config || {} }, %{ $params });
47 my $app = $self->param('catalyst_application');
49 # Stash catalyst_component_name in the config here, so that custom COMPONENT
50 # methods also pass it.
51 $config{catalyst_component_name} = $self->catalyst_component_name;
53 unless ( $component->can( 'COMPONENT' ) ) {
54 # FIXME - make some deprecation warnings
60 $instance = $component->COMPONENT( $app, \%config );
63 Catalyst::Exception->throw(
64 message => qq/Couldn't instantiate component "$component", "$_"/
71 my $metaclass = Moose::Util::find_meta($component);
72 my $method_meta = $metaclass->find_method_by_name('COMPONENT');
73 my $component_method_from = $method_meta->associated_metaclass->name;
74 my $value = defined($instance) ? $instance : 'undef';
75 Catalyst::Exception->throw(
77 qq/Couldn't instantiate component "$component", COMPONENT method (from $component_method_from) didn't return an object-like value (value was $value)./
81 __PACKAGE__->meta->make_immutable;
91 Catalyst::IOC::ConstructorInjection
99 Catalyst Contributors, see Catalyst.pm
103 This library is free software. You can redistribute it and/or modify it under
104 the same terms as Perl itself.