Bug 16586: Koha Plugins: Limit results of GetPlugins by metadata
[koha.git] / Koha / Plugins.pm
index a188d7c..da336ad 100644 (file)
@@ -46,18 +46,26 @@ sub new {
     return bless( $args, $class );
 }
 
-=head2 GetPlugins()
+=head2 GetPlugins
 
-This will return a list of all the available plugins of the passed type.
+This will return a list of all available plugins, optionally limited by
+method or metadata value.
 
-Usage: my @plugins = C4::Plugins::GetPlugins( $method );
+    my @plugins = C4::Plugins::GetPlugins({
+        method => 'some_method',
+        metadata => { some_key => 'some_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.
 
-At the moment, the available types are 'report', 'tool' and 'to_marc'.
 =cut
 
 sub GetPlugins {
-    my $self   = shift;
-    my $method = shift;
+    my ( $self, $params ) = @_;
+    my $method = $params->{method};
+    my $req_metadata = $params->{metadata} // {};
 
     my @plugin_classes = $self->plugins();
     my @plugins;
@@ -68,13 +76,18 @@ sub GetPlugins {
 
             my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
 
-            if ($method) {
-                if ( $plugin->can($method) ) {
-                    push( @plugins, $plugin );
+            # Limit results by method or metadata
+            my $ok = 1;
+            next if $method && !$plugin->can($method);
+            my $plugin_metadata = $plugin->get_metadata;
+            foreach my $key ( keys %$req_metadata ) {
+                if( !$plugin_metadata->{$key} ||
+                  $plugin_metadata->{$key} ne $req_metadata->{$key} ) {
+                    $ok = 0;
+                    last;
                 }
-            } else {
-                push( @plugins, $plugin );
             }
+            push( @plugins, $plugin ) if $ok;
         }
     }
     return @plugins;