I like this more than the constructorinjection getting a ->config parameter, but...
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC / ConstructorInjection.pm
CommitLineData
b7da37bd 1package Catalyst::IOC::ConstructorInjection;
2use Moose;
eff5194d 3use Try::Tiny;
9e6091e2 4use Catalyst::Utils ();
b7da37bd 5extends 'Bread::Board::ConstructorInjection';
6
bf142143 7with 'Bread::Board::Service::WithClass',
104155f6 8 'Bread::Board::Service::WithParameters',
c614c3c2 9 'Bread::Board::Service::WithDependencies';
b7da37bd 10
00f61c9b 11has config => (
9e6091e2 12 is => 'ro',
00f61c9b 13 isa => 'HashRef',
14 required => 1,
9e6091e2 15);
16
b7da37bd 17sub get {
18 my $self = shift;
19
b7da37bd 20 my $component = $self->class;
00f61c9b 21 my %config = (%{ $self->config }, %{ $self->params });
b32d8169 22
3f1b0032 23 # FIXME - Is depending on the application name to pass into constructors here a good idea?
24 # This makes app/ctx split harder I think.. Need to think more here, but I think
25 # we want to pass the application in as a parameter when building the service
26 # rather than depending on the app name, so that later, when the app becomes an instance
27 # then it'll get passed in, and components can stash themselves 'per app instance'
c614c3c2 28 my $app_name = $self->param('application_name');
b7da37bd 29
b7da37bd 30 # Stash catalyst_component_name in the config here, so that custom COMPONENT
31 # methods also pass it. local to avoid pointlessly shitting in config
32 # for the debug screen, as $component is already the key name.
00f61c9b 33 # XXX FIXME - WRONG!!! MyApp::Model::Foo may be an instance of something
34 # totally diferent, ergo it should get a catalyst_component_name
35 # of MyApp::Model::Foo.. Write failing tests for this in master?
36 $config{catalyst_component_name} = $component;
b7da37bd 37
00f61c9b 38 unless ( $component->can( 'COMPONENT' ) ) {
c614c3c2 39 # FIXME - make some deprecation warnings
40 return $component;
41 }
42
00f61c9b 43 my $instance;
eff5194d 44 try {
00f61c9b 45 $instance = $component->COMPONENT( $app_name, \%config );
c614c3c2 46 }
eff5194d 47 catch {
c614c3c2 48 Catalyst::Exception->throw(
eff5194d 49 message => qq/Couldn't instantiate component "$component", "$_"/
c614c3c2 50 );
eff5194d 51 };
52
53 return $instance
54 if blessed $instance;
55
56 my $metaclass = Moose::Util::find_meta($component);
00f61c9b 57 my $method_meta = $metaclass->find_method_by_name('COMPONENT');
eff5194d 58 my $component_method_from = $method_meta->associated_metaclass->name;
59 my $value = defined($instance) ? $instance : 'undef';
60 Catalyst::Exception->throw(
61 message =>
00f61c9b 62 qq/Couldn't instantiate component "$component", COMPONENT method (from $component_method_from) didn't return an object-like value (value was $value)./
eff5194d 63 );
b7da37bd 64}
65
66__PACKAGE__->meta->make_immutable;
67
68no Moose; 1;
69
70__END__
71
72=pod
73
74=head1 NAME
75
1ab07ed4 76Catalyst::IOC::ConstructorInjection
77
78=head1 SYNOPSIS
79
80=head1 DESCRIPTION
b7da37bd 81
82=head1 AUTHORS
83
84Catalyst Contributors, see Catalyst.pm
85
86=head1 COPYRIGHT
87
88This library is free software. You can redistribute it and/or modify it under
89the same terms as Perl itself.
90
91=cut