I'm really sorry
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC / Container.pm
index 68d930b..4370a9a 100644 (file)
@@ -108,6 +108,13 @@ sub BUILD {
     $self->add_sub_container(
         $self->build_model_subcontainer( @default_model )
     );
+
+    {
+        no strict 'refs';
+        my $class = ref $self;
+        warn("In build " . ${ $class . '::customise_container' });
+        ${ $class . '::customise_container' }->($self);
+    }
 }
 
 sub build_model_subcontainer {
@@ -592,16 +599,16 @@ sub find_component {
 }
 
 sub _find_component_regexp {
-    my ( $self, $component, @args ) = @_;
+    my ( $self, $component, $ctx, @args ) = @_;
     my @result;
 
-    my @components = grep { m{$component} } keys %{ $self->get_all_components };
+    my @components = grep { m{$component} } keys %{ $self->get_all_components($ctx) };
 
     for (@components) {
         my ($type, $name) = _get_component_type_name($_);
 
         push @result, $self->get_component_from_sub_container(
-            $type, $name, @args
+            $type, $name, $ctx, @args
         ) if $type;
     }
 
@@ -609,7 +616,7 @@ sub _find_component_regexp {
 }
 
 sub get_all_components {
-    my $self = shift;
+    my ($self, $class) = @_;
     my %components;
 
     # FIXME - if we're getting from these containers, we need to either:
@@ -619,27 +626,9 @@ sub get_all_components {
         my $container = $self->get_sub_container($type);
 
         for my $component ($container->get_service_list) {
-            my $comp = $container->resolve(
-                service => $component
-            );
-            my $comp_name = ref $comp || $comp; # THIS IS WRONG! :)
-                                                # Just as it is called Model::Foo
-                                                # does not mean it has to be
-                                                # an instance of model::foo
-            # (AndrĂ©'s answer)
-            # t0m, you're absolutely right, I really hadn't thought about it.
-            # But then, we have a problem: suppose there is a component called
-            # MyApp::M::Foo, for instance. The service name would be 'Foo',
-            # and it would be stored in the 'model' sub container. So we have
-            # $app_name . '::' . uc_first($type) . '::' . $service_name
-            # that would return MyApp::Model::Foo. It would get really, really
-            # ugly to check MyApp::M::Foo. So, either we change the hash key,
-            # or we drop support for ::[CMV]::, or I don't know, maybe you
-            # have a better solution? :)
-            # maybe catalyst_component_name? But then we'd have the same
-            # problem on Catalyst::IOC line 73
-
-            $components{$comp_name} = $comp;
+            my $comp_service = $container->get_service($component);
+
+            $components{$comp_service->catalyst_component_name} = $comp_service->get(ctx => $class);
         }
     }
 
@@ -652,8 +641,6 @@ sub add_component {
 
     return unless $type;
 
-    my $component_service_name = "${type}_${name}";
-
     # The 'component' sub-container will create the object, and store it's
     # instance, which, by default, will live throughout the application.
     # The model/view/controller sub-containers only reference the instance
@@ -662,6 +649,12 @@ sub add_component {
     my $instance_container       = $self->get_sub_container('component');
     my $accept_context_container = $self->get_sub_container($type);
 
+    # Custom containers might have added the service already
+    # We don't want to override that
+    return if $accept_context_container->has_service( $name );
+
+    my $component_service_name = "${type}_${name}";
+
     $instance_container->add_service(
         Catalyst::IOC::ConstructorInjection->new(
             name      => $component_service_name,
@@ -672,20 +665,18 @@ sub add_component {
                 depends_on( '/application_name' ),
             ],
         )
-    ) unless $instance_container->has_service( $component_service_name );
-    # ^ custom containers might have added the service already.
-    # we don't want to override that.
+    );
 
     $accept_context_container->add_service(
         Catalyst::IOC::BlockInjection->new(
             name         => $name,
+            catalyst_component_name => $component,
             dependencies => [
                 depends_on( "/component/$component_service_name" ),
             ],
             block => sub { shift->param($component_service_name) },
         )
-    ) unless $accept_context_container->has_service( $name );
-    # ^ same as above
+    );
 }
 
 # FIXME: should this sub exist?