Happier with that
[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     warn("In get $self");
41     my $component   = $self->class;
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. local to avoid pointlessly shitting in config
57     # for the debug screen, as $component is already the key name.
58     $config{catalyst_component_name} = $self->catalyst_component_name;
59
60     unless ( $component->can( 'COMPONENT' ) ) {
61         # FIXME - make some deprecation warnings
62         return $component;
63     }
64
65     my $instance;
66     try {
67         $instance = $component->COMPONENT( $app_name, \%config );
68     }
69     catch {
70         Catalyst::Exception->throw(
71             message => qq/Couldn't instantiate component "$component", "$_"/
72         );
73     };
74
75     return $instance
76         if blessed $instance;
77
78     my $metaclass = Moose::Util::find_meta($component);
79     my $method_meta = $metaclass->find_method_by_name('COMPONENT');
80     my $component_method_from = $method_meta->associated_metaclass->name;
81     my $value = defined($instance) ? $instance : 'undef';
82     Catalyst::Exception->throw(
83         message =>
84         qq/Couldn't instantiate component "$component", COMPONENT method (from $component_method_from) didn't return an object-like value (value was $value)./
85     );
86 }
87
88 __PACKAGE__->meta->make_immutable;
89
90 no Moose; 1;
91
92 __END__
93
94 =pod
95
96 =head1 NAME
97
98 Catalyst::IOC::ConstructorInjection
99
100 =head1 SYNOPSIS
101
102 =head1 DESCRIPTION
103
104 =head1 AUTHORS
105
106 Catalyst Contributors, see Catalyst.pm
107
108 =head1 COPYRIGHT
109
110 This library is free software. You can redistribute it and/or modify it under
111 the same terms as Perl itself.
112
113 =cut