X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FView%2FComponent%2FSubInclude.pm;h=de3b20ac82553940a5131e42026a4fdd56628249;hb=956a83d8811378be1976e2e62a4fb8931535fefa;hp=ba2c397da3e38b2e4b2afb9f4a05ba4a02be5812;hpb=d547aa9523060fbe40d76cfd935e19c296def29e;p=catagits%2FCatalyst-View-Component-SubInclude.git diff --git a/lib/Catalyst/View/Component/SubInclude.pm b/lib/Catalyst/View/Component/SubInclude.pm index ba2c397..de3b20a 100644 --- a/lib/Catalyst/View/Component/SubInclude.pm +++ b/lib/Catalyst/View/Component/SubInclude.pm @@ -2,6 +2,9 @@ package Catalyst::View::Component::SubInclude; use Moose::Role; use Carp qw/croak/; +use Catalyst::Utils (); +use Class::MOP (); +use MooseX::Types::Moose qw/Str HashRef/; use namespace::clean -except => 'meta'; with 'Catalyst::Component::ContextClosure'; @@ -106,7 +109,7 @@ in runtime. It expects a fully qualified class name. has 'subinclude_plugin' => ( is => 'rw', - isa => 'Str' + isa => Str, ); around 'new' => sub { @@ -131,7 +134,7 @@ before 'render' => sub { sub set_subinclude_plugin { my ($self, $plugin) = @_; - my $subinclude_class = $self->_subinclude_plugin_class_name( $plugin ); + my $subinclude_class = blessed $self->_subinclude_plugin_class_instance( $plugin ); $self->subinclude_plugin( $subinclude_class ); } @@ -142,23 +145,32 @@ sub _subinclude { sub _subinclude_using { my ($self, $c, $plugin, @args) = @_; - $plugin = $self->_subinclude_plugin_class_name($plugin); + $plugin = $self->_subinclude_plugin_class_instance($plugin); $plugin->generate_subinclude( $c, @args ); } -sub _subinclude_plugin_class_name { +has _subinclude_plugin_class_instance_cache => ( + isa => HashRef, + is => 'ro', + default => sub { {} }, +); + +sub _subinclude_plugin_class_instance { my ($self, $plugin) = @_; - # check if name is already fully qualified - my $pkg = __PACKAGE__; - return $plugin if $plugin =~ /^$pkg/; + my $class = $plugin =~ /::/ ? $plugin : __PACKAGE__ . '::' . $plugin; + + my $cache = $self->_subinclude_plugin_class_instance_cache; + return $cache->{$plugin} if exists $cache->{$plugin}; - my $class_name = __PACKAGE__ . '::' . $plugin; + my $plugin_config = Catalyst::Utils::merge_hashes( + $self->config->{subinclude}->{ALL}||{}, + $self->config->{subinclude}->{$plugin}||{} + ); - eval "require $class_name"; - croak "Error requiring $class_name: $@" if $@; + Class::MOP::load_class($class); - $class_name; + return $cache->{$plugin} = $class->new($plugin_config); } =head1 SEE ALSO