From 252ccbabc8ef67158878a19551d35734222ef04d Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 15 Dec 2023 08:31:33 +0000 Subject: [PATCH] Bug 35507: Add verbose parameters in Koha::Plugins Yet another approach ;) Driemaal is scheepsrecht / Third time lucky.. Test plan: Run prove $(git grep -l Koha::Plugin | grep -P "^t\/db") Signed-off-by: Marcel de Rooy Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer (cherry picked from commit d59bf322e9eb41b38ea35bf0e1a1955177c31cb3) Signed-off-by: Fridolin Somers (cherry picked from commit 4fb9fad791f10469eeb43b8f54399c1e37c5c6a6) Signed-off-by: Lucas Gass --- Koha/Plugins.pm | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/Koha/Plugins.pm b/Koha/Plugins.pm index b72557284b..02c5c86fb5 100644 --- a/Koha/Plugins.pm +++ b/Koha/Plugins.pm @@ -104,17 +104,18 @@ sub call { Returns a list of enabled plugins. - @plugins = Koha::Plugins->get_enabled_plugins(); + @plugins = Koha::Plugins->get_enabled_plugins( [ verbose => 1 ] ); =cut sub get_enabled_plugins { - my ($class) = @_; + my ( $class, $params ) = @_; return unless C4::Context->config('enable_plugins'); my $enabled_plugins = Koha::Cache::Memory::Lite->get_from_cache(ENABLED_PLUGINS_CACHE_KEY); unless ($enabled_plugins) { + my $verbose = $params->{verbose} // $class->_verbose; $enabled_plugins = []; my @plugin_classes; @@ -127,10 +128,7 @@ sub get_enabled_plugins { }; foreach my $plugin_class (@plugin_classes) { - unless (can_load(modules => { $plugin_class => undef }, nocache => 1)) { - warn "Failed to load $plugin_class: $Module::Load::Conditional::ERROR"; - next; - } + next unless can_load( modules => { $plugin_class => undef }, verbose => $verbose, nocache => 1 ); my $plugin = eval { $plugin_class->new() }; if ($@ || !$plugin) { @@ -146,6 +144,12 @@ sub get_enabled_plugins { return @$enabled_plugins; } +sub _verbose { + my $class = shift; + # Return false when running unit tests + return exists $ENV{_} && $ENV{_} =~ /\/prove(\s|$)|\.t$/ ? 0 : 1; +} + =head2 GetPlugins This will return a list of all available plugins, optionally limited by @@ -154,6 +158,7 @@ method or metadata value. my @plugins = Koha::Plugins::GetPlugins({ method => 'some_method', metadata => { some_key => 'some_value' }, + [ verbose => 1 ], }); The method and metadata parameters are optional. @@ -166,6 +171,7 @@ sub GetPlugins { my $method = $params->{method}; my $req_metadata = $params->{metadata} // {}; + my $verbose = delete $params->{verbose} // $self->_verbose; my $filter = ( $method ) ? { plugin_method => $method } : undef; @@ -181,7 +187,7 @@ sub GetPlugins { # Loop through all plugins that implement at least a method while ( my $plugin_class = $plugin_classes->next ) { - if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) { + if ( can_load( modules => { $plugin_class => undef }, verbose => $verbose, nocache => 1 ) ) { my $plugin; my $failed_instantiation; @@ -222,7 +228,7 @@ sub GetPlugins { =head2 InstallPlugins -Koha::Plugins::InstallPlugins() +Koha::Plugins::InstallPlugins( [ verbose => 1 ] ) This method iterates through all plugins physically present on a system. For each plugin module found, it will test that the plugin can be loaded, @@ -235,12 +241,13 @@ has removed a plugin directly from the system without using the UI sub InstallPlugins { my ( $self, $params ) = @_; + my $verbose = $params->{verbose} // $self->_verbose; my @plugin_classes = $self->plugins(); my @plugins; foreach my $plugin_class (@plugin_classes) { - if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) { + if ( can_load( modules => { $plugin_class => undef }, verbose => $verbose, nocache => 1 ) ) { next unless $plugin_class->isa('Koha::Plugins::Base'); my $plugin; @@ -268,10 +275,6 @@ sub InstallPlugins { } push @plugins, $plugin; - } else { - my $error = $Module::Load::Conditional::ERROR; - # Do not warn the error if the plugin has been uninstalled - warn $error unless $error =~ m|^Could not find or check module '$plugin_class'|; } } -- 2.39.5