Created component sub-container, brought BlockInjection back
André Walker [Mon, 1 Aug 2011 21:08:33 +0000 (18:08 -0300)]
lib/Catalyst/IOC/BlockInjection.pm [new file with mode: 0644]
lib/Catalyst/IOC/ConstructorInjection.pm
lib/Catalyst/IOC/Container.pm
lib/Catalyst/IOC/Service/WithAcceptContext.pm
lib/Catalyst/IOC/Service/WithCOMPONENT.pm

diff --git a/lib/Catalyst/IOC/BlockInjection.pm b/lib/Catalyst/IOC/BlockInjection.pm
new file mode 100644 (file)
index 0000000..00ed4a5
--- /dev/null
@@ -0,0 +1,34 @@
+package Catalyst::IOC::BlockInjection;
+use Moose;
+extends 'Bread::Board::BlockInjection';
+
+with 'Bread::Board::Service::WithDependencies',
+     'Bread::Board::Service::WithParameters',
+     'Catalyst::IOC::Service::WithAcceptContext';
+
+__PACKAGE__->meta->make_immutable;
+
+no Moose; 1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+Catalyst::IOC::BlockInjection
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+=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 108d94f..6474fd5 100644 (file)
@@ -5,8 +5,7 @@ extends 'Bread::Board::ConstructorInjection';
 with 'Bread::Board::Service::WithClass',
      'Bread::Board::Service::WithDependencies',
      'Bread::Board::Service::WithParameters',
-     'Catalyst::IOC::Service::WithCOMPONENT',
-     'Catalyst::IOC::Service::WithAcceptContext';
+     'Catalyst::IOC::Service::WithCOMPONENT';
 
 # FIXME - how much of this should move to ::WithCOMPONENT?
 sub get {
index 742a5eb..b3559b6 100644 (file)
@@ -8,6 +8,7 @@ use Devel::InnerPackage ();
 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;
@@ -82,6 +83,10 @@ sub BUILD {
     my $config = $self->resolve( service => 'config' );
 
     $self->add_sub_container(
+        $self->build_component_subcontainer
+    );
+
+    $self->add_sub_container(
         $self->build_controller_subcontainer
     );
 
@@ -122,6 +127,14 @@ sub build_controller_subcontainer {
     );
 }
 
+sub build_component_subcontainer {
+    my $self = shift;
+
+    return Bread::Board::Container->new(
+        name => 'component',
+    );
+}
+
 sub build_application_name_service {
     my $self = shift;
 
@@ -637,10 +650,13 @@ sub add_component {
 
     return unless $type;
 
-    $self->get_sub_container($type)->add_service(
+    my $component_service_name = "${type}_${name}";
+
+    $self->get_sub_container('component')->add_service(
         Catalyst::IOC::ConstructorInjection->new(
-            name      => $name,
+            name      => $component_service_name,
             class     => $component,
+            lifecycle => 'Singleton',
             dependencies => [
                 depends_on( '/application_name' ),
                 depends_on( '/config' ),
@@ -650,12 +666,24 @@ sub add_component {
                     isa => 'Str',
                     default => Catalyst::Utils::class2classsuffix( $component ),
                 },
+            },
+        )
+    );
+
+    $self->get_sub_container($type)->add_service(
+        Catalyst::IOC::BlockInjection->new(
+            name         => $name,
+            dependencies => [
+                depends_on( "/component/$component_service_name" ),
+            ],
+            parameters => {
                 accept_context_args => {
                     isa => 'ArrayRef|Undef',
                     required => 0,
                     default => undef,
                 },
             },
+            block => sub { return shift->param($component_service_name) },
         )
     );
 }
index f1bf3d8..4559a69 100644 (file)
@@ -18,7 +18,7 @@ around 'get' => sub {
     my $accept_context_args = $self->param('accept_context_args');
     my $ac_sub = $self->accept_context_sub;
 
-    if ( $accept_context_args && $instance->can($ac_sub) ) {
+    if ( $instance->can($ac_sub) ) {
         return $instance->$ac_sub( @$accept_context_args );
     }
 
@@ -34,7 +34,7 @@ __END__
 
 =head1 NAME
 
-Catalyst::Service::WithAcceptContext
+Catalyst::IOC::Service::WithAcceptContext
 
 =head1 DESCRIPTION
 
index 0effd2d..97a08ea 100644 (file)
@@ -3,14 +3,6 @@ use Moose::Role;
 
 with 'Bread::Board::Service';
 
-# FIXME - just till I understand how it's supposed to be done
-# Made this so that COMPONENT is executed once,
-# and ACCEPT_CONTEXT every call.
-has instance => (
-    is => 'rw',
-    required => 0,
-);
-
 sub _build_constructor_name { 'COMPONENT' }
 
 around 'get' => sub {
@@ -24,10 +16,6 @@ around 'get' => sub {
         return $component;
     }
 
-    if ($self->instance) {
-        return $self->instance;
-    }
-
     my $instance = eval { $self->$orig() };
 
     if ( my $error = $@ ) {
@@ -47,8 +35,6 @@ around 'get' => sub {
         );
     }
 
-    $self->instance($instance);
-
     return $instance;
 };
 
@@ -61,7 +47,7 @@ __END__
 
 =head1 NAME
 
-Catalyst::Service::WithCOMPONENT
+Catalyst::IOC::Service::WithCOMPONENT
 
 =head1 DESCRIPTION