package Catalyst::View::Component::SubInclude::SubRequest;
-use warnings;
-use strict;
-
+use Moose;
use Carp qw/croak/;
-use namespace::clean;
+use MooseX::Types::Moose qw/ Bool /;
+use namespace::clean -except => 'meta';
=head1 NAME
=head1 VERSION
-Version 0.07
+Version 0.07_03
=cut
-our $VERSION = '0.07';
+our $VERSION = '0.07_03';
+$VERSION = eval $VERSION;
=head1 SYNOPSIS
=head1 DESCRIPTION
C<Catalyst::View::Component::SubInclude::SubRequest> uses Catalyst sub-requests
-to render the subinclude contents.
+to render the subinclude contents.
It requires L<Catalyst::Plugin::SubRequest>.
-=head1 CLASS METHODS
+=head1 METHODS
=head2 C<generate_subinclude( $c, $path, @args )>
$c->sub_request( $translated_path, {}, @args );
-Notice that the stash will always be empty. This behavior could be configurable
-in the future through an additional switch - for now, this behavior guarantees a
-common interface for all plugins.
+Notice that the stash will be empty by default. This behavior is configurable
+(see below).
+
+=head1 CONFIGURATION
+
+=head2 keep_stash
+
+You can choose to not localize the stash for SubRequests' subinclude calls. The subrequest
+will have the same stash as the request that spawned it. Configure the keep_stash key
+in your view:
+
+ __PACKAGE__->config(
+ subinclude => {
+ 'SubRequest' => {
+ keep_stash => 1,
+ },
+ }
+ );
+
+Note: the stash that the subrequest recieves is a shallow copy of the original stash. That
+means that changes to values of keys on the first level of the stash will be lost when the
+subrequest call returns. Don't count on this behaviour, as it may change in the future.
=cut
+has keep_stash => (
+ isa => Bool,
+ is => 'ro',
+ default => 0,
+);
+
sub generate_subinclude {
- my ($class, $c, $path, @params) = @_;
- my $stash = {};
+ my ($self, $c, $path, @params) = @_;
+ my $stash = $self->keep_stash ? $c->stash : {};
croak "subincludes through subrequests require Catalyst::Plugin::SubRequest"
unless $c->can('sub_request');
- my $args = ref $params[0] eq 'ARRAY' ? shift @params : [];
- my $query = ref $params[-1] eq 'HASH' ? pop @params : {};
-
- my $dispatcher = $c->dispatcher;
- my ($action) = $dispatcher->_invoke_as_path( $c, $path, $args );
+ my $query = ref $params[-1] eq 'HASH' ? pop @params : {};
+
+ my $action = blessed($path)
+ ? $path
+ : $c->dispatcher->get_action_by_path($path);
- my $uri = $c->uri_for( $action, $args, @params );
+ my $uri = $c->uri_for( $action, @params );
$c->sub_request( $uri->path, $stash, $query );
}
=head1 SEE ALSO
-L<Catalyst::View::Component::SubInclude|Catalyst::View::Component::SubInclude>,
+L<Catalyst::View::Component::SubInclude|Catalyst::View::Component::SubInclude>,
L<Catalyst::Plugin::SubRequest|Catalyst::Plugin::SubRequest>
=head1 AUTHOR
=cut
+__PACKAGE__->meta->make_immutable;
1;