da336adec6e2c151cd95ea65fed24c1803251ed4
[srvgit] / 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
25 use C4::Context;
26 use C4::Output;
27
28 BEGIN {
29     push @INC, C4::Context->config("pluginsdir");
30     pop @INC if $INC[-1] eq '.';
31 }
32
33 =head1 NAME
34
35 Koha::Plugins - Module for loading and managing plugins.
36
37 =cut
38
39 sub new {
40     my ( $class, $args ) = @_;
41
42     return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
43
44     $args->{'pluginsdir'} = C4::Context->config("pluginsdir");
45
46     return bless( $args, $class );
47 }
48
49 =head2 GetPlugins
50
51 This will return a list of all available plugins, optionally limited by
52 method or metadata value.
53
54     my @plugins = C4::Plugins::GetPlugins({
55         method => 'some_method',
56         metadata => { some_key => 'some_value' },
57     });
58
59 The method and metadata parameters are optional.
60 Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'.
61 If you pass multiple keys in the metadata hash, all keys must match.
62
63 =cut
64
65 sub GetPlugins {
66     my ( $self, $params ) = @_;
67     my $method = $params->{method};
68     my $req_metadata = $params->{metadata} // {};
69
70     my @plugin_classes = $self->plugins();
71     my @plugins;
72
73     foreach my $plugin_class (@plugin_classes) {
74         if ( can_load( modules => { $plugin_class => undef } ) ) {
75             next unless $plugin_class->isa('Koha::Plugins::Base');
76
77             my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
78
79             # Limit results by method or metadata
80             my $ok = 1;
81             next if $method && !$plugin->can($method);
82             my $plugin_metadata = $plugin->get_metadata;
83             foreach my $key ( keys %$req_metadata ) {
84                 if( !$plugin_metadata->{$key} ||
85                   $plugin_metadata->{$key} ne $req_metadata->{$key} ) {
86                     $ok = 0;
87                     last;
88                 }
89             }
90             push( @plugins, $plugin ) if $ok;
91         }
92     }
93     return @plugins;
94 }
95
96 1;
97 __END__
98
99 =head1 AUTHOR
100
101 Kyle M Hall <kyle.m.hall@gmail.com>
102
103 =cut