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