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