Bug 14544: QA fixes - some minor bug fixes
[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'];
24
25 use C4::Context;
26 use C4::Output;
27
28 BEGIN {
29     push @INC, C4::Context->config("pluginsdir");
30 }
31
32 =head1 NAME
33
34 Koha::Plugins - Module for loading and managing plugins.
35
36 =cut
37
38 sub new {
39     my ( $class, $args ) = @_;
40
41     return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
42
43     $args->{'pluginsdir'} = C4::Context->config("pluginsdir");
44
45     return bless( $args, $class );
46 }
47
48 =head2 GetPlugins()
49
50 This will return a list of all the available plugins of the passed type.
51
52 Usage: my @plugins = C4::Plugins::GetPlugins( $method );
53
54 At the moment, the available types are 'report', 'tool' and 'to_marc'.
55 =cut
56
57 sub GetPlugins {
58     my $self   = shift;
59     my $method = shift;
60
61     my @plugin_classes = $self->plugins();
62     my @plugins;
63
64     foreach my $plugin_class (@plugin_classes) {
65         if ( can_load( modules => { $plugin_class => undef } ) ) {
66             my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
67
68             if ($method) {
69                 if ( $plugin->can($method) ) {
70                     push( @plugins, $plugin );
71                 }
72             } else {
73                 push( @plugins, $plugin );
74             }
75         }
76     }
77     return @plugins;
78 }
79
80 1;
81 __END__
82
83 =head1 AUTHOR
84
85 Kyle M Hall <kyle.m.hall@gmail.com>
86
87 =cut