Introduce a way to pass config down into the plugins
Tomas Doran [Sun, 24 Jan 2010 22:41:08 +0000 (22:41 +0000)]
Use this to implement a way to keep the current stash when doing
a subrequest or visit. Docs and tests to follow.

This is also pretty gross, it'd be nicer at this point to make the
plugins instances which are built (and their config merged) on first
use, rather than merging and passing the config down into the class
method every time.

lib/Catalyst/View/Component/SubInclude.pm
lib/Catalyst/View/Component/SubInclude/ESI.pm
lib/Catalyst/View/Component/SubInclude/SubRequest.pm
lib/Catalyst/View/Component/SubInclude/Visit.pm

index ba2c397..045cb9d 100644 (file)
@@ -2,6 +2,7 @@ package Catalyst::View::Component::SubInclude;
 use Moose::Role;
 
 use Carp qw/croak/;
+use Catalyst::Utils ();
 use namespace::clean -except => 'meta';
 
 with 'Catalyst::Component::ContextClosure';
@@ -143,7 +144,8 @@ sub _subinclude {
 sub _subinclude_using {
     my ($self, $c, $plugin, @args) = @_;
     $plugin = $self->_subinclude_plugin_class_name($plugin);
-    $plugin->generate_subinclude( $c, @args );
+    my $plugin_config = Catalyst::Utils::merge_hashes($self->config->{subinclude}->{ALL}||{}, $self->config->{subinclude}->{$plugin}||{});
+    $plugin->generate_subinclude( $plugin_config, $c, @args );
 }
 
 sub _subinclude_plugin_class_name {
index 4ce7799..6ea8d3c 100644 (file)
@@ -55,7 +55,7 @@ common interface for plugins.
 =cut
 
 sub generate_subinclude {
-    my ($class, $c, $path, @params) = @_;
+    my ($class, $config, $c, $path, @params) = @_;
 
     my $uri = $c->uri_for_action( $path, @params );
 
index 6f5cdfb..c1c882a 100644 (file)
@@ -71,8 +71,8 @@ common interface for all plugins.
 =cut
 
 sub generate_subinclude {
-    my ($class, $c, $path, @params) = @_;
-    my $stash = {};
+    my ($class, $config, $c, $path, @params) = @_;
+    my $stash = $config->{keep_stash} ? { %{ $c->stash } } : {};
 
     croak "subincludes through subrequests require Catalyst::Plugin::SubRequest"
         unless $c->can('sub_request');
index 034f058..d5c5ec0 100644 (file)
@@ -59,13 +59,13 @@ with the other plugins.
 =cut
 
 sub generate_subinclude {
-    my ($class, $c, $path, @params) = @_;
+    my ($class, $config, $c, $path, @params) = @_;
 
     croak "subincludes through visit() require Catalyst version 5.71000 or newer"
         unless $c->can('visit');
 
     {
-        local $c->{stash} = {};
+        local $c->{stash} = $config->{keep_stash} ? $c->{stash} : {};
         
         local $c->request->{parameters} = 
             ref $params[-1] eq 'HASH' ? pop @params : {};