Bug 16586: [QA Follow-up] Simplify code
[koha.git] / Koha / Plugins.pm
1 package Koha::Plugins;
2
3 # Copyright 2012 Kyle Hall
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Module::Load::Conditional qw(can_load);
23 use Module::Pluggable search_path => ['Koha::Plugin'], except => qr/::Edifact(|::Line|::Message|::Order|::Segment|::Transport)$/;
24 use List::MoreUtils qw( any );
25
26 use C4::Context;
27 use C4::Output;
28
29 BEGIN {
30     push @INC, C4::Context->config("pluginsdir");
31     pop @INC if $INC[-1] eq '.';
32 }
33
34 =head1 NAME
35
36 Koha::Plugins - Module for loading and managing plugins.
37
38 =cut
39
40 sub new {
41     my ( $class, $args ) = @_;
42
43     return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
44
45     $args->{'pluginsdir'} = C4::Context->config("pluginsdir");
46
47     return bless( $args, $class );
48 }
49
50 =head2 GetPlugins
51
52 This will return a list of all available plugins, optionally limited by
53 method or metadata value.
54
55     my @plugins = C4::Plugins::GetPlugins({
56         method => 'some_method',
57         metadata => { some_key => 'some_value' },
58     });
59
60 The method and metadata parameters are optional.
61 Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'.
62 If you pass multiple keys in the metadata hash, all keys must match.
63
64 =cut
65
66 sub GetPlugins {
67     my ( $self, $params ) = @_;
68     my $method = $params->{method};
69     my $req_metadata = $params->{metadata} // {};
70
71     my @plugin_classes = $self->plugins();
72     my @plugins;
73
74     foreach my $plugin_class (@plugin_classes) {
75         if ( can_load( modules => { $plugin_class => undef } ) ) {
76             next unless $plugin_class->isa('Koha::Plugins::Base');
77
78             my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
79
80             # Limit results by method or metadata
81             next if $method && !$plugin->can($method);
82             my $plugin_metadata = $plugin->get_metadata;
83             next if $plugin_metadata
84                 and %$req_metadata
85                 and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
86             push @plugins, $plugin;
87         }
88     }
89     return @plugins;
90 }
91
92 1;
93 __END__
94
95 =head1 AUTHOR
96
97 Kyle M Hall <kyle.m.hall@gmail.com>
98
99 =cut