Bug 7804 - Add Koha Plugin System - Unit Tests
[koha.git] / Koha / Plugins / Handler.pm
blobd2f324a3f8f805568856849894a54714bf0febc9
1 package Koha::Plugins::Handler;
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Modern::Perl;
22 use File::Path qw(remove_tree);
24 use Module::Load::Conditional qw(can_load);
26 use C4::Context;
28 BEGIN {
29 push @INC, C4::Context->config("pluginsdir");
32 =head1 NAME
34 C4::Plugins::Handler - Handler Module for running plugins
36 =head1 SYNOPSIS
38 Koha::Plugins::Handler->run({ class => $class, method => $method, cgi => $cgi });
39 $p->run();
41 =over 2
43 =cut
45 =item run
47 Runs a plugin
49 =cut
51 sub run {
52 my ( $class, $args ) = @_;
54 die('Plugins not enabled in config') unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
56 my $plugin_class = $args->{'class'};
57 my $plugin_method = $args->{'method'};
58 my $cgi = $args->{'cgi'};
60 if ( can_load( modules => { $plugin_class => undef } ) ) {
61 my $plugin = $plugin_class->new( { cgi => $cgi } );
62 if ( $plugin->can($plugin_method) ) {
63 $plugin->$plugin_method();
64 } else {
65 warn "Plugin does not have method $plugin_method";
67 } else {
68 warn "Plugin $plugin_class cannot be loaded";
72 =item delete
74 Deletes a plugin
76 =cut
78 sub delete {
79 my ( $class, $args ) = @_;
80 my $plugin_class = $args->{'class'};
81 my $plugin_dir = C4::Context->config("pluginsdir");
82 my $plugin_path = "$plugin_dir/" . join( '/', split( '::', $args->{'class'} ) );
84 Koha::Plugins::Handler->run( { class => $plugin_class, method => 'uninstall' } );
86 C4::Context->dbh->do( "DELETE FROM plugin_data WHERE plugin_class = ?", undef, ($plugin_class) );
88 unlink("$plugin_path.pm");
89 remove_tree($plugin_path);
93 __END__
95 =back
97 =head1 AUTHOR
99 Kyle M Hall <kyle.m.hall@gmail.com>
101 =cut