Lim/Plugins.pm

filter: Statementstmt 25.5% 22 / 86 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 50.0% 7 / 14 Podpod 100% 5 / 5 totaltotal 19.1% 34 / 178 SCAR 64.3
CC
31
Cov
15%
Load
552.0
_new
12.0
LoadedModules
12.0
CRAP 617.6
« Lim/RPC/Call.pm ↑ Summary Lim.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);
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 sub _new {
44 0 ▶ my $this = shift;
Subroutine _new: not called CC=3 SCAR=24.8
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,
49 plugin => {}
50 };
51 0 bless $self, $class;
52  
53 0 $self->Load;
54  
55 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
56 0 $self;
57 }
58  
59 sub DESTROY {
60 0 ▶ my ($self) = @_;
Subroutine DESTROY: not called CC=2 SCAR=17.9
61 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
62
63 0 delete $self->{plugin};
64 }
65  
66 END {
67 6 ▶ undef($INSTANCE);
Subroutine END: called CC=1 SCAR=0.0
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 ▶ $INSTANCE ||= Lim::Plugins->_new;
Subroutine instance: not called CC=2 SCAR=17.9
Pod documented
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
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 ▶ my ($self) = @_;
Subroutine Load: not called CC=23 SCAR=63.1
Pod documented
89
90 0 foreach my $module (findsubmod Lim::Plugin) {
91 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
92 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
93 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
94 0 next;
95 }
96
97 0 my $name = $module;
98 0 $name =~ s/.*:://o;
99 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
100 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
101 0 next;
102 }
103 }
104
105 0 ▶ if (exists $self->{plugin}->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"plugin"}{$module}) 0 0 0
106 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
107 0 next;
108 }
109  
110 0 ▶ if ($module =~ /^([\w:]+)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module =~ /^([\w:]+)$/ou) { } 0 0 0
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 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
121 0 $name = $module->Name;
122 0 $description = $module->Description;
123 };
124
125 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
126 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
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 ▶ 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
148 0 foreach my $module (keys %{Lim::Config->{plugin}->{load}}) {
149 0 ▶ unless (Lim::Config->{plugin}->{load}->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Lim::Config()->{"plugin"}{"load"}{$module}) 0 0 0
150 0 next;
151 }
152
153 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
154 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
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 ▶ my ($self) = @_;
Subroutine LoadedModules: not called CC=3 SCAR=24.8
Pod documented
171 0 my @modules;
172
173 0 foreach my $module (values %{$self->{plugin}}) {
174 0 ▶ if ($module->{loaded}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module->{"loaded"}) 0 0 0
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 ▶ my ($self) = @_;
Subroutine Loaded: not called CC=3 SCAR=24.8
Pod documented
201 0 my @modules;
202
203 0 foreach my $module (values %{$self->{plugin}}) {
204 0 ▶ if ($module->{loaded}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module->{"loaded"}) 0 0 0
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<Loaded> for how
215 the hash reference looks.
216  
217 =cut
218  
219 sub All {
220 0 ▶ values %{$_[0]->{plugin}};
Subroutine All: not called CC=1 SCAR=6.9
Pod documented
221 }
222  
223 =back
224  
225 =head1 AUTHOR
226  
227 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
228  
229 =head1 BUGS
230  
231 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
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<https://github.com/jelu/lim/issues>
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 6 1; # End of Lim::Plugins
« Lim/RPC/Call.pm ↑ Summary Lim.pm »