Lets stop shitting everywhere
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC / ConstructorInjection.pm
CommitLineData
b7da37bd 1package Catalyst::IOC::ConstructorInjection;
2use Moose;
eff5194d 3use Try::Tiny;
9e6091e2 4use Catalyst::Utils ();
b7da37bd 5extends 'Bread::Board::ConstructorInjection';
6
bf142143 7with 'Bread::Board::Service::WithClass',
104155f6 8 'Bread::Board::Service::WithParameters',
c614c3c2 9 'Bread::Board::Service::WithDependencies';
b7da37bd 10
9e6091e2 11has config_key => (
12 is => 'ro',
13 isa => 'Str',
14 lazy_build => 1,
15);
16
c614c3c2 17sub _build_config_key { Catalyst::Utils::class2classsuffix( shift->class ) }
18
19sub _build_constructor_name { 'COMPONENT' }
9e6091e2 20
b7da37bd 21sub get {
22 my $self = shift;
23
eff5194d 24 my $instance;
25
b7da37bd 26 my $constructor = $self->constructor_name;
b7da37bd 27 my $component = $self->class;
b32d8169 28 my %params = %{ $self->params };
29 use Data::Dumper;
30 Carp::cluck("Building $component with " . Dumper(\%params));
31 my $config = delete($params{'config'})->{ $self->config_key } || {};
32 %$config = (%$config, %params);
33
3f1b0032 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'
c614c3c2 39 my $app_name = $self->param('application_name');
b7da37bd 40
b7da37bd 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.
bf142143 44 local $config->{catalyst_component_name} = $component;
b7da37bd 45
c614c3c2 46 unless ( $component->can( $constructor ) ) {
47 # FIXME - make some deprecation warnings
48 return $component;
49 }
50
eff5194d 51 try {
52 $instance = $component->$constructor( $app_name, $config );
c614c3c2 53 }
eff5194d 54 catch {
c614c3c2 55 Catalyst::Exception->throw(
eff5194d 56 message => qq/Couldn't instantiate component "$component", "$_"/
c614c3c2 57 );
eff5194d 58 };
59
60 return $instance
61 if blessed $instance;
62
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(
68 message =>
69 qq/Couldn't instantiate component "$component", $constructor() method (from $component_method_from) didn't return an object-like value (value was $value)./
70 );
b7da37bd 71}
72
73__PACKAGE__->meta->make_immutable;
74
75no Moose; 1;
76
77__END__
78
79=pod
80
81=head1 NAME
82
1ab07ed4 83Catalyst::IOC::ConstructorInjection
84
85=head1 SYNOPSIS
86
87=head1 DESCRIPTION
b7da37bd 88
89=head1 AUTHORS
90
91Catalyst Contributors, see Catalyst.pm
92
93=head1 COPYRIGHT
94
95This library is free software. You can redistribute it and/or modify it under
96the same terms as Perl itself.
97
98=cut