File Coverage

blib/lib/Auth/Kokolores/Plugins.pm
Criterion Covered Total %
statement 3 44 6.8
branch 0 10 0.0
condition n/a
subroutine 1 6 16.6
pod 0 5 0.0
total 4 65 6.1


line stmt bran cond sub pod time code
1             package Auth::Kokolores::Plugins;
2              
3 1     1   4 use Moose;
  1         2  
  1         5  
4              
5             our $VERSION = '1.00'; # VERSION
6             # ABSTRACT: class for handling kokolores plugins
7              
8             has 'plugins' => (
9             is => 'ro',
10             isa => 'ArrayRef[Auth::Kokolores::Plugin]',
11             default => sub { [] },
12             traits => [ 'Array' ],
13             handles => {
14             'add_plugin' => 'push',
15             'all_plugins' => 'elements',
16             'num_plugins' => 'count',
17             }
18             );
19              
20             has 'plugin_prefix' => (
21             is => 'ro', isa => 'Str', default => 'Auth::Kokolores::Plugin::',
22             );
23              
24             sub init {
25 0     0 0   my $self = shift;
26 0           foreach my $p ( $self->all_plugins ) {
27 0           $p->init(@_);
28             }
29 0           return;
30             }
31              
32             sub child_init {
33 0     0 0   my $self = shift;
34 0           foreach my $p ( $self->all_plugins ) {
35 0           $p->child_init(@_);
36             }
37 0           return;
38             }
39              
40             sub shutdown {
41 0     0 0   my $self = shift;
42 0           foreach my $p ( $self->all_plugins ) {
43 0           $p->shutdown(@_);
44             }
45 0           return;
46             }
47              
48             sub load_plugin {
49 0     0 0   my ( $self, $plugin_name, $params ) = @_;
50 0 0         if( ! defined $params->{'module'} ) {
51 0           die('no module defined for plugin '.$plugin_name.'!');
52             }
53 0           my $module = $params->{'module'};
54 0           my $plugin_class = $self->plugin_prefix.$module;
55 0           my $plugin;
56              
57 0           my $code = "require ".$plugin_class.";";
58 0           eval $code; ## no critic (ProhibitStringyEval)
59 0 0         if($@) {
60 0           die('could not load module '.$module.' for plugin '.$plugin_name.': '.$@);
61             }
62              
63 0           eval {
64 0           $plugin = $plugin_class->new(
65             name => $plugin_name,
66             %$params,
67             );
68 0           $plugin->init();
69             };
70 0 0         if($@) {
71 0           die('could not initialize plugin '.$plugin_name.': '.$@);
72             }
73 0           $self->add_plugin($plugin);
74              
75 0           return;
76             }
77              
78             sub new_from_config {
79 0     0 0   my ( $class, $server, $config ) = @_;
80              
81 0           my $self = $class->new();
82              
83 0 0         if( ! defined $config ) {
84 0           return( $self );
85             }
86 0 0         if( ref($config) ne 'HASH' ) {
87 0           die('config must be an hashref!');
88             }
89              
90 0           foreach my $plugin_name ( keys %{$config} ) {
  0            
91             $self->load_plugin(
92             $plugin_name, {
93             server => $server,
94 0           %{$config->{$plugin_name}},
  0            
95             },
96             );
97             }
98 0           $server->log(1, 'loaded '.$self->num_plugins.' plugins...');
99              
100 0           return $self;
101             }
102              
103             __PACKAGE__->meta->make_immutable;
104              
105             1;
106              
107             __END__
108              
109             =pod
110              
111             =encoding UTF-8
112              
113             =head1 NAME
114              
115             Auth::Kokolores::Plugins - class for handling kokolores plugins
116              
117             =head1 VERSION
118              
119             version 1.00
120              
121             =head1 AUTHOR
122              
123             Markus Benning <ich@markusbenning.de>
124              
125             =head1 COPYRIGHT AND LICENSE
126              
127             This software is Copyright (c) 2016 by Markus Benning <ich@markusbenning.de>.
128              
129             This is free software, licensed under:
130              
131             The GNU General Public License, Version 2, June 1991
132              
133             =cut