From: Tomas Doran Date: Wed, 10 Aug 2011 23:52:06 +0000 (+0100) Subject: Almost working sugar X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=7cd05fd23b8593ff14e73c068817f5fa62ce698a Almost working sugar --- diff --git a/lib/Catalyst/IOC.pm b/lib/Catalyst/IOC.pm index b04f32f..0c6ad6e 100644 --- a/lib/Catalyst/IOC.pm +++ b/lib/Catalyst/IOC.pm @@ -4,13 +4,20 @@ use warnings; use Bread::Board; use Catalyst::IOC::ConstructorInjection; -# FIXME - neither of these work: +# FIXME - All of these imports need to get the importing package +# as the customise_container and current_container variables +# NEED to be in the containers package so there can be multiple +# containers.. use Sub::Exporter -setup => { exports => [qw/ component + model + container /], groups => { default => [qw/ component + model + container /]}, }; #use Sub::Exporter -setup => [ @@ -32,6 +39,23 @@ use Sub::Exporter -setup => { #Moose::Exporter->setup_import_methods( # also => ['Bread::Board'], #); +our $customise_container; +our $current_container; + +sub container (&) { + my $code = shift; + $customise_container = sub { + warn("In customise container"); + local $current_container = shift(); + $code->(); + }; +} + +sub model (&) { + my $code = shift; + local $current_container = $current_container->get_sub_container('model'); + $code->(); +} sub component { my ($name, %args) = @_; @@ -40,14 +64,15 @@ sub component { # FIXME - check $args{type} here! - Catalyst::IOC::ConstructorInjection->new( + my $service = Catalyst::IOC::ConstructorInjection->new( %args, name => $name, lifecycle => 'Singleton', # XX FIXME - needs to become possible to intuit catalyst_component_name # from dependencies (like config) catalyst_component_name => 'TestAppCustomContainer::Model::Bar', - ) + ); + $current_container->add_service($service); } 1; diff --git a/t/lib/TestAppCustomContainer.pm b/t/lib/TestAppCustomContainer.pm index a70d3f0..8915669 100644 --- a/t/lib/TestAppCustomContainer.pm +++ b/t/lib/TestAppCustomContainer.pm @@ -4,9 +4,12 @@ use Catalyst; extends 'Catalyst'; use namespace::autoclean; + confess("No default container") unless $ENV{TEST_APP_CURRENT_CONTAINER}; +Class::MOP::load_class($ENV{TEST_APP_CURRENT_CONTAINER}); # FIXME! +# Custom container name is silently ignored if the class doesn't exist! __PACKAGE__->config( container_class => $ENV{TEST_APP_CURRENT_CONTAINER} -) if $ENV{TEST_APP_CURRENT_CONTAINER}; +); __PACKAGE__->setup; diff --git a/t/lib/TestAppCustomContainer/SugarContainer.pm b/t/lib/TestAppCustomContainer/SugarContainer.pm index 80c0fff..514ccbb 100644 --- a/t/lib/TestAppCustomContainer/SugarContainer.pm +++ b/t/lib/TestAppCustomContainer/SugarContainer.pm @@ -2,21 +2,24 @@ package TestAppCustomContainer::SugarContainer; use Moose; use namespace::autoclean; use Catalyst::IOC; -use Bread::Board; +use Bread::Board qw/ depends_on /; extends 'Catalyst::IOC::Container'; sub BUILD { my $self = shift; + warn("In build"); + $Catalyst::IOC::customise_container->($self); +} - warn("Add Bar to model"); - $self->get_sub_container('model')->add_service( +container { + model { component( 'Bar' => class => 'TestAppCustomContainer::Model::Bar', dependencies => { foo => depends_on('/model/DefaultSetup') }, - ) - ); -} + ); + }; +}; __PACKAGE__->meta->make_immutable;