Bug 11175: (follow-up) Use clean_search_term
[koha-ffzg.git] / Koha / Plugins.pm
index 9dc42c5..033ba28 100644 (file)
@@ -19,20 +19,24 @@ package Koha::Plugins;
 
 use Modern::Perl;
 
+use Array::Utils qw( array_minus );
 use Class::Inspector;
-use List::MoreUtils qw(any);
-use Module::Load::Conditional qw(can_load);
-use Module::Load qw(load);
+use List::MoreUtils qw( any );
+use Module::Load::Conditional qw( can_load );
+use Module::Load;
 use Module::Pluggable search_path => ['Koha::Plugin'], except => qr/::Edifact(|::Line|::Message|::Order|::Segment|::Transport)$/;
+use Try::Tiny;
 
 use C4::Context;
 use C4::Output;
+
+use Koha::Exceptions::Plugin;
 use Koha::Plugins::Methods;
 
 BEGIN {
     my $pluginsdir = C4::Context->config("pluginsdir");
     my @pluginsdir = ref($pluginsdir) eq 'ARRAY' ? @$pluginsdir : $pluginsdir;
-    push( @INC, @pluginsdir );
+    push @INC, array_minus(@pluginsdir, @INC) ;
     pop @INC if $INC[-1] eq '.';
 }
 
@@ -40,6 +44,10 @@ BEGIN {
 
 Koha::Plugins - Module for loading and managing plugins.
 
+=head2 new
+
+Constructor
+
 =cut
 
 sub new {
@@ -52,6 +60,38 @@ sub new {
     return bless( $args, $class );
 }
 
+=head2 call
+
+Calls a plugin method for all enabled plugins
+
+    @responses = Koha::Plugins->call($method, @args)
+
+Note: Pass your arguments as refs, when you want subsequent plugins to use the value
+updated by preceding plugins, provided that these plugins support that.
+
+=cut
+
+sub call {
+    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;
+            }
+
+            push @responses, $response;
+        }
+
+    }
+    return @responses;
+}
+
 =head2 GetPlugins
 
 This will return a list of all available plugins, optionally limited by
@@ -63,7 +103,6 @@ method or metadata value.
     });
 
 The method and metadata parameters are optional.
-Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'.
 If you pass multiple keys in the metadata hash, all keys must match.
 
 =cut
@@ -88,24 +127,39 @@ sub GetPlugins {
     # Loop through all plugins that implement at least a method
     while ( my $plugin_class = $plugin_classes->next ) {
 
-        load $plugin_class;
-        my $plugin = $plugin_class->new({
-            enable_plugins => $self->{'enable_plugins'}
-                # loads even if plugins are disabled
-                # FIXME: is this for testing without bothering to mock config?
-        });
+        if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
+
+            my $plugin;
+            my $failed_instantiation;
+
+            try {
+                $plugin = $plugin_class->new({
+                    enable_plugins => $self->{'enable_plugins'}
+                        # loads even if plugins are disabled
+                        # FIXME: is this for testing without bothering to mock config?
+                });
+            }
+            catch {
+                warn "$_";
+                $failed_instantiation = 1;
+            };
 
-        next unless $plugin->is_enabled or
-                    defined($params->{all}) && $params->{all};
+            next if $failed_instantiation;
 
-        # filter the plugin out by metadata
-        my $plugin_metadata = $plugin->get_metadata;
-        next
-            if $plugin_metadata
-            and %$req_metadata
-            and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
+            next unless $plugin->is_enabled or
+                        defined($params->{all}) && $params->{all};
 
-        push @plugins, $plugin;
+            # filter the plugin out by metadata
+            my $plugin_metadata = $plugin->get_metadata;
+            next
+                if $plugin_metadata
+                and %$req_metadata
+                and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
+
+            push @plugins, $plugin;
+        } elsif ( defined($params->{errors}) && $params->{errors} ){
+            push @plugins, { error => 'cannot_load', name => $plugin_class };
+        }
 
     }
 
@@ -120,7 +174,7 @@ 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,
 and if it can, will store its available methods in the plugin_methods table.
 
-NOTE: We re-load all plugins here as a protective measure in case someone
+NOTE: We reload all plugins here as a protective measure in case someone
 has removed a plugin directly from the system without using the UI
 
 =cut
@@ -135,7 +189,18 @@ sub InstallPlugins {
         if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
             next unless $plugin_class->isa('Koha::Plugins::Base');
 
-            my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
+            my $plugin;
+            my $failed_instantiation;
+
+            try {
+                $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
+            }
+            catch {
+                warn "$_";
+                $failed_instantiation = 1;
+            };
+
+            next if $failed_instantiation;
 
             Koha::Plugins::Methods->search({ plugin_class => $plugin_class })->delete();