package Catalyst::IOC::ConstructorInjection;
use Moose;
+use Bread::Board::Dependency;
use Try::Tiny;
use Catalyst::Utils ();
+
extends 'Bread::Board::ConstructorInjection';
-with 'Bread::Board::Service::WithClass',
- 'Bread::Board::Service::WithParameters',
- 'Bread::Board::Service::WithDependencies';
+sub BUILD {
+ my $self = shift;
+ $self->add_dependency(
+ __catalyst_config => Bread::Board::Dependency->new(
+ service_path => '/config'
+ )
+ );
+}
-has config_key => (
- is => 'ro',
- isa => 'Str',
- lazy_build => 1,
+has catalyst_component_name => (
+ is => 'ro',
);
-sub _build_config_key { Catalyst::Utils::class2classsuffix( shift->class ) }
+has config => (
+ init_arg => undef,
+ is => 'ro',
+ isa => 'HashRef',
+ writer => '_set_config',
+ clearer => '_clear_config',
+);
-sub _build_constructor_name { 'COMPONENT' }
+around resolve_dependencies => sub {
+ my ($orig, $self, @args) = @_;
+ my %deps = $self->$orig(@args);
+ my $app_config = delete $deps{__catalyst_config};
+ my $conf_key = Catalyst::Utils::class2classsuffix($self->catalyst_component_name);
+ $self->_set_config($app_config->{$conf_key} || {});
+ return %deps;
+};
sub get {
my $self = shift;
+ my $component = $self->class;
- my $instance;
+ my $params = $self->params;
+ my %config = (%{ $self->config || {} }, %{ $params });
+ $self->_clear_config;
- my $constructor = $self->constructor_name;
- my $component = $self->class;
- my $config = $self->param('config')->{ $self->config_key } || {};
# FIXME - Is depending on the application name to pass into constructors here a good idea?
# This makes app/ctx split harder I think.. Need to think more here, but I think
# we want to pass the application in as a parameter when building the service
my $app_name = $self->param('application_name');
# Stash catalyst_component_name in the config here, so that custom COMPONENT
- # methods also pass it. local to avoid pointlessly shitting in config
- # for the debug screen, as $component is already the key name.
- local $config->{catalyst_component_name} = $component;
+ # methods also pass it.
+ $config{catalyst_component_name} = $self->catalyst_component_name;
- unless ( $component->can( $constructor ) ) {
+ unless ( $component->can( 'COMPONENT' ) ) {
# FIXME - make some deprecation warnings
return $component;
}
+ my $instance;
try {
- $instance = $component->$constructor( $app_name, $config );
+ $instance = $component->COMPONENT( $app_name, \%config );
}
catch {
Catalyst::Exception->throw(
if blessed $instance;
my $metaclass = Moose::Util::find_meta($component);
- my $method_meta = $metaclass->find_method_by_name($constructor);
+ my $method_meta = $metaclass->find_method_by_name('COMPONENT');
my $component_method_from = $method_meta->associated_metaclass->name;
my $value = defined($instance) ? $instance : 'undef';
Catalyst::Exception->throw(
message =>
- qq/Couldn't instantiate component "$component", $constructor() method (from $component_method_from) didn't return an object-like value (value was $value)./
+ qq/Couldn't instantiate component "$component", COMPONENT method (from $component_method_from) didn't return an object-like value (value was $value)./
);
}