72c551bdae7f5944be9d91fddcc06cae16d54499
[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(__catalyst_config => Bread::Board::Dependency->new(service_path => '/config'));
12     warn("Added dependency for config in " . $self->class);
13 }
14
15 has catalyst_component_name => (
16     is => 'ro',
17 );
18
19 has config => (
20     init_arg   => undef,
21     is         => 'ro',
22     isa        => 'HashRef',
23     writer     => '_set_config',
24     clearer    => '_clear_config',
25 );
26
27 around resolve_dependencies => sub {
28     my ($orig, $self, @args) = @_;
29     my %deps = $self->$orig(@args);
30 #    use Data::Dumper;
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} || {});
35     return %deps;
36 };
37
38 sub get {
39     my $self = shift;
40     my $component   = $self->class;
41     warn("In get $component");
42
43     my $params = $self->params;
44     my %config = (%{ $self->config || {} }, %{ $params });
45 #    warn(Data::Dumper::Dumper(\%config));
46     $self->_clear_config;
47
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');
54
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;
58
59     unless ( $component->can( 'COMPONENT' ) ) {
60         # FIXME - make some deprecation warnings
61         return $component;
62     }
63
64     my $instance;
65     try {
66         $instance = $component->COMPONENT( $app_name, \%config );
67     }
68     catch {
69         Catalyst::Exception->throw(
70             message => qq/Couldn't instantiate component "$component", "$_"/
71         );
72     };
73
74     return $instance
75         if blessed $instance;
76
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(
82         message =>
83         qq/Couldn't instantiate component "$component", COMPONENT method (from $component_method_from) didn't return an object-like value (value was $value)./
84     );
85 }
86
87 __PACKAGE__->meta->make_immutable;
88
89 no Moose; 1;
90
91 __END__
92
93 =pod
94
95 =head1 NAME
96
97 Catalyst::IOC::ConstructorInjection
98
99 =head1 SYNOPSIS
100
101 =head1 DESCRIPTION
102
103 =head1 AUTHORS
104
105 Catalyst Contributors, see Catalyst.pm
106
107 =head1 COPYRIGHT
108
109 This library is free software. You can redistribute it and/or modify it under
110 the same terms as Perl itself.
111
112 =cut