File Coverage

blib/lib/Lim/Plugins.pm
Criterion Covered Total %
statement 19 83 22.8
branch 0 38 0.0
condition 0 21 0.0
subroutine 7 14 50.0
pod 5 5 100.0
total 31 161 19.2


line stmt bran cond sub pod time code
1             package Lim::Plugins;
2              
3 4     4   27 use common::sense;
  4         8  
  4         33  
4 4     4   205 use Carp;
  4         10  
  4         354  
5              
6 4     4   30 use Log::Log4perl ();
  4         23  
  4         117  
7 4     4   28 use Scalar::Util qw(blessed);
  4         9  
  4         687  
8 4     4   27 use Module::Find qw(findsubmod);
  4         10  
  4         256  
9              
10 4     4   23 use Lim ();
  4         10  
  4         16576  
11              
12             =encoding utf8
13              
14             =head1 NAME
15              
16             Lim::Plugins - Lim's plugin loader and container
17              
18             =head1 VERSION
19              
20             See L for version.
21              
22             =cut
23              
24             our $VERSION = $Lim::VERSION;
25             our $INSTANCE;
26              
27             =head1 SYNOPSIS
28              
29             =over 4
30              
31             use Lim::Plugins;
32              
33             Lim::Plugins->instance->Load;
34              
35             =back
36              
37             =head1 METHODS
38              
39             =over 4
40              
41             =cut
42              
43             sub _new {
44 0     0     my $this = shift;
45 0   0       my $class = ref($this) || $this;
46 0           my %args = ( @_ );
47 0           my $self = {
48             logger => Log::Log4perl->get_logger,
49             plugin => {}
50             };
51 0           bless $self, $class;
52              
53 0           $self->Load;
54              
55 0 0         Lim::OBJ_DEBUG and $self->{logger}->debug('new ', __PACKAGE__, ' ', $self);
56 0           $self;
57             }
58              
59             sub DESTROY {
60 0     0     my ($self) = @_;
61 0 0         Lim::OBJ_DEBUG and $self->{logger}->debug('destroy ', __PACKAGE__, ' ', $self);
62            
63 0           delete $self->{plugin};
64             }
65              
66             END {
67 4     4   43 undef($INSTANCE);
68             }
69              
70             =item $instance = Lim::Plugins->instance
71              
72             Returns the singelton instance of this class.
73              
74             =cut
75              
76             sub instance {
77 0   0 0 1   $INSTANCE ||= Lim::Plugins->_new;
78             }
79              
80             =item $instance->Load
81              
82             Loads all plugins that exists on the system under Lim::Plugin::. Returns the
83             reference to itself even on error.
84              
85             =cut
86              
87             sub Load {
88 0     0 1   my ($self) = @_;
89            
90 0           foreach my $module (findsubmod Lim::Plugin) {
91 0 0 0       if (exists Lim::Config->{plugin} and exists Lim::Config->{plugin}->{load}) {
92 0 0 0       if (exists Lim::Config->{plugin}->{load}->{$module} and !Lim::Config->{plugin}->{load}->{$module}) {
93 0 0         Lim::WARN and $self->{logger}->warn('Skipping ', $module, ' configured not to load.');
94 0           next;
95             }
96            
97 0           my $name = $module;
98 0           $name =~ s/.*:://o;
99 0 0 0       if (exists Lim::Config->{plugin}->{load}->{$name} and !Lim::Config->{plugin}->{load}->{$name}) {
100 0 0         Lim::WARN and $self->{logger}->warn('Skipping ', $module, ' configured not to load.');
101 0           next;
102             }
103             }
104            
105 0 0         if (exists $self->{plugin}->{$module}) {
106 0 0         Lim::WARN and $self->{logger}->warn('Plugin ', $module, ' already loaded');
107 0           next;
108             }
109              
110 0 0         if ($module =~ /^([\w:]+)$/o) {
111 0           $module = $1;
112             }
113             else {
114 0           next;
115             }
116              
117 0           my ($name, $description);
118 0           eval {
119 0           eval "require $module;";
120 0 0         die $@ if $@;
121 0           $name = $module->Name;
122 0           $description = $module->Description;
123             };
124            
125 0 0         if ($@) {
126 0 0         Lim::WARN and $self->{logger}->warn('Unable to load plugin ', $module, ': ', $@);
127 0           $self->{plugin}->{$module} = {
128             name => $name,
129             description => $description,
130             module => $module,
131             version => -1,
132             loaded => 0,
133             error => $@
134             };
135 0           next;
136             }
137            
138 0           $self->{plugin}->{$module} = {
139             name => $name,
140             description => $description,
141             module => $module,
142             version => $module->VERSION,
143             loaded => 1
144             };
145             }
146            
147 0 0 0       if (exists Lim::Config->{plugin} and exists Lim::Config->{plugin}->{load}) {
148 0           foreach my $module (keys %{Lim::Config->{plugin}->{load}}) {
  0            
149 0 0         unless (Lim::Config->{plugin}->{load}->{$module}) {
150 0           next;
151             }
152            
153 0 0 0       unless (exists $self->{plugin}->{$module} or exists $self->{plugin}->{'Lim::Plugin::'.$module}) {
154 0 0         Lim::ERR and $self->{logger}->error('Required module ', $module, ' not found');
155             # TODO Should we die here?
156             }
157             }
158             }
159            
160 0           $self;
161             }
162              
163             =item @modules = $instance->LoadedModules
164              
165             Returns a list of loaded plugin's module name (eg Lim::Plugin::Example).
166              
167             =cut
168              
169             sub LoadedModules {
170 0     0 1   my ($self) = @_;
171 0           my @modules;
172            
173 0           foreach my $module (values %{$self->{plugin}}) {
  0            
174 0 0         if ($module->{loaded}) {
175 0           push(@modules, $module->{module});
176             }
177             }
178            
179 0           return @modules;
180             }
181              
182             =item @modules = $instance->Loaded
183              
184             Returns a list of hash references of loaded plugins.
185              
186             =over 4
187              
188             {
189             name => Short name (eg Example),
190             module => Module name (Lim::Plugin::Example),
191             version => Version (Lim::Plugin::Example->VERSION),
192             loaded => True or false if the plugin is loaded (True)
193             }
194              
195             =back
196              
197             =cut
198              
199             sub Loaded {
200 0     0 1   my ($self) = @_;
201 0           my @modules;
202            
203 0           foreach my $module (values %{$self->{plugin}}) {
  0            
204 0 0         if ($module->{loaded}) {
205 0           push(@modules, $module);
206             }
207             }
208            
209 0           return @modules;
210             }
211              
212             =item @modules = $instance->All
213              
214             Returns a list of hash references of all known plugins, check C for how
215             the hash reference looks.
216              
217             =cut
218              
219             sub All {
220 0     0 1   values %{$_[0]->{plugin}};
  0            
221             }
222              
223             =back
224              
225             =head1 AUTHOR
226              
227             Jerry Lundström, C<< >>
228              
229             =head1 BUGS
230              
231             Please report any bugs or feature requests to L.
232              
233             =head1 SUPPORT
234              
235             You can find documentation for this module with the perldoc command.
236              
237             perldoc Lim::Plugins
238              
239             You can also look for information at:
240              
241             =over 4
242              
243             =item * Lim issue tracker (report bugs here)
244              
245             L
246              
247             =back
248              
249             =head1 ACKNOWLEDGEMENTS
250              
251             =head1 LICENSE AND COPYRIGHT
252              
253             Copyright 2012-2013 Jerry Lundström.
254              
255             This program is free software; you can redistribute it and/or modify it
256             under the terms of either: the GNU General Public License as published
257             by the Free Software Foundation; or the Artistic License.
258              
259             See http://dev.perl.org/licenses/ for more information.
260              
261              
262             =cut
263              
264             1; # End of Lim::Plugins