refactor component injection
[catagits/Catalyst-Runtime.git] / lib / Catalyst.pm
index b0e0129..fc278fb 100644 (file)
@@ -715,20 +715,15 @@ sub _comp_names {
 
 # Filter a component before returning by calling ACCEPT_CONTEXT if available
 
-#our %tracker = ();
 sub _filter_component {
     my ( $c, $comp, @args ) = @_;
 
-    # die "Circular Dependencies Detected." if $tracker{$comp};
-    #   $tracker{$comp}++;
     if(ref $comp eq 'CODE') {
       $comp = $comp->();
     }
-    #$tracker{$comp}++;
 
     if ( eval { $comp->can('ACCEPT_CONTEXT'); } ) {
-        die "Component '${\$comp->catalyst_component_name}' does ACCEPT_CONTEXT but I am in application scope" unless blessed $c;
-        return $comp->ACCEPT_CONTEXT( $c, @args );
+      return $comp->ACCEPT_CONTEXT( $c, @args );
     }
 
     $c->log->warn("You called component '${\$comp->catalyst_component_name}' with arguments [@args], but this component does not ACCEPT_CONTEXT, so args are ignored.") if scalar(@args) && $c->debug;
@@ -2855,25 +2850,49 @@ sub setup_components {
     # of named components in the configuration that are not actually existing (not a
     # real file).
 
+    my @injected_components = $class->setup_injected_components;
+
+    # All components are registered, now we need to 'init' them.
+    foreach my $component_name (@comps, @injected_components) {
+      $class->components->{$component_name} = $class->components->{$component_name}->() if
+        (ref($class->components->{$component_name}) || '') eq 'CODE';
+    }
+}
+
+=head2 $app->setup_injected_components
+
+Called by setup_compoents to setup components that are injected.
+
+=cut
+
+sub setup_injected_components {
+    my ($class) = @_;
     my @injected_components = keys %{$class->config->{inject_components} ||+{}};
+
     foreach my $injected_comp_name(@injected_components) {
-      my $component_class = $class->config->{inject_components}->{$injected_comp_name}->{from_component} || '';
-      if($component_class) {
-        my @roles = @{$class->config->{inject_components}->{$injected_comp_name}->{roles} ||[]};
-        my %args = %{ $class->config->{$injected_comp_name} || +{} };
+        $class->setup_injected_component(
+          $injected_comp_name,
+          $class->config->{inject_components}->{$injected_comp_name});
+    }
+
+    return @injected_components;
+}
+
+=head2 $app->setup_injected_component( $injected_component_name, $config )
+
+Setup a given injected component.
+
+=cut
 
+sub setup_injected_component {
+    my ($class, $injected_comp_name, $config) = @_;
+    if(my $component_class = $config->{from_component}) {
+        my @roles = @{$config->{roles} ||[]};
         Catalyst::Utils::inject_component(
           into => $class,
           component => $component_class,
           (scalar(@roles) ? (traits => \@roles) : ()),
           as => $injected_comp_name);
-      }
-    }
-
-    # All components are registered, now we need to 'init' them.
-    foreach my $component_name (@comps, @injected_components) {
-      $class->components->{$component_name} = $class->components->{$component_name}->() if
-        (ref($class->components->{$component_name}) || '') eq 'CODE';
     }
 }