Merge branch 'master' into gsoc_breadboard
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC / ConstructorInjection.pm
1 package Catalyst::IOC::ConstructorInjection;
2 use Moose;
3 use Bread::Board::Dependency;
4 use Try::Tiny;
5 use Catalyst::Utils ();
6
7 extends 'Bread::Board::ConstructorInjection';
8
9 sub BUILD {
10     my $self = shift;
11     $self->add_dependency(
12         __catalyst_config => Bread::Board::Dependency->new(
13             service_path => '/config'
14         )
15     );
16 }
17
18 has catalyst_component_name => (
19     is => 'ro',
20 );
21
22 has config => (
23     init_arg   => undef,
24     is         => 'ro',
25     isa        => 'HashRef',
26     writer     => '_set_config',
27     clearer    => '_clear_config',
28 );
29
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} || {});
36     return %deps;
37 };
38
39 sub get {
40     my $self = shift;
41     my $component   = $self->class;
42
43     my $params = $self->params;
44     my %config = (%{ $self->config || {} }, %{ $params });
45     $self->_clear_config;
46
47     # FIXME - Is depending on the application name to pass into constructors here a good idea?
48     #         This makes app/ctx split harder I think.. Need to think more here, but I think
49     #         we want to pass the application in as a parameter when building the service
50     #         rather than depending on the app name, so that later, when the app becomes an instance
51     #         then it'll get passed in, and components can stash themselves 'per app instance'
52     my $app_name    = $self->param('application_name');
53
54     # Stash catalyst_component_name in the config here, so that custom COMPONENT
55     # methods also pass it.
56     $config{catalyst_component_name} = $self->catalyst_component_name;
57
58     unless ( $component->can( 'COMPONENT' ) ) {
59         # FIXME - make some deprecation warnings
60         return $component;
61     }
62
63     my $instance;
64     try {
65         $instance = $component->COMPONENT( $app_name, \%config );
66     }
67     catch {
68         Catalyst::Exception->throw(
69             message => qq/Couldn't instantiate component "$component", "$_"/
70         );
71     };
72
73     return $instance
74         if blessed $instance;
75
76     my $metaclass = Moose::Util::find_meta($component);
77     my $method_meta = $metaclass->find_method_by_name('COMPONENT');
78     my $component_method_from = $method_meta->associated_metaclass->name;
79     my $value = defined($instance) ? $instance : 'undef';
80     Catalyst::Exception->throw(
81         message =>
82         qq/Couldn't instantiate component "$component", COMPONENT method (from $component_method_from) didn't return an object-like value (value was $value)./
83     );
84 }
85
86 __PACKAGE__->meta->make_immutable;
87
88 no Moose; 1;
89
90 __END__
91
92 =pod
93
94 =head1 NAME
95
96 Catalyst::IOC::ConstructorInjection
97
98 =head1 SYNOPSIS
99
100 =head1 DESCRIPTION
101
102 =head1 AUTHORS
103
104 Catalyst Contributors, see Catalyst.pm
105
106 =head1 COPYRIGHT
107
108 This library is free software. You can redistribute it and/or modify it under
109 the same terms as Perl itself.
110
111 =cut