From: Florian Ragwitz Date: Sun, 2 May 2010 23:16:25 +0000 (+0000) Subject: Allow parameterized roles to be applied as plugins. X-Git-Tag: 5.80023~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=62b6b631cf9141ffd9ae02e4b87c182b080b335a Allow parameterized roles to be applied as plugins. --- diff --git a/Changes b/Changes index aef1516..7ea3d84 100644 --- a/Changes +++ b/Changes @@ -20,6 +20,7 @@ HttpOnly flag - Allow the myapp_test.pl script to be given a list of paths which it will retrieve all of. (RT#53653) + - Allow parameterized roles to be applied as plugins. Documentation: - The Catalyst::Test::get method is documented as returning the raw diff --git a/Makefile.PL b/Makefile.PL index c71fa40..2590316 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -26,6 +26,7 @@ requires 'Carp'; requires 'Class::C3::Adopt::NEXT' => '0.07'; requires 'CGI::Simple::Cookie' => '1.109'; requires 'Data::Dump'; +requires 'Data::OptList'; requires 'HTML::Entities'; requires 'HTTP::Body' => '1.06'; # ->cleanup(1) requires 'HTTP::Headers' => '1.64'; diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 04a8ad4..9f2f836 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -14,6 +14,7 @@ use Catalyst::Request::Upload; use Catalyst::Response; use Catalyst::Utils; use Catalyst::Controller; +use Data::OptList; use Devel::InnerPackage (); use File::stat; use Module::Pluggable::Object (); @@ -2782,6 +2783,7 @@ the plugin name does not begin with C. my ( $proto, $plugin, $instant ) = @_; my $class = ref $proto || $proto; + # FIXME: also pass along plugin options as soon as the mop has it Class::MOP::load_class( $plugin ); $class->log->warn( "$plugin inherits from 'Catalyst::Component' - this is decated and will not work in 5.81" ) if $plugin->isa( 'Catalyst::Component' ); @@ -2797,22 +2799,30 @@ the plugin name does not begin with C. my ( $class, $plugins ) = @_; $class->_plugins( {} ) unless $class->_plugins; - $plugins ||= []; + $plugins = Data::OptList::mkopt($plugins || []); - my @plugins = Catalyst::Utils::resolve_namespace($class . '::Plugin', 'Catalyst::Plugin', @$plugins); + my @plugins = map { + [ Catalyst::Utils::resolve_namespace( + $class . '::Plugin', + 'Catalyst::Plugin', $_->[0] + ), + $_->[1], + ] + } @{ $plugins }; for my $plugin ( reverse @plugins ) { - Class::MOP::load_class($plugin); - my $meta = find_meta($plugin); + Class::MOP::load_class($plugin->[0]); + # pass along $plugin->[1] as well once cmop supports it + my $meta = find_meta($plugin->[0]); next if $meta && $meta->isa('Moose::Meta::Role'); - $class->_register_plugin($plugin); + $class->_register_plugin($plugin->[0]); } my @roles = - map { $_->name } - grep { $_ && blessed($_) && $_->isa('Moose::Meta::Role') } - map { find_meta($_) } + map { $_->[0]->name, $_->[1] } + grep { $_->[0] && blessed($_->[0]) && $_->[0]->isa('Moose::Meta::Role') } + map { [find_meta($_->[0]), $_->[1]] } @plugins; Moose::Util::apply_all_roles( diff --git a/t/aggregate/unit_core_plugin.t b/t/aggregate/unit_core_plugin.t index 11cef84..16a5e24 100644 --- a/t/aggregate/unit_core_plugin.t +++ b/t/aggregate/unit_core_plugin.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 24; +use Test::More; use lib 't/lib'; @@ -61,3 +61,4 @@ my @expected = qw( is_deeply [ TestApp->registered_plugins ], \@expected, 'registered_plugins() should only report the plugins for the current class'; +done_testing; diff --git a/t/lib/PluginTestApp.pm b/t/lib/PluginTestApp.pm index 1031586..29a02cd 100644 --- a/t/lib/PluginTestApp.pm +++ b/t/lib/PluginTestApp.pm @@ -1,10 +1,13 @@ package PluginTestApp; use Test::More; -use Catalyst qw( - Test::Plugin - +TestApp::Plugin::FullyQualified - ); +use Catalyst ( + 'Test::Plugin', + '+TestApp::Plugin::FullyQualified', + (eval { require MooseX::Role::Parameterized; 1 } + ? ('+TestApp::Plugin::ParameterizedRole' => { method_name => 'affe' }) + : ()), +); sub _test_plugins { my $c = shift; diff --git a/t/lib/PluginTestApp/Controller/Root.pm b/t/lib/PluginTestApp/Controller/Root.pm index 5358074..7bec366 100644 --- a/t/lib/PluginTestApp/Controller/Root.pm +++ b/t/lib/PluginTestApp/Controller/Root.pm @@ -34,6 +34,13 @@ sub run_time_plugins : Local { ref($c)->plugin( faux => $faux_plugin ); isa_ok $c, 'Catalyst::Plugin::Test::Plugin'; + + # applied parameterized role + if (eval { require MooseX::Role::Parameterized; 1 }) { + can_ok $c, 'affe'; + is $c->affe, 'birne', 'right method created by parameterized role'; + } + isa_ok $c, 'TestApp::Plugin::FullyQualified'; ok !$c->isa($faux_plugin), '... and it should not inherit from the instant plugin';