Commit | Line | Data |
b7da37bd |
1 | package Catalyst::IOC::ConstructorInjection; |
2 | use Moose; |
28ec44a3 |
3 | use Bread::Board::Dependency; |
eff5194d |
4 | use Try::Tiny; |
9e6091e2 |
5 | use Catalyst::Utils (); |
28ec44a3 |
6 | |
b7da37bd |
7 | extends 'Bread::Board::ConstructorInjection'; |
8 | |
28ec44a3 |
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 | ); |
b7da37bd |
18 | |
00f61c9b |
19 | has config => ( |
28ec44a3 |
20 | init_arg => undef, |
9e6091e2 |
21 | is => 'ro', |
00f61c9b |
22 | isa => 'HashRef', |
28ec44a3 |
23 | writer => '_set_config', |
24 | clearer => '_clear_config', |
9e6091e2 |
25 | ); |
26 | |
28ec44a3 |
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 | |
b7da37bd |
38 | sub get { |
39 | my $self = shift; |
28ec44a3 |
40 | warn("In get $self"); |
b7da37bd |
41 | my $component = $self->class; |
28ec44a3 |
42 | |
43 | my $params = $self->params; |
44 | my %config = (%{ $self->config }, %{ $params }); |
45 | warn(Data::Dumper::Dumper(\%config)); |
46 | $self->_clear_config; |
b32d8169 |
47 | |
3f1b0032 |
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' |
c614c3c2 |
53 | my $app_name = $self->param('application_name'); |
b7da37bd |
54 | |
b7da37bd |
55 | # Stash catalyst_component_name in the config here, so that custom COMPONENT |
384481fd |
56 | # methods also pass it. |
28ec44a3 |
57 | $config{catalyst_component_name} = $self->catalyst_component_name; |
b7da37bd |
58 | |
00f61c9b |
59 | unless ( $component->can( 'COMPONENT' ) ) { |
c614c3c2 |
60 | # FIXME - make some deprecation warnings |
61 | return $component; |
62 | } |
63 | |
00f61c9b |
64 | my $instance; |
eff5194d |
65 | try { |
00f61c9b |
66 | $instance = $component->COMPONENT( $app_name, \%config ); |
c614c3c2 |
67 | } |
eff5194d |
68 | catch { |
c614c3c2 |
69 | Catalyst::Exception->throw( |
eff5194d |
70 | message => qq/Couldn't instantiate component "$component", "$_"/ |
c614c3c2 |
71 | ); |
eff5194d |
72 | }; |
73 | |
74 | return $instance |
75 | if blessed $instance; |
76 | |
77 | my $metaclass = Moose::Util::find_meta($component); |
00f61c9b |
78 | my $method_meta = $metaclass->find_method_by_name('COMPONENT'); |
eff5194d |
79 | my $component_method_from = $method_meta->associated_metaclass->name; |
80 | my $value = defined($instance) ? $instance : 'undef'; |
81 | Catalyst::Exception->throw( |
82 | message => |
00f61c9b |
83 | qq/Couldn't instantiate component "$component", COMPONENT method (from $component_method_from) didn't return an object-like value (value was $value)./ |
eff5194d |
84 | ); |
b7da37bd |
85 | } |
86 | |
87 | __PACKAGE__->meta->make_immutable; |
88 | |
89 | no Moose; 1; |
90 | |
91 | __END__ |
92 | |
93 | =pod |
94 | |
95 | =head1 NAME |
96 | |
1ab07ed4 |
97 | Catalyst::IOC::ConstructorInjection |
98 | |
99 | =head1 SYNOPSIS |
100 | |
101 | =head1 DESCRIPTION |
b7da37bd |
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 |