Bug 26115: Remove leftover Carp::Always
[koha.git] / Koha / Plugins.pm
blob8d6957a29818370fe68fde144c04e178b15990e3
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 Class::Inspector;
23 use List::MoreUtils qw(any);
24 use Module::Load::Conditional qw(can_load);
25 use Module::Load qw(load);
26 use Module::Pluggable search_path => ['Koha::Plugin'], except => qr/::Edifact(|::Line|::Message|::Order|::Segment|::Transport)$/;
28 use C4::Context;
29 use C4::Output;
30 use Koha::Plugins::Methods;
32 BEGIN {
33 my $pluginsdir = C4::Context->config("pluginsdir");
34 my @pluginsdir = ref($pluginsdir) eq 'ARRAY' ? @$pluginsdir : $pluginsdir;
35 push( @INC, @pluginsdir );
36 pop @INC if $INC[-1] eq '.';
39 =head1 NAME
41 Koha::Plugins - Module for loading and managing plugins.
43 =cut
45 sub new {
46 my ( $class, $args ) = @_;
48 return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
50 $args->{'pluginsdir'} = C4::Context->config("pluginsdir");
52 return bless( $args, $class );
55 =head2 call
57 Calls a plugin method for all enabled plugins
59 @responses = Koha::Plugins->call($method, @args)
61 =cut
63 sub call {
64 my ($class, $method, @args) = @_;
66 my @responses;
67 if (C4::Context->config('enable_plugins')) {
68 my @plugins = $class->new({ enable_plugins => 1 })->GetPlugins({ method => $method });
69 @plugins = grep { $_->can($method) } @plugins;
70 foreach my $plugin (@plugins) {
71 my $response = eval { $plugin->$method(@args) };
72 if ($@) {
73 warn sprintf("Plugin error (%s): %s", $plugin->get_metadata->{name}, $@);
74 next;
77 push @responses, $response;
81 return @responses;
84 =head2 GetPlugins
86 This will return a list of all available plugins, optionally limited by
87 method or metadata value.
89 my @plugins = Koha::Plugins::GetPlugins({
90 method => 'some_method',
91 metadata => { some_key => 'some_value' },
92 });
94 The method and metadata parameters are optional.
95 Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'.
96 If you pass multiple keys in the metadata hash, all keys must match.
98 =cut
100 sub GetPlugins {
101 my ( $self, $params ) = @_;
103 my $method = $params->{method};
104 my $req_metadata = $params->{metadata} // {};
106 my $filter = ( $method ) ? { plugin_method => $method } : undef;
108 my $plugin_classes = Koha::Plugins::Methods->search(
109 $filter,
110 { columns => 'plugin_class',
111 distinct => 1
113 )->_resultset->get_column('plugin_class');
115 my @plugins;
117 # Loop through all plugins that implement at least a method
118 while ( my $plugin_class = $plugin_classes->next ) {
120 load $plugin_class;
121 my $plugin = $plugin_class->new({
122 enable_plugins => $self->{'enable_plugins'}
123 # loads even if plugins are disabled
124 # FIXME: is this for testing without bothering to mock config?
127 next unless $plugin->is_enabled or
128 defined($params->{all}) && $params->{all};
130 # filter the plugin out by metadata
131 my $plugin_metadata = $plugin->get_metadata;
132 next
133 if $plugin_metadata
134 and %$req_metadata
135 and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
137 push @plugins, $plugin;
141 return @plugins;
144 =head2 InstallPlugins
146 Koha::Plugins::InstallPlugins()
148 This method iterates through all plugins physically present on a system.
149 For each plugin module found, it will test that the plugin can be loaded,
150 and if it can, will store its available methods in the plugin_methods table.
152 NOTE: We re-load all plugins here as a protective measure in case someone
153 has removed a plugin directly from the system without using the UI
155 =cut
157 sub InstallPlugins {
158 my ( $self, $params ) = @_;
160 my @plugin_classes = $self->plugins();
161 my @plugins;
163 foreach my $plugin_class (@plugin_classes) {
164 if ( can_load( modules => { $plugin_class => undef }, nocache => 1 ) ) {
165 next unless $plugin_class->isa('Koha::Plugins::Base');
167 my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
169 Koha::Plugins::Methods->search({ plugin_class => $plugin_class })->delete();
171 foreach my $method ( @{ Class::Inspector->methods( $plugin_class, 'public' ) } ) {
172 Koha::Plugins::Method->new(
174 plugin_class => $plugin_class,
175 plugin_method => $method,
177 )->store();
180 push @plugins, $plugin;
181 } else {
182 my $error = $Module::Load::Conditional::ERROR;
183 # Do not warn the error if the plugin has been uninstalled
184 warn $error unless $error =~ m|^Could not find or check module '$plugin_class'|;
187 return @plugins;
191 __END__
193 =head1 AVAILABLE HOOKS
195 =head2 after_hold_create
197 =head3 Parameters
199 =over
201 =item * C<$hold> - A Koha::Hold object that has just been inserted in database
203 =back
205 =head3 Return value
207 None
209 =head3 Example
211 sub after_hold_create {
212 my ($self, $hold) = @_;
214 warn "New hold for borrower " . $hold->borrower->borrowernumber;
217 =head1 AUTHOR
219 Kyle M Hall <kyle.m.hall@gmail.com>
221 =cut