Bug 26434: Fix plugin dirs addition to @INC
[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 Array::Utils qw(array_minus);
23 use Module::Load::Conditional qw(can_load);
24 use Module::Pluggable search_path => ['Koha::Plugin'], except => qr/::Edifact(|::Line|::Message|::Order|::Segment|::Transport)$/;
25 use List::MoreUtils qw( any );
26
27 use C4::Context;
28 use C4::Output;
29
30 BEGIN {
31     my $pluginsdir = C4::Context->config("pluginsdir");
32     my @pluginsdir = ref($pluginsdir) eq 'ARRAY' ? @$pluginsdir : $pluginsdir;
33     push @INC, array_minus(@pluginsdir, @INC) ;
34     pop @INC if $INC[-1] eq '.';
35 }
36
37 =head1 NAME
38
39 Koha::Plugins - Module for loading and managing plugins.
40
41 =cut
42
43 sub new {
44     my ( $class, $args ) = @_;
45
46     return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
47
48     $args->{'pluginsdir'} = C4::Context->config("pluginsdir");
49
50     return bless( $args, $class );
51 }
52
53 =head2 GetPlugins
54
55 This will return a list of all available plugins, optionally limited by
56 method or metadata value.
57
58     my @plugins = Koha::Plugins::GetPlugins({
59         method => 'some_method',
60         metadata => { some_key => 'some_value' },
61     });
62
63 The method and metadata parameters are optional.
64 Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'.
65 If you pass multiple keys in the metadata hash, all keys must match.
66
67 =cut
68
69 sub GetPlugins {
70     my ( $self, $params ) = @_;
71     my $method = $params->{method};
72     my $req_metadata = $params->{metadata} // {};
73
74     my @plugin_classes = $self->plugins();
75     my @plugins;
76
77     foreach my $plugin_class (@plugin_classes) {
78         if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
79             next unless $plugin_class->isa('Koha::Plugins::Base');
80
81             my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
82
83             my $plugin_enabled = $plugin->retrieve_data('__ENABLED__');
84             $plugin->{enabled} = $plugin_enabled;
85
86             # Want all plugins. Not only enabled ones.
87             if ( defined($params->{all}) && $params->{all} ) {
88                 $plugin_enabled = 1;
89             }
90
91             next unless $plugin_enabled;
92
93             # Limit results by method or metadata
94             next if $method && !$plugin->can($method);
95             my $plugin_metadata = $plugin->get_metadata;
96             next if $plugin_metadata
97                 and %$req_metadata
98                 and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
99             push @plugins, $plugin;
100         } else {
101             my $error = $Module::Load::Conditional::ERROR;
102             # Do not warn the error if the plugin has been uninstalled
103             warn $error unless $error =~ m|^Could not find or check module '$plugin_class'|;
104         }
105     }
106     return @plugins;
107 }
108
109 1;
110 __END__
111
112 =head1 AUTHOR
113
114 Kyle M Hall <kyle.m.hall@gmail.com>
115
116 =cut