fixing merge conflicts from master
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC / ConstructorInjection.pm
CommitLineData
b7da37bd 1package Catalyst::IOC::ConstructorInjection;
2use Moose;
28ec44a3 3use Bread::Board::Dependency;
eff5194d 4use Try::Tiny;
9e6091e2 5use Catalyst::Utils ();
28ec44a3 6
b7da37bd 7extends 'Bread::Board::ConstructorInjection';
8
28ec44a3 9sub BUILD {
10 my $self = shift;
fddc99a9 11 $self->add_dependency(
12 __catalyst_config => Bread::Board::Dependency->new(
13 service_path => '/config'
14 )
15 );
28ec44a3 16}
17
18has catalyst_component_name => (
19 is => 'ro',
20);
b7da37bd 21
00f61c9b 22has config => (
28ec44a3 23 init_arg => undef,
9e6091e2 24 is => 'ro',
00f61c9b 25 isa => 'HashRef',
28ec44a3 26 writer => '_set_config',
27 clearer => '_clear_config',
9e6091e2 28);
29
28ec44a3 30around resolve_dependencies => sub {
31 my ($orig, $self, @args) = @_;
32 my %deps = $self->$orig(@args);
28ec44a3 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
b7da37bd 39sub get {
40 my $self = shift;
b7da37bd 41 my $component = $self->class;
28ec44a3 42
43 my $params = $self->params;
4d57c248 44 my %config = (%{ $self->config || {} }, %{ $params });
28ec44a3 45 $self->_clear_config;
b32d8169 46
3f1b0032 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'
c614c3c2 52 my $app_name = $self->param('application_name');
b7da37bd 53
b7da37bd 54 # Stash catalyst_component_name in the config here, so that custom COMPONENT
384481fd 55 # methods also pass it.
28ec44a3 56 $config{catalyst_component_name} = $self->catalyst_component_name;
b7da37bd 57
00f61c9b 58 unless ( $component->can( 'COMPONENT' ) ) {
c614c3c2 59 # FIXME - make some deprecation warnings
60 return $component;
61 }
62
00f61c9b 63 my $instance;
eff5194d 64 try {
00f61c9b 65 $instance = $component->COMPONENT( $app_name, \%config );
c614c3c2 66 }
eff5194d 67 catch {
c614c3c2 68 Catalyst::Exception->throw(
eff5194d 69 message => qq/Couldn't instantiate component "$component", "$_"/
c614c3c2 70 );
eff5194d 71 };
72
73 return $instance
74 if blessed $instance;
75
76 my $metaclass = Moose::Util::find_meta($component);
00f61c9b 77 my $method_meta = $metaclass->find_method_by_name('COMPONENT');
eff5194d 78 my $component_method_from = $method_meta->associated_metaclass->name;
79 my $value = defined($instance) ? $instance : 'undef';
80 Catalyst::Exception->throw(
81 message =>
00f61c9b 82 qq/Couldn't instantiate component "$component", COMPONENT method (from $component_method_from) didn't return an object-like value (value was $value)./
eff5194d 83 );
b7da37bd 84}
85
86__PACKAGE__->meta->make_immutable;
87
88no Moose; 1;
89
90__END__
91
92=pod
93
94=head1 NAME
95
1ab07ed4 96Catalyst::IOC::ConstructorInjection
97
98=head1 SYNOPSIS
99
100=head1 DESCRIPTION
b7da37bd 101
102=head1 AUTHORS
103
104Catalyst Contributors, see Catalyst.pm
105
106=head1 COPYRIGHT
107
108This library is free software. You can redistribute it and/or modify it under
109the same terms as Perl itself.
110
111=cut