@@ -, +, @@ --- Koha/Plugins.pm | 35 +++++++++++++++++++++++++++ t/db_dependent/Koha/Plugins/Plugins.t | 32 +++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) --- a/Koha/Plugins.pm +++ a/Koha/Plugins.pm @@ -82,6 +82,41 @@ sub call { return @responses; } +=head2 call_recursive + +Calls a plugin method for all enabled plugins, +passing the return value from the previous plugin +to the next one. + + @response = Koha::Plugins->call_recursive($method, @args) + +=cut + +sub call_recursive { + my ( $class, $method, @args ) = @_; + + my @responses; + if ( C4::Context->config('enable_plugins') ) { + + my @plugins = $class->new( { enable_plugins => 1 } )->GetPlugins( { method => $method } ); + @plugins = grep { $_->can($method) } @plugins; + + foreach my $plugin (@plugins) { + my @response = eval { $plugin->$method(@args) }; + + if ($@) { + warn sprintf( "Plugin error (%s): %s", $plugin->get_metadata->{name}, $@ ); + next; + } else { + @args = @response; + } + } + + } + + return @args; +} + =head2 GetPlugins This will return a list of all available plugins, optionally limited by --- a/t/db_dependent/Koha/Plugins/Plugins.t +++ a/t/db_dependent/Koha/Plugins/Plugins.t @@ -25,7 +25,7 @@ use File::Temp qw( tempdir tempfile ); use FindBin qw($Bin); use Module::Load::Conditional qw(can_load); use Test::MockModule; -use Test::More tests => 53; +use Test::More tests => 54; use C4::Context; use Koha::Database; @@ -78,6 +78,36 @@ subtest 'call() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'call_recursive() tests' => sub { + plan tests => 6; + + $schema->storage->txn_begin; + # Temporarily remove any installed plugins data + Koha::Plugins::Methods->delete; + + t::lib::Mocks::mock_config('enable_plugins', 1); + my $plugins = Koha::Plugins->new({ enable_plugins => 1 }); + my @plugins = $plugins->InstallPlugins; + foreach my $plugin (@plugins) { + $plugin->enable(); + } + + my (@responses) = Koha::Plugins->call_recursive('item_barcode_transform', 1); + is( scalar @responses, 1, "Got back one element" ); + is( $responses[0], 4, "Got expected response" ); + + (@responses) = Koha::Plugins->call_recursive('item_barcode_transform', 'abcd'); + is( scalar @responses, 1, "Got back one element" ); + is( $responses[0], 'abcd', "Got expected response" ); + + t::lib::Mocks::mock_config('enable_plugins', 0); + (@responses) = Koha::Plugins->call_recursive('item_barcode_transform', 1); + is( scalar @responses, 1, "Got back one element" ); + is( $responses[0], 1, "call_recursive should return the original arguments if plugins are disabled" ); + + $schema->storage->txn_rollback; +}; + subtest 'GetPlugins() tests' => sub { plan tests => 2; --