fixed issue when injecting controllers
John Napiorkowski [Wed, 22 Apr 2015 20:09:44 +0000 (15:09 -0500)]
Changes
lib/Catalyst.pm
lib/Catalyst/Dispatcher.pm
t/accept_context_regression.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index d382ed1..decb05b 100644 (file)
--- a/Changes
+++ b/Changes
@@ -27,8 +27,8 @@
     in general any request or response trait on CPAN that used 'CatalystX::RoleApplicator'
     should now just work with this core feature.  Note that  can also set thse roles
     via new configuration keys, 'request_class_traits', 'response_class_traits' 
-    and 'stats_class_traits'. If you use both config at application class methods, they
-    are combined.
+    and 'stats_class_traits'. If you use both configuration and application class methods,
+    they are combined.
   - NEW FEATURE: Core concepts from 'CatalystX::ComponentsFromConfig'.  You can now
     setup components directly from configuration.  This could save you some effort and
     creating 'empty' base classes in your Model/View and Controller directories.  This
index e0dac5d..2c6df37 100644 (file)
@@ -493,7 +493,7 @@ L<< detach|/"$c->detach( $action [, \@arguments ] )" >>. Like C<< $c->visit >>,
 C<< $c->go >> will perform a full dispatch on the specified action or method,
 with localized C<< $c->action >> and C<< $c->namespace >>. Like C<detach>,
 C<go> escapes the processing of the current request chain on completion, and
-does not return to its caller.
+does not return to its cunless blessed $cunless blessed $caller.
 
 @arguments are arguments to the final destination of $action. @captures are
 arguments to the intermediate steps, if any, on the way to the final sub of
@@ -542,6 +542,7 @@ t/middleware-stash.t in the distribution /t directory.
 
 sub stash {
   my $c = shift;
+  $c->log->error("You are requesting the stash but you don't have a context") unless blessed $c;
   return Catalyst::Middleware::Stash::get_stash($c->req->env)->(@_);
 }
 
@@ -721,11 +722,12 @@ sub _filter_component {
     # die "Circular Dependencies Detected." if $tracker{$comp};
     #   $tracker{$comp}++;
     if(ref $comp eq 'CODE') {
-      $comp = $comp->($c);
+      $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 );
     }
 
@@ -2869,9 +2871,9 @@ sub setup_components {
     }
 
     # All components are registered, now we need to 'init' them.
-    foreach my $component_name (keys %{$class->components||{}}) {
+    foreach my $component_name (@comps, @injected_components) {
       $class->components->{$component_name} = $class->components->{$component_name}->() if
-      (ref($class->components->{$component_name}) || '') eq 'CODE';
+        (ref($class->components->{$component_name}) || '') eq 'CODE';
     }
 }
 
index 4b9fa8e..f63267b 100644 (file)
@@ -613,8 +613,8 @@ sub setup_actions {
       $self->_load_dispatch_types( @{ $self->preload_dispatch_types } );
     @{ $self->_registered_dispatch_types }{@classes} = (1) x @classes;
 
-    foreach my $comp_key ( keys %{ $c->components } ) {
-      my $comp = $c->component($comp_key);
+    foreach my $comp ( values %{ $c->components } ) {
+        $comp = $comp->() if ref($comp) eq 'CODE';
         $comp->register_actions($c) if $comp->can('register_actions');
     }
 
diff --git a/t/accept_context_regression.t b/t/accept_context_regression.t
new file mode 100644 (file)
index 0000000..0fb09e9
--- /dev/null
@@ -0,0 +1,40 @@
+use Test::Most;
+
+{
+  package MyApp::Model::AcceptContext;
+  use base 'Catalyst::Model';
+
+  sub ACCEPT_CONTEXT {
+    my ($self, $c, @args) = @_;
+    Test::Most::ok( ref $c);
+  }
+
+  $INC{'MyApp/Model/AcceptContext.pm'} = __FILE__;
+
+  package MyApp::Controller::Root;
+  use base 'Catalyst::Controller';
+
+  sub test_model :Local {
+    my ($self, $c) = @_;
+    $c->res->body('test');
+  }
+
+  $INC{'MyApp/Controller/Root.pm'} = __FILE__;
+
+  package MyApp;
+  use Catalyst;
+  
+  MyApp->setup;
+}
+
+use Catalyst::Test 'MyApp';
+
+my ($res, $c) = ctx_request('/test_model');
+
+ok $res;
+
+use Devel::Dwarn;
+#Dwarn $c->model('AcceptContext');
+
+done_testing;
+