Bug 22907: Add missing use statement
[koha.git] / Koha / Plugins.pm
blobbe6bf3304d46ff8cdc1779bba223fe25a27d2683
1 package Koha::Plugins;
3 # Copyright 2012 Kyle Hall
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
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 );
26 use C4::Context;
27 use C4::Output;
29 BEGIN {
30 my $pluginsdir = C4::Context->config("pluginsdir");
31 my @pluginsdir = ref($pluginsdir) eq 'ARRAY' ? @$pluginsdir : $pluginsdir;
32 push( @INC, @pluginsdir );
33 pop @INC if $INC[-1] eq '.';
36 =head1 NAME
38 Koha::Plugins - Module for loading and managing plugins.
40 =cut
42 sub new {
43 my ( $class, $args ) = @_;
45 return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
47 $args->{'pluginsdir'} = C4::Context->config("pluginsdir");
49 return bless( $args, $class );
52 =head2 GetPlugins
54 This will return a list of all available plugins, optionally limited by
55 method or metadata value.
57 my @plugins = Koha::Plugins::GetPlugins({
58 method => 'some_method',
59 metadata => { some_key => 'some_value' },
60 });
62 The method and metadata parameters are optional.
63 Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'.
64 If you pass multiple keys in the metadata hash, all keys must match.
66 =cut
68 sub GetPlugins {
69 my ( $self, $params ) = @_;
70 my $method = $params->{method};
71 my $req_metadata = $params->{metadata} // {};
73 my @plugin_classes = $self->plugins();
74 my @plugins;
76 foreach my $plugin_class (@plugin_classes) {
77 if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
78 next unless $plugin_class->isa('Koha::Plugins::Base');
80 my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
82 my $plugin_enabled = $plugin->retrieve_data('__ENABLED__');
83 $plugin->{enabled} = $plugin_enabled;
85 # Want all plugins. Not only enabled ones.
86 if ( defined($params->{all}) && $params->{all} ) {
87 $plugin_enabled = 1;
90 next unless $plugin_enabled;
92 # Limit results by method or metadata
93 next if $method && !$plugin->can($method);
94 my $plugin_metadata = $plugin->get_metadata;
95 next if $plugin_metadata
96 and %$req_metadata
97 and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
98 push @plugins, $plugin;
99 } else {
100 my $error = $Module::Load::Conditional::ERROR;
101 # Do not warn the error if the plugin has been uninstalled
102 warn $error unless $error =~ m|^Could not find or check module '$plugin_class'|;
105 return @plugins;
109 __END__
111 =head1 AUTHOR
113 Kyle M Hall <kyle.m.hall@gmail.com>
115 =cut