overridden locate_components
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC.pm
index b04f32f..1ac807b 100644 (file)
@@ -1,16 +1,26 @@
 package Catalyst::IOC;
 use strict;
 use warnings;
-use Bread::Board;
+use Bread::Board qw/depends_on/;
 use Catalyst::IOC::ConstructorInjection;
+no strict 'refs';
 
-# 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/
+        depends_on
         component
+        model
+        container
     /],
     groups  => { default => [qw/
+        depends_on
         component
+        model
+        container
     /]},
 };
 #use Sub::Exporter -setup => [
@@ -32,22 +42,46 @@ use Sub::Exporter -setup => {
 #Moose::Exporter->setup_import_methods(
 #    also => ['Bread::Board'],
 #);
+sub container (&) {
+    my $code = shift;
+    my $caller = caller;
+    ${"${caller}::customise_container"} = sub {
+        warn("In customise container");
+        local ${"${caller}::current_container"} = shift;
+        $code->();
+    };
+}
+
+sub model (&) {
+    my $code = shift;
+    my $caller = caller;
+    local ${"${caller}::current_container"} = ${"${caller}::current_container"}->get_sub_container('model');
+    $code->();
+}
 
 sub component {
     my ($name, %args) = @_;
+    my $caller = caller;
     $args{dependencies} ||= {};
     $args{dependencies}{application_name} = depends_on( '/application_name' );
 
     # FIXME - check $args{type} here!
 
-    Catalyst::IOC::ConstructorInjection->new(
+    my $component_name = join '::', (
+        ${"${caller}::current_container"}->resolve(service => '/application_name'),
+        ucfirst(${"${caller}::current_container"}->name),
+        $name
+    );
+
+    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',
-    )
+        catalyst_component_name => $component_name,
+    );
+    ${"${caller}::current_container"}->add_service($service);
 }
 
 1;