1 package Catalyst::IOC::ConstructorInjection;
3 use Bread::Board::Dependency;
5 use Catalyst::Utils ();
7 extends 'Bread::Board::ConstructorInjection';
11 $self->add_dependency(__catalyst_config => Bread::Board::Dependency->new(service_path => '/config'));
12 warn("Added dependency for config in " . $self->class);
15 has catalyst_component_name => (
23 writer => '_set_config',
24 clearer => '_clear_config',
27 around resolve_dependencies => sub {
28 my ($orig, $self, @args) = @_;
29 my %deps = $self->$orig(@args);
31 # warn("$self Resolve deps" . Data::Dumper::Dumper(\%deps));
32 my $app_config = delete $deps{__catalyst_config};
33 my $conf_key = Catalyst::Utils::class2classsuffix($self->catalyst_component_name);
34 $self->_set_config($app_config->{$conf_key} || {});
41 my $component = $self->class;
43 my $params = $self->params;
44 my %config = (%{ $self->config }, %{ $params });
45 # warn(Data::Dumper::Dumper(\%config));
48 # FIXME - Is depending on the application name to pass into constructors here a good idea?
49 # This makes app/ctx split harder I think.. Need to think more here, but I think
50 # we want to pass the application in as a parameter when building the service
51 # rather than depending on the app name, so that later, when the app becomes an instance
52 # then it'll get passed in, and components can stash themselves 'per app instance'
53 my $app_name = $self->param('application_name');
55 # Stash catalyst_component_name in the config here, so that custom COMPONENT
56 # methods also pass it.
57 $config{catalyst_component_name} = $self->catalyst_component_name;
59 unless ( $component->can( 'COMPONENT' ) ) {
60 # FIXME - make some deprecation warnings
66 $instance = $component->COMPONENT( $app_name, \%config );
69 Catalyst::Exception->throw(
70 message => qq/Couldn't instantiate component "$component", "$_"/
77 my $metaclass = Moose::Util::find_meta($component);
78 my $method_meta = $metaclass->find_method_by_name('COMPONENT');
79 my $component_method_from = $method_meta->associated_metaclass->name;
80 my $value = defined($instance) ? $instance : 'undef';
81 Catalyst::Exception->throw(
83 qq/Couldn't instantiate component "$component", COMPONENT method (from $component_method_from) didn't return an object-like value (value was $value)./
87 __PACKAGE__->meta->make_immutable;
97 Catalyst::IOC::ConstructorInjection
105 Catalyst Contributors, see Catalyst.pm
109 This library is free software. You can redistribute it and/or modify it under
110 the same terms as Perl itself.