created ConstructorInjection (NOT working)
André Walker [Thu, 28 Jul 2011 21:08:48 +0000 (18:08 -0300)]
lib/Catalyst/IOC/ConstructorInjection.pm [new file with mode: 0644]
lib/Catalyst/IOC/Container.pm

diff --git a/lib/Catalyst/IOC/ConstructorInjection.pm b/lib/Catalyst/IOC/ConstructorInjection.pm
new file mode 100644 (file)
index 0000000..e43b1cc
--- /dev/null
@@ -0,0 +1,70 @@
+package Catalyst::IOC::ConstructorInjection;
+use Moose;
+extends 'Bread::Board::ConstructorInjection';
+
+with 'Catalyst::IOC::Service::WithAcceptContext';
+
+sub _build_constructor_name { 'COMPONENT' }
+
+sub get {
+    my $self = shift;
+
+    my $constructor = $self->constructor_name;
+    my $config      = $self->param('config')->{ $self->params->{suffix} } || {};
+    my $class       = $self->param('class');
+    my $component   = $self->class;
+
+    unless ( $self->class->can( $constructor ) ) {
+        # FIXME - make some deprecation warnings
+        return $component;
+    }
+
+    # 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} = $self->class;
+
+    my $instance = eval { $component->$constructor( $class, $config ) };
+
+    if ( my $error = $@ ) {
+        chomp $error;
+        Catalyst::Exception->throw(
+            message => qq/Couldn't instantiate component "$component", "$error"/
+        );
+    }
+    elsif (!blessed $instance) {
+        my $metaclass = Moose::Util::find_meta($component);
+        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", COMPONENT() method (from $component_method_from) didn't return an object-like value (value was $value)./
+        );
+    }
+
+    return $instance;
+}
+
+__PACKAGE__->meta->make_immutable;
+
+no Moose; 1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+Catalyst::IOC::BlockInjection
+
+=head1 AUTHORS
+
+Catalyst Contributors, see Catalyst.pm
+
+=head1 COPYRIGHT
+
+This library is free software. You can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
index 2abd08b..9963633 100644 (file)
@@ -9,6 +9,7 @@ use Hash::Util qw/lock_hash/;
 use MooseX::Types::LoadableClass qw/ LoadableClass /;
 use Moose::Util;
 use Catalyst::IOC::BlockInjection;
+use Catalyst::IOC::ConstructorInjection;
 use Module::Pluggable::Object ();
 use namespace::autoclean;
 
@@ -615,14 +616,22 @@ sub get_all_components {
 sub add_component {
     my ( $self, $component, $class ) = @_;
     my ( $type, $name ) = _get_component_type_name($component);
+    my $suffix = Catalyst::Utils::class2classsuffix( $component );
 
     return unless $type;
 
     $self->get_sub_container($type)->add_service(
-        Catalyst::IOC::BlockInjection->new(
+        Catalyst::IOC::ConstructorInjection->new(
             lifecycle => 'Singleton', # FIXME?
             name      => $name,
-            block     => sub { $self->setup_component( $component, $class ) },
+            class     => $component,
+            dependencies => {
+                application_name => Bread::Board::Dependency->new( service_path => '/application_name' ),
+                config => Bread::Board::Dependency->new( service_path => '/config' ),
+            },
+            params => {
+                suffix => $suffix,
+            },
         )
     );
 }