Changelog, bump versions
[catagits/Catalyst-View-Component-SubInclude.git] / lib / Catalyst / View / Component / SubInclude.pm
index 77c5945..57fd5ca 100644 (file)
@@ -2,7 +2,12 @@ package Catalyst::View::Component::SubInclude;
 use Moose::Role;
 
 use Carp qw/croak/;
-use namespace::clean qw/croak/;
+use Catalyst::Utils ();
+use Class::MOP ();
+use MooseX::Types::Moose qw/Str HashRef/;
+use namespace::clean -except => 'meta';
+
+with 'Catalyst::Component::ContextClosure';
 
 =head1 NAME
 
@@ -10,11 +15,12 @@ Catalyst::View::Component::SubInclude - Use subincludes in your Catalyst views
 
 =head1 VERSION
 
-Version 0.02
+Version 0.07_01
 
 =cut
 
-our $VERSION = '0.02';
+our $VERSION = '0.07_01';
+$VERSION = eval $VERSION;
 
 =head1 SYNOPSIS
 
@@ -29,6 +35,7 @@ our $VERSION = '0.02';
 Then, somewhere in your templates:
 
   [% subinclude('/my/widget') %]
+  [% subinclude_using('SubRequest', '/page/footer') %]
 
 =head1 DESCRIPTION
 
@@ -45,7 +52,7 @@ ESI (L<http://www.catalystframework.org/calendar/2008/17>) or any other
 sub-include plugin you might want to implement. An LWP plugin seems useful and
 might be developed in the future.
 
-=head1 STASH FUNCTION
+=head1 STASH FUNCTIONS
 
 This component does its magic by exporting a C<subinclude> coderef entry to the
 stash. This way, it's easily accessible by the templates (which is the most 
@@ -54,7 +61,15 @@ common use-case).
 =head2 C<subinclude( $path, @args )>
 
 This will render and return the body of the included resource (as specified by 
-C<$path>).
+C<$path>) using the default subinclude plugin.
+
+=head2 C<subinclude_using( $plugin, $path, @args )>
+
+This will render and return the body of the included resource (as specified by 
+C<$path>) using the specified subinclude plugin.
+
+The C<subinclude> function above is implemented basically as a shortcut which 
+calls this function using the default plugin as the first parameter.
 
 =head1 SUBINCLUDE PLUGINS
 
@@ -63,7 +78,7 @@ L<SubRequest|Catalyst::Plugin::View::Component::SubRequest>,
 L<Visit|Catalyst::Plugin::View::Component::Visit> and 
 L<ESI|Catalyst::Plugin::View::Component::ESI>.
 
-By default, the SubRequest plugin will be used. This can be changed in the 
+By default, the C<SubRequest> plugin will be used. This can be changed in the 
 view's configuration options (either in the config file or in the view module
 itself). 
 
@@ -73,6 +88,14 @@ Configuration file example:
       subinclude_plugin   ESI
   </View::TT>
 
+=head2 C<set_subinclude_plugin( $plugin )>
+
+This method changes the current active subinclude plugin in runtime. It expects
+the plugin suffix (e.g. C<ESI> or C<SubRequest>) or a fully-qualified class 
+name in the C<Catalyst::View::Component::SubInclude> namespace.
+
+=head2 Writing plugins
+
 If writing your own plugin, keep in kind plugins are required to implement a 
 class method C<generate_subinclude> with the following signature:
 
@@ -80,12 +103,15 @@ class method C<generate_subinclude> with the following signature:
       my ($class, $c, @args) = @_;
   }
 
+The default plugin is stored in the C<subinclude_plugin> which can be changed
+in runtime. It expects a fully qualified class name.
+
 =cut
 
 has 'subinclude_plugin' => (
     is => 'rw',
-    isa => 'ClassName'
-); 
+    isa => Str,
+);
 
 around 'new' => sub {
     my $next = shift;
@@ -94,27 +120,60 @@ around 'new' => sub {
     my $self = $class->$next( @_ );
     
     my $subinclude_plugin = $self->config->{subinclude_plugin} || 'SubRequest';
-    my $subinclude_class  = __PACKAGE__ . '::' . $subinclude_plugin;
-    
-    eval "require $subinclude_class";
-    croak "Error requiring $subinclude_class: $@" if $@;
-
-    $self->subinclude_plugin( $subinclude_class );
+    $self->set_subinclude_plugin( $subinclude_plugin );
     
     $self;
 };
 
-around 'render' => sub {
-    my $next = shift;
+before 'render' => sub {
     my ($self, $c, @args) = @_;
-    
-    $c->stash->{subinclude} = sub {
-        $self->subinclude_plugin->generate_subinclude( $c, @_ );
-    };
 
-    $self->$next( $c, @args );
+    $c->stash->{subinclude}       = $self->make_context_closure(sub { $self->_subinclude( @_ ) }, $c);
+    $c->stash->{subinclude_using} = $self->make_context_closure(sub { $self->_subinclude_using( @_ ) }, $c);
 };
 
+sub set_subinclude_plugin {
+    my ($self, $plugin) = @_;
+
+    my $subinclude_class = blessed $self->_subinclude_plugin_class_instance( $plugin );
+    $self->subinclude_plugin( $subinclude_class );
+}
+
+sub _subinclude {
+    my ($self, $c, @args) = @_;
+    $self->_subinclude_using( $c, $self->subinclude_plugin, @args );
+}
+
+sub _subinclude_using {
+    my ($self, $c, $plugin, @args) = @_;
+    $plugin = $self->_subinclude_plugin_class_instance($plugin);
+    $plugin->generate_subinclude( $c, @args );
+}
+
+has _subinclude_plugin_class_instance_cache => (
+    isa => HashRef,
+    is => 'ro',
+    default => sub { {} },
+);
+
+sub _subinclude_plugin_class_instance {
+    my ($self, $plugin) = @_;
+    
+    my $class = $plugin =~ /::/ ? $plugin : __PACKAGE__ . '::' . $plugin;
+
+    my $cache = $self->_subinclude_plugin_class_instance_cache;
+    return $cache->{$plugin} if exists $cache->{$plugin};
+
+    my $plugin_config = Catalyst::Utils::merge_hashes(
+        $self->config->{subinclude}->{ALL}||{},
+        $self->config->{subinclude}->{$plugin}||{}
+    );
+    
+    Class::MOP::load_class($class);
+
+    return $cache->{$plugin} = $class->new($plugin_config);
+}
+
 =head1 SEE ALSO
 
 L<Catalyst::Plugin::SubRequest|Catalyst::Plugin::SubRequest>,