1 package Catalyst::IOC::ConstructorInjection;
4 use Catalyst::Utils ();
5 extends 'Bread::Board::ConstructorInjection';
7 with 'Bread::Board::Service::WithClass',
8 'Bread::Board::Service::WithParameters',
9 'Bread::Board::Service::WithDependencies';
17 sub _build_config_key { Catalyst::Utils::class2classsuffix( shift->class ) }
19 sub _build_constructor_name { 'COMPONENT' }
26 my $constructor = $self->constructor_name;
27 my $component = $self->class;
28 my %params = %{ $self->params };
30 Carp::cluck("Building $component with " . Dumper(\%params));
31 my $config = delete($params{'config'})->{ $self->config_key } || {};
32 %$config = (%$config, %params);
34 # FIXME - Is depending on the application name to pass into constructors here a good idea?
35 # This makes app/ctx split harder I think.. Need to think more here, but I think
36 # we want to pass the application in as a parameter when building the service
37 # rather than depending on the app name, so that later, when the app becomes an instance
38 # then it'll get passed in, and components can stash themselves 'per app instance'
39 my $app_name = $self->param('application_name');
41 # Stash catalyst_component_name in the config here, so that custom COMPONENT
42 # methods also pass it. local to avoid pointlessly shitting in config
43 # for the debug screen, as $component is already the key name.
44 local $config->{catalyst_component_name} = $component;
46 unless ( $component->can( $constructor ) ) {
47 # FIXME - make some deprecation warnings
52 $instance = $component->$constructor( $app_name, $config );
55 Catalyst::Exception->throw(
56 message => qq/Couldn't instantiate component "$component", "$_"/
63 my $metaclass = Moose::Util::find_meta($component);
64 my $method_meta = $metaclass->find_method_by_name($constructor);
65 my $component_method_from = $method_meta->associated_metaclass->name;
66 my $value = defined($instance) ? $instance : 'undef';
67 Catalyst::Exception->throw(
69 qq/Couldn't instantiate component "$component", $constructor() method (from $component_method_from) didn't return an object-like value (value was $value)./
73 __PACKAGE__->meta->make_immutable;
83 Catalyst::IOC::ConstructorInjection
91 Catalyst Contributors, see Catalyst.pm
95 This library is free software. You can redistribute it and/or modify it under
96 the same terms as Perl itself.