Lim/Plugins.pm

filter: Statementstmt 23.1% 22 / 95 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 50.0% 7 / 14 Podpod 100% 5 / 5 totaltotal 17.3% 34 / 196 CC 35 SCAR 60.6
CC
35
Cov
14%
Load
756.0
_new
12.0
LoadedModules
12.0
CRAP 428.2
« Lim/Component.pm ↑ Summary Lim/RPC/Protocol/HTTP.pm »
1 package Lim::Plugins;
2  
3 6 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Log::Log4perl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Module::Find qw(findsubmod);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
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<Lim> for version.
21  
22 =cut
23  
24 6 our $VERSION = $Lim::VERSION;
25 6 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 0 ▶sub _new {
Subroutine _new: not called CC=3 SCAR=24.8
44 0 my $this = shift;
45 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
46 0 my %args = ( @_ );
47 0 my $self = {
48 logger => Log::Log4perl->get_logger($class),
49 plugin => {}
50 };
51 0 bless $self, $class;
52 0 weaken($self->{logger});
53  
54 0 $self->Load;
55  
56 0 ▶ Lim::OBJ_DEBUG and $self->{logger}->debug('new ', __PACKAGE__, ' ', $self);
Branch: 1 branch
BranchTrueFalseTotal
Lim::OBJ_DEBUG() and $self->{"logger"}->debug("new ", __PACKAGE__, " ", $self) 0 0 0
57 0 $self;
58 }
59  
60 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
61 0 my ($self) = @_;
62 0 ▶ Lim::OBJ_DEBUG and $self->{logger}->debug('destroy ', __PACKAGE__, ' ', $self);
Branch: 1 branch
BranchTrueFalseTotal
Lim::OBJ_DEBUG() and $self->{"logger"}->debug("destroy ", __PACKAGE__, " ", $self) 0 0 0
63  
64 0 delete $self->{plugin};
65 }
66  
67 6 ▶END {
Subroutine END: called CC=1 SCAR=0.0
68 6 undef($INSTANCE);
69 }
70  
71 =item $instance = Lim::Plugins->instance
72  
73 Returns the singelton instance of this class.
74  
75 =cut
76  
77 0 ▶sub instance {
Subroutine instance: not called CC=2 SCAR=17.9
Pod documented
78 0 ▶ $INSTANCE ||= Lim::Plugins->_new;
Condition0%0/3
$Lim::Plugins::INSTANCE ||= "Lim::Plugins"->_new
l!l&&r!l&&!r
000
MC/DC0%0/2
$Lim::Plugins::INSTANCE ||= "Lim::Plugins"->_new
$Lim::Plugins::INSTANCE "Lim::Plugins"->_new
Truth table0%0/3
A ||= B
A= $Lim::Plugins::INSTANCEB= "Lim::Plugins"->_new
ABresult
1X1
011
000
79 }
80  
81 =item $instance->Load
82  
83 Loads all plugins that exists on the system under Lim::Plugin::. Returns the
84 reference to itself even on error.
85  
86 =cut
87  
88 0 ▶sub Load {
Subroutine Load: not called CC=27 SCAR=66.3
Pod documented
89 0 my ($self) = @_;
90 0 my @modules;
91  
92 0 ▶ if (Lim::Config->{plugin}->{load_all}) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Lim::Config()->{'plugin'}{'load_all'}) { } 0 0 0
93 0 @modules = findsubmod Lim::Plugin;
94 }
95 else {
96 0 foreach (findsubmod Lim::Plugin) {
97 0 my $name = $_;
98 0 $name =~ s/.*:://o;
99 0 ▶ if (exists Lim::Config->{plugin}->{load}->{$_}
Branch: 1 branch
BranchTrueFalseTotal
if (exists &Lim::Config()->{"plugin"}{"load"}{$_} or exists &Lim::Config()->{"plugin"}{"load"}{$name}) 0 0 0
Condition0%0/3
exists &Lim::Config()->{"plugin"}{"load"}{$_} or exists &Lim::Config()->{"plugin"}{"load"}{$name}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists &Lim::Config()->{"plugin"}{"load"}{$_} or exists &Lim::Config()->{"plugin"}{"load"}{$name}
exists &Lim::Config()->{"plugin"}{"load"}{$_} exists &Lim::Config()->{"plugin"}{"load"}{$name}
Truth table0%0/3
A or B
A= exists &Lim::Config()->{"plugin"}{"load"}{$_}B= exists &Lim::Config()->{"plugin"}{"load"}{$name}
ABresult
1X1
011
000
100 or exists Lim::Config->{plugin}->{load}->{$name})
101 {
102 0 push(@modules, $_);
103 }
104 }
105 }
106  
107 0 foreach my $module (@modules) {
108 0 ▶ if (exists Lim::Config->{plugin} and exists Lim::Config->{plugin}->{load}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists &Lim::Config()->{"plugin"} and exists &Lim::Config()->{"plugin"}{"load"}) 0 0 0
Condition0%0/3
exists &Lim::Config()->{"plugin"} and exists &Lim::Config()->{"plugin"}{"load"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists &Lim::Config()->{"plugin"} and exists &Lim::Config()->{"plugin"}{"load"}
exists &Lim::Config()->{"plugin"} exists &Lim::Config()->{"plugin"}{"load"}
Truth table0%0/3
A and B
A= exists &Lim::Config()->{"plugin"}B= exists &Lim::Config()->{"plugin"}{"load"}
ABresult
0X0
100
111
109 0 ▶ if (exists Lim::Config->{plugin}->{load}->{$module} and !Lim::Config->{plugin}->{load}->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists &Lim::Config()->{"plugin"}{"load"}{$module} and not &Lim::Config()->{"plugin"}{"load"}{$module}) 0 0 0
Condition0%0/3
exists &Lim::Config()->{"plugin"}{"load"}{$module} and not &Lim::Config()->{"plugin"}{"load"}{$module}
!ll&&!rl&&r
000
MC/DC0%0/2
exists &Lim::Config()->{"plugin"}{"load"}{$module} and not &Lim::Config()->{"plugin"}{"load"}{$module}
exists &Lim::Config()->{"plugin"}{"load"}{$module} not &Lim::Config()->{"plugin"}{"load"}{$module}
Truth table0%0/3
A and not(B)
A= exists &Lim::Config()->{"plugin"}{"load"}{$module}B= not &Lim::Config()->{"plugin"}{"load"}{$module}
ABresult
0X0
100
111
110 0 ▶ Lim::WARN and $self->{logger}->warn('Skipping ', $module, ' configured not to load.');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Skipping ", $module, " configured not to load.") 0 0 0
111 0 next;
112 }
113  
114 0 my $name = $module;
115 0 $name =~ s/.*:://o;
116 0 ▶ if (exists Lim::Config->{plugin}->{load}->{$name} and !Lim::Config->{plugin}->{load}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists &Lim::Config()->{"plugin"}{"load"}{$name} and not &Lim::Config()->{"plugin"}{"load"}{$name}) 0 0 0
Condition0%0/3
exists &Lim::Config()->{"plugin"}{"load"}{$name} and not &Lim::Config()->{"plugin"}{"load"}{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
exists &Lim::Config()->{"plugin"}{"load"}{$name} and not &Lim::Config()->{"plugin"}{"load"}{$name}
exists &Lim::Config()->{"plugin"}{"load"}{$name} not &Lim::Config()->{"plugin"}{"load"}{$name}
Truth table0%0/3
A and not(B)
A= exists &Lim::Config()->{"plugin"}{"load"}{$name}B= not &Lim::Config()->{"plugin"}{"load"}{$name}
ABresult
0X0
100
111
117 0 ▶ Lim::WARN and $self->{logger}->warn('Skipping ', $module, ' configured not to load.');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Skipping ", $module, " configured not to load.") 0 0 0
118 0 next;
119 }
120 }
121  
122 0 ▶ if (exists $self->{plugin}->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"plugin"}{$module}) 0 0 0
123 0 ▶ Lim::WARN and $self->{logger}->warn('Plugin ', $module, ' already loaded');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Plugin ", $module, " already loaded") 0 0 0
124 0 next;
125 }
126  
127 0 ▶ if ($module =~ /^([\w:]+)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module =~ /^([\w:]+)$/ou) { } 0 0 0
128 0 $module = $1;
129 }
130 else {
131 0 next;
132 }
133  
134 0 my ($name, $description);
135 0 eval {
136 0 eval "require $module;";
137 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
138 0 $name = $module->Name;
139 0 $description = $module->Description;
140 };
141  
142 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
143 0 ▶ Lim::WARN and $self->{logger}->warn('Unable to load plugin ', $module, ': ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Unable to load plugin ", $module, ": ", $@) 0 0 0
144 0 $self->{plugin}->{$module} = {
145 name => $name,
146 description => $description,
147 module => $module,
148 version => -1,
149 loaded => 0,
150 error => $@
151 };
152 0 next;
153 }
154  
155 0 $self->{plugin}->{$module} = {
156 name => $name,
157 description => $description,
158 module => $module,
159 version => $module->VERSION,
160 loaded => 1
161 };
162 }
163  
164 0 ▶ if (exists Lim::Config->{plugin} and exists Lim::Config->{plugin}->{load}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists &Lim::Config()->{"plugin"} and exists &Lim::Config()->{"plugin"}{"load"}) 0 0 0
Condition0%0/3
exists &Lim::Config()->{"plugin"} and exists &Lim::Config()->{"plugin"}{"load"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists &Lim::Config()->{"plugin"} and exists &Lim::Config()->{"plugin"}{"load"}
exists &Lim::Config()->{"plugin"} exists &Lim::Config()->{"plugin"}{"load"}
Truth table0%0/3
A and B
A= exists &Lim::Config()->{"plugin"}B= exists &Lim::Config()->{"plugin"}{"load"}
ABresult
0X0
100
111
165 0 foreach my $module (keys %{Lim::Config->{plugin}->{load}}) {
166 0 ▶ unless (Lim::Config->{plugin}->{load}->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Lim::Config()->{"plugin"}{"load"}{$module}) 0 0 0
167 0 next;
168 }
169  
170 0 ▶ unless (exists $self->{plugin}->{$module} or exists $self->{plugin}->{'Lim::Plugin::'.$module}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"plugin"}{$module} or exists $self->{"plugin"}{'Lim::Plugin::' . $module}) 0 0 0
Condition0%0/3
exists $self->{"plugin"}{$module} or exists $self->{"plugin"}{'Lim::Plugin::' . $module}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $self->{"plugin"}{$module} or exists $self->{"plugin"}{'Lim::Plugin::' . $module}
exists $self->{"plugin"}{$module} exists $self->{"plugin"}{'Lim::Plugin::' . $module}
Truth table0%0/3
A or B
A= exists $self->{"plugin"}{$module}B= exists $self->{"plugin"}{'Lim::Plugin::' . $module}
ABresult
1X1
011
000
171 0 ▶ Lim::ERR and $self->{logger}->error('Required module ', $module, ' not found');
Branch: 1 branch
BranchTrueFalseTotal
Lim::ERR() and $self->{"logger"}->error("Required module ", $module, " not found") 0 0 0
172 # TODO Should we die here?
173 }
174 }
175 }
176  
177 0 $self;
178 }
179  
180 =item @modules = $instance->LoadedModules
181  
182 Returns a list of loaded plugin's module name (eg Lim::Plugin::Example).
183  
184 =cut
185  
186 0 ▶sub LoadedModules {
Subroutine LoadedModules: not called CC=3 SCAR=24.8
Pod documented
187 0 my ($self) = @_;
188 0 my @modules;
189  
190 0 foreach my $module (values %{$self->{plugin}}) {
191 0 ▶ if ($module->{loaded}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module->{"loaded"}) 0 0 0
192 0 push(@modules, $module->{module});
193 }
194 }
195  
196 0 return @modules;
197 }
198  
199 =item @modules = $instance->Loaded
200  
201 Returns a list of hash references of loaded plugins.
202  
203 =over 4
204  
205 {
206 name => Short name (eg Example),
207 module => Module name (Lim::Plugin::Example),
208 version => Version (Lim::Plugin::Example->VERSION),
209 loaded => True or false if the plugin is loaded (True)
210 }
211  
212 =back
213  
214 =cut
215  
216 0 ▶sub Loaded {
Subroutine Loaded: not called CC=3 SCAR=24.8
Pod documented
217 0 my ($self) = @_;
218 0 my @modules;
219  
220 0 foreach my $module (values %{$self->{plugin}}) {
221 0 ▶ if ($module->{loaded}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module->{"loaded"}) 0 0 0
222 0 push(@modules, $module);
223 }
224 }
225  
226 0 return @modules;
227 }
228  
229 =item @modules = $instance->All
230  
231 Returns a list of hash references of all known plugins, check C<Loaded> for how
232 the hash reference looks.
233  
234 =cut
235  
236 0 ▶sub All {
Subroutine All: not called CC=1 SCAR=6.9
Pod documented
237 0 values %{$_[0]->{plugin}};
238 }
239  
240 =back
241  
242 =head1 AUTHOR
243  
244 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
245  
246 =head1 BUGS
247  
248 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
249  
250 =head1 SUPPORT
251  
252 You can find documentation for this module with the perldoc command.
253  
254 perldoc Lim::Plugins
255  
256 You can also look for information at:
257  
258 =over 4
259  
260 =item * Lim issue tracker (report bugs here)
261  
262 L<https://github.com/jelu/lim/issues>
263  
264 =back
265  
266 =head1 ACKNOWLEDGEMENTS
267  
268 =head1 LICENSE AND COPYRIGHT
269  
270 Copyright 2012-2013 Jerry Lundström.
271  
272 This program is free software; you can redistribute it and/or modify it
273 under the terms of either: the GNU General Public License as published
274 by the Free Software Foundation; or the Artistic License.
275  
276 See http://dev.perl.org/licenses/ for more information.
277  
278  
279 =cut
280  
281 6 1; # End of Lim::Plugins
« Lim/Component.pm ↑ Summary Lim/RPC/Protocol/HTTP.pm »