removing noise (comments and warnings)
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC.pm
index a204295..a71bddb 100644 (file)
@@ -1,46 +1,72 @@
 package Catalyst::IOC;
 use strict;
 use warnings;
-use Bread::Board;
-
-# FIXME - neither of these work:
-#use Sub::Exporter -setup => [
-#    qw(
-#        as
-#        container
-#        depends_on
-#        service
-#        alias
-#        wire_names
-#        include
-#        typemap
-#        infer
-#    )
-#];
-#use Sub::Exporter -setup => [
-#    qw(
-#        Bread::Board::as
-#        Bread::Board::container
-#        Bread::Board::depends_on
-#        Bread::Board::service
-#        Bread::Board::alias
-#        Bread::Board::wire_names
-#        Bread::Board::include
-#        Bread::Board::typemap
-#        Bread::Board::infer
-#    )
-#];
-# I'm probably doing it wrong.
-# Anyway, I'll just use Moose::Exporter. Do I really have to use Sub::Exporter?
-use Moose::Exporter;
-Moose::Exporter->setup_import_methods(
-    also => ['Bread::Board'],
-);
+use Bread::Board qw/depends_on/;
+use Catalyst::IOC::ConstructorInjection;
+no strict 'refs';
+
+use Sub::Exporter -setup => {
+    exports => [qw/
+        depends_on
+        component
+        model
+        container
+    /],
+    groups  => { default => [qw/
+        depends_on
+        component
+        model
+        container
+    /]},
+};
+
+sub container (&) {
+    my $code = shift;
+    my $caller = caller;
+    ${"${caller}::customise_container"} = sub {
+        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' );
+
+    my $lifecycle = $args{lifecycle};
+    my %catalyst_lifecycles = map { $_ => 1 } qw/ COMPONENTSingleton Request /;
+    $args{lifecycle} = $lifecycle
+                     ? $catalyst_lifecycles{$lifecycle} ? "+Catalyst::IOC::LifeCycle::$lifecycle" : $lifecycle
+                     : 'Singleton'
+                     ;
+
+    # FIXME - check $args{type} here!
+
+    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,
+        catalyst_component_name => $component_name,
+    );
+    ${"${caller}::current_container"}->add_service($service);
+}
 
 1;
 
-# FIXME - should the code example below be on this file or Catalyst::IOC::Container?
-
 __END__
 
 =pod