renaming application_name -> catalyst_application
[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
96c9f5d4 47 my $app = $self->param('catalyst_application');
b7da37bd 48
b7da37bd 49 # Stash catalyst_component_name in the config here, so that custom COMPONENT
384481fd 50 # methods also pass it.
28ec44a3 51 $config{catalyst_component_name} = $self->catalyst_component_name;
b7da37bd 52
00f61c9b 53 unless ( $component->can( 'COMPONENT' ) ) {
c614c3c2 54 # FIXME - make some deprecation warnings
55 return $component;
56 }
57
00f61c9b 58 my $instance;
eff5194d 59 try {
96c9f5d4 60 $instance = $component->COMPONENT( $app, \%config );
c614c3c2 61 }
eff5194d 62 catch {
c614c3c2 63 Catalyst::Exception->throw(
eff5194d 64 message => qq/Couldn't instantiate component "$component", "$_"/
c614c3c2 65 );
eff5194d 66 };
67
68 return $instance
69 if blessed $instance;
70
71 my $metaclass = Moose::Util::find_meta($component);
00f61c9b 72 my $method_meta = $metaclass->find_method_by_name('COMPONENT');
eff5194d 73 my $component_method_from = $method_meta->associated_metaclass->name;
74 my $value = defined($instance) ? $instance : 'undef';
75 Catalyst::Exception->throw(
76 message =>
00f61c9b 77 qq/Couldn't instantiate component "$component", COMPONENT method (from $component_method_from) didn't return an object-like value (value was $value)./
eff5194d 78 );
b7da37bd 79}
80
81__PACKAGE__->meta->make_immutable;
82
83no Moose; 1;
84
85__END__
86
87=pod
88
89=head1 NAME
90
1ab07ed4 91Catalyst::IOC::ConstructorInjection
92
93=head1 SYNOPSIS
94
95=head1 DESCRIPTION
b7da37bd 96
97=head1 AUTHORS
98
99Catalyst Contributors, see Catalyst.pm
100
101=head1 COPYRIGHT
102
103This library is free software. You can redistribute it and/or modify it under
104the same terms as Perl itself.
105
106=cut