Bug 29719: Unit tests
[koha.git] / Koha / Plugins / Handler.pm
1 package Koha::Plugins::Handler;
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 File::Path qw( remove_tree );
24
25 use Module::Load qw( load );
26
27 use C4::Context;
28 use Koha::Plugins::Methods;
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::Handler - Handler Module for running plugins
40
41 =head1 SYNOPSIS
42
43   Koha::Plugins::Handler->run({ class => $class, method => $method, cgi => $cgi });
44   $p->run();
45
46 =over 2
47
48 =cut
49
50 =item run
51
52 Runs a plugin
53
54 =cut
55
56 sub run {
57     my ( $class, $args ) = @_;
58
59     return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
60
61     my $plugin_class  = $args->{'class'};
62     my $plugin_method = $args->{'method'};
63     my $cgi           = $args->{'cgi'};
64     my $params        = $args->{'params'};
65
66     my $has_method = Koha::Plugins::Methods->search({ plugin_class => $plugin_class, plugin_method => $plugin_method })->count();
67     if ( $has_method ) {
68         load $plugin_class;
69         my $plugin = $plugin_class->new( { cgi => $cgi, enable_plugins => $args->{'enable_plugins'} } );
70         return $plugin->$plugin_method( $params );
71     } else {
72         warn "Plugin does not have method $plugin_method";
73         return;
74     }
75 }
76
77 =item delete
78
79 Deletes a plugin
80
81 =cut
82
83 sub delete {
84     my ( $class, $args ) = @_;
85
86     return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
87
88     my $plugin_class = $args->{'class'};
89
90     my $plugin_path = $plugin_class;
91     $plugin_path =~ s/::/\//g;  # Take class name, transform :: to / to get path
92     $plugin_path =~ s/$/.pm/;   # Add .pm to the end
93     require $plugin_path;   # Require the plugin to have it's path listed in INC
94     $plugin_path =
95       $INC{$plugin_path};   # Get the full true path to the plugin from INC
96     $plugin_path =~ s/.pm//;    # Remove the .pm from the end
97
98     Koha::Plugins::Handler->run({
99         class          => $plugin_class,
100         method         => 'uninstall',
101         enable_plugins => $args->{enable_plugins},
102     });
103
104     C4::Context->dbh->do( "DELETE FROM plugin_data WHERE plugin_class = ?", undef, ($plugin_class) );
105     Koha::Plugins::Methods->search({ plugin_class => $plugin_class })->delete();
106
107     unlink "$plugin_path.pm" or warn "Could not unlink $plugin_path.pm: $!";
108     remove_tree($plugin_path);
109 }
110
111 1;
112 __END__
113
114 =back
115
116 =head1 AUTHOR
117
118 Kyle M Hall <kyle.m.hall@gmail.com>
119
120 =cut