Pluggable/Pipeline.pm

filter: Statementstmt 32.8% 65 / 198 Branchbran 17.0% 15 / 88 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 10 / 20 Podpod 100% 13 / 13 totaltotal 31.6% 103 / 325 CC 52 SCAR 31.9
CC
52
Cov
28%
replace
56.0
insert_before
56.0
insert_after
56.0
CRAP 24.2
« Pluggable.pm ↑ Summary Pluggable/Constants.pm »
1 package POE::Component::Pluggable::Pipeline;
2 3 $POE::Component::Pluggable::Pipeline::VERSION = '1.28';
3 #ABSTRACT: the plugin pipeline for POE::Component::Pluggable
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
11 1 my ($package, $pluggable) = @_;
12  
13 1 my $self = bless {
14 PLUGS => {},
15 PIPELINE => [],
16 HANDLES => {},
17 OBJECT => $pluggable,
18 }, $package;
19  
20 1 weaken($self->{OBJECT});
21  
22 1 return $self;
23 }
24  
25 1 ▶sub push {
Subroutine push: called CC=3 SCAR=11.7
Pod documented
26 1 my ($self, $alias, $plug) = @_;
27  
28 1 ▶ if ($self->{PLUGS}{$alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"PLUGS"}{$alias}) 0 1 1
29 0 $@ = "Plugin named '$alias' already exists ($self->{PLUGS}{$alias})";
30 0 return;
31 }
32  
33 1 my $return = $self->_register($alias, $plug);
34 1 ▶ return if !$return;
Branch: 1 branch
BranchTrueFalseTotal
unless $return 0 1 1
35  
36 1 push @{ $self->{PIPELINE} }, $plug;
37 1 return scalar @{ $self->{PIPELINE} };
38 }
39  
40 0 ▶sub pop {
Subroutine pop: not called CC=3 SCAR=24.8
Pod documented
41 0 my ($self) = @_;
42  
43 0 ▶ return if !@{ $self->{PIPELINE} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"PIPELINE"};} 0 0 0
44  
45 0 my $plug = pop @{ $self->{PIPELINE} };
46 0 my $alias = $self->{PLUGS}{$plug};
47 0 $self->_unregister($alias, $plug);
48  
49 0 ▶ return wantarray ? ($plug, $alias) : $plug;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
50 }
51  
52 0 ▶sub unshift {
Subroutine unshift: not called CC=3 SCAR=24.8
Pod documented
53 0 my ($self, $alias, $plug) = @_;
54  
55 0 ▶ if ($self->{PLUGS}{$alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"PLUGS"}{$alias}) 0 0 0
56 0 $@ = "Plugin named '$alias' already exists ($self->{PLUGS}{$alias}";
57 0 return;
58 }
59  
60 0 my $return = $self->_register($alias, $plug);
61 0 ▶ return if !$return;
Branch: 1 branch
BranchTrueFalseTotal
unless $return 0 0 0
62  
63 0 unshift @{ $self->{PIPELINE} }, $plug;
64 0 return scalar @{ $self->{PIPELINE} };
65 }
66  
67 0 ▶sub shift {
Subroutine shift: not called CC=3 SCAR=24.8
Pod documented
68 0 my ($self) = @_;
69  
70 0 ▶ return if !@{ $self->{PIPELINE} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"PIPELINE"};} 0 0 0
71  
72 0 my $plug = shift @{ $self->{PIPELINE} };
73 0 my $alias = $self->{PLUGS}{$plug};
74 0 $self->_unregister($alias, $plug);
75  
76 0 ▶ return wantarray ? ($plug, $alias) : $plug;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
77 }
78  
79 0 ▶sub replace {
Subroutine replace: not called CC=7 SCAR=40.3
Pod documented
80 0 my ($self, $old, $new_a, $new_p) = @_;
81  
82 my ($old_a, $old_p) = ref $old
83 ? ($self->{PLUGS}{$old}, $old)
84 0 ▶ : ($old, $self->{PLUGS}{$old})
Branch: 1 branch
BranchTrueFalseTotal
ref $old ? : 0 0 0
85 ;
86  
87 0 ▶ if (!$old_p) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($old_p) 0 0 0
88 0 $@ = "Plugin '$old_a' does not exist";
89 0 return;
90 }
91  
92 0 $self->_unregister($old_a, $old_p);
93  
94 0 ▶ if ($self->{PLUGS}{$new_a}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"PLUGS"}{$new_a}) 0 0 0
95 0 $@ = "Plugin named '$new_a' already exists ($self->{PLUGS}{$new_a}";
96 0 return;
97 }
98  
99 0 my $return = $self->_register($new_a, $new_p);
100 0 ▶ return if !$return;
Branch: 1 branch
BranchTrueFalseTotal
unless $return 0 0 0
101  
102 0 for my $plugin (@{ $self->{PIPELINE} }) {
103 0 ▶ if ($plugin == $old_p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin == $old_p) 0 0 0
104 0 $plugin = $new_p;
105 0 last;
106 }
107 }
108  
109 0 return 1;
110 }
111  
112 1 ▶sub remove {
Subroutine remove: called CC=6 SCAR=19.5
Pod documented
113 1 my ($self, $old) = @_;
114 my ($old_a, $old_p) = ref $old
115 ? ($self->{PLUGS}{$old}, $old)
116 1 ▶ : ($old, $self->{PLUGS}{$old})
Branch: 1 branch
BranchTrueFalseTotal
ref $old ? : 0 1 1
117 ;
118  
119 1 ▶ if (!$old_p) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($old_p) 0 1 1
120 0 $@ = "Plugin '$old_a' does not exist";
121 0 return;
122 }
123  
124 1 my $i = 0;
125 1 for my $plugin (@{ $self->{PIPELINE} }) {
126 1 ▶ if ($plugin == $old_p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin == $old_p) 1 0 1
127 1 splice(@{ $self->{PIPELINE} }, $i, 1);
128 1 last;
129 }
130 0 $i++;
131 }
132  
133 1 $self->_unregister($old_a, $old_p);
134  
135 1 ▶ return wantarray ? ($old_p, $old_a) : $old_p;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
136 }
137  
138 4 ▶sub get {
Subroutine get: called CC=4 SCAR=16.4
Pod documented
139 4 my ($self, $old) = @_;
140  
141 my ($old_a, $old_p) = ref $old
142 ? ($self->{PLUGS}{$old}, $old)
143 4 ▶ : ($old, $self->{PLUGS}{$old})
Branch: 1 branch
BranchTrueFalseTotal
ref $old ? : 4 0 4
144 ;
145  
146  
147 4 ▶ if (!$old_p) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($old_p) 0 4 4
148 0 $@ = "Plugin '$old_a' does not exist";
149 0 return;
150 }
151  
152 4 ▶ return wantarray ? ($old_p, $old_a) : $old_p;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
153 }
154  
155 0 ▶sub get_index {
Subroutine get_index: not called CC=5 SCAR=34.0
Pod documented
156 0 my ($self, $old) = @_;
157  
158 my ($old_a, $old_p) = ref $old
159 ? ($self->{PLUGS}{$old}, $old)
160 0 ▶ : ($old, $self->{PLUGS}{$old})
Branch: 1 branch
BranchTrueFalseTotal
ref $old ? : 0 0 0
161 ;
162  
163 0 ▶ if (!$old_p) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($old_p) 0 0 0
164 0 $@ = "Plugin '$old_a' does not exist";
165 0 return -1;
166 }
167  
168 0 my $i = 0;
169 0 for my $plugin (@{ $self->{PIPELINE} }) {
170 0 ▶ return $i if $plugin == $old_p;
Branch: 1 branch
BranchTrueFalseTotal
if $plugin == $old_p 0 0 0
171 0 $i++;
172 }
173  
174 0 return -1;
175 }
176  
177 0 ▶sub insert_before {
Subroutine insert_before: not called CC=7 SCAR=40.3
Pod documented
178 0 my ($self, $old, $new_a, $new_p) = @_;
179  
180 my ($old_a, $old_p) = ref $old
181 ? ($self->{PLUGS}{$old}, $old)
182 0 ▶ : ($old, $self->{PLUGS}{$old})
Branch: 1 branch
BranchTrueFalseTotal
ref $old ? : 0 0 0
183 ;
184  
185 0 ▶ if (!$old_p) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($old_p) 0 0 0
186 0 $@ = "Plugin '$old_a' does not exist";
187 0 return;
188 }
189  
190 0 ▶ if ($self->{PLUGS}{$new_a}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"PLUGS"}{$new_a}) 0 0 0
191 0 $@ = "Plugin named '$new_a' already exists ($self->{PLUGS}{$new_a}";
192 0 return;
193 }
194  
195 0 my $return = $self->_register($new_a, $new_p);
196 0 ▶ return if !$return;
Branch: 1 branch
BranchTrueFalseTotal
unless $return 0 0 0
197  
198 0 my $i = 0;
199 0 for my $plugin (@{ $self->{PIPELINE} }) {
200 0 ▶ if ($plugin == $old_p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin == $old_p) 0 0 0
201 0 splice(@{ $self->{PIPELINE} }, $i, 0, $new_p);
202 0 last;
203 }
204 0 $i++;
205 }
206  
207 0 return 1;
208 }
209  
210 0 ▶sub insert_after {
Subroutine insert_after: not called CC=7 SCAR=40.3
Pod documented
211 0 my ($self, $old, $new_a, $new_p) = @_;
212 my ($old_a, $old_p) = ref $old
213 ? ($self->{PLUGS}{$old}, $old)
214 0 ▶ : ($old, $self->{PLUGS}{$old})
Branch: 1 branch
BranchTrueFalseTotal
ref $old ? : 0 0 0
215 ;
216  
217 0 ▶ if (!$old_p) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($old_p) 0 0 0
218 0 $@ = "Plugin '$old_a' does not exist";
219 0 return;
220 }
221  
222 0 ▶ if ($self->{PLUGS}{$new_a}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"PLUGS"}{$new_a}) 0 0 0
223 0 $@ = "Plugin named '$new_a' already exists ($self->{PLUGS}{$new_a}";
224 0 return;
225 }
226  
227 0 my $return = $self->_register($new_a, $new_p);
228 0 ▶ return if !$return;
Branch: 1 branch
BranchTrueFalseTotal
unless $return 0 0 0
229  
230 0 my $i = 0;
231 0 for my $plugin (@{ $self->{PIPELINE} }) {
232 0 ▶ if ($plugin == $old_p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin == $old_p) 0 0 0
233 0 splice(@{ $self->{PIPELINE} }, $i+1, 0, $new_p);
234 0 last;
235 }
236 0 $i++;
237 }
238  
239 0 return 1;
240 }
241  
242 0 ▶sub bump_up {
Subroutine bump_up: not called CC=4 SCAR=30.0
Pod documented
243 0 my ($self, $old, $diff) = @_;
244 0 my $idx = $self->get_index($old);
245  
246 0 ▶ return -1 if $idx < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $idx < 0 0 0 0
247  
248 0 my $pipeline = $self->{PIPELINE};
249 0 ▶ $diff ||= 1;
Condition0%0/2
$diff ||= 1
l!l
00
MC/DC0%0/1
$diff ||= 1
$diff
Truth table0%0/2
A
A= $diff
Aresult
11
00
250  
251 0 my $pos = $idx - $diff;
252  
253 0 ▶ if ($pos < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0) 0 0 0
254 0 carp "$idx - $diff is negative, moving to head of the pipeline";
255 }
256  
257 0 splice(@$pipeline, $pos, 0, splice(@$pipeline, $idx, 1));
258 0 return $pos;
259 }
260  
261 0 ▶sub bump_down {
Subroutine bump_down: not called CC=4 SCAR=30.0
Pod documented
262 0 my ($self, $old, $diff) = @_;
263 0 my $idx = $self->get_index($old);
264  
265 0 ▶ return -1 if $idx < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $idx < 0 0 0 0
266  
267 0 my $pipeline = $self->{PIPELINE};
268 0 ▶ $diff ||= 1;
Condition0%0/2
$diff ||= 1
l!l
00
MC/DC0%0/1
$diff ||= 1
$diff
Truth table0%0/2
A
A= $diff
Aresult
11
00
269  
270 0 my $pos = $idx + $diff;
271  
272 0 ▶ if ($pos >= @$pipeline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= @$pipeline) 0 0 0
273 0 carp "$idx + $diff is too high, moving to back of the pipeline";
274 }
275  
276 0 splice(@$pipeline, $pos, 0, splice(@$pipeline, $idx, 1));
277 0 return $pos;
278 }
279  
280 1 ▶sub _register {
Subroutine _register: called CC=4 SCAR=15.6
281 1 my ($self, $alias, $plug) = @_;
282 1 ▶ return if !defined $self->{OBJECT};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"OBJECT"} 0 1 1
283  
284 1 my $return;
285 1 my $sub = "$self->{OBJECT}{_pluggable_reg_prefix}register";
286 1 eval { $return = $plug->$sub($self->{OBJECT}) };
287  
288 1 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 1 1
elsif (not $return) { } 0 1 1
289 0 chomp $@;
290 0 my $error = "$sub call on plugin '$alias' failed: $@";
291 0 $self->_handle_error($error, $plug, $alias);
292 }
293 elsif (!$return) {
294 0 my $error = "$sub call on plugin '$alias' did not return a true value";
295 0 $self->_handle_error($error, $plug, $alias);
296 }
297  
298 1 $self->{PLUGS}{$plug} = $alias;
299 1 $self->{PLUGS}{$alias} = $plug;
300  
301 $self->{OBJECT}->_pluggable_event(
302 1 "$self->{OBJECT}{_pluggable_prefix}plugin_add",
303 $alias, $plug,
304 );
305  
306 1 return $return;
307 }
308  
309 1 ▶sub _unregister {
Subroutine _unregister: called CC=4 SCAR=15.4
310 1 my ($self, $alias, $plug) = @_;
311 1 ▶ return if !defined $self->{OBJECT};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"OBJECT"} 0 1 1
312  
313 1 my $return;
314 1 my $sub = "$self->{OBJECT}{_pluggable_reg_prefix}unregister";
315 1 eval { $return = $plug->$sub($self->{OBJECT}) };
316  
317 1 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 1 1
elsif (not $return) { } 0 1 1
318 0 chomp $@;
319 0 my $error = "$sub call on plugin '$alias' failed: $@";
320 0 $self->_handle_error($error, $plug, $alias);
321 }
322 elsif (!$return) {
323 0 my $error = "$sub call on plugin '$alias' did not return a true value";
324 0 $self->_handle_error($error, $plug, $alias);
325 }
326  
327 1 delete $self->{PLUGS}{$plug};
328 1 delete $self->{PLUGS}{$alias};
329 1 delete $self->{HANDLES}{$plug};
330  
331 $self->{OBJECT}->_pluggable_event(
332 1 "$self->{OBJECT}{_pluggable_prefix}plugin_del",
333 $alias, $plug,
334 );
335  
336 1 return $return;
337 }
338  
339 0 ▶sub _handle_error {
Subroutine _handle_error: not called CC=2 SCAR=17.9
340 0 my ($self, $error, $plugin, $alias) = @_;
341  
342 0 ▶ warn "$error\n" if $self->{OBJECT}{_pluggable_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"OBJECT"}{"_pluggable_debug"} 0 0 0
343 $self->{OBJECT}->_pluggable_event(
344 0 "$self->{OBJECT}{_pluggable_prefix}plugin_error",
345 $error, $plugin, $alias,
346 );
347  
348 0 return;
349 }
350  
351 3 qq[Pipey McPipeline];
352  
353 __END__
« Pluggable.pm ↑ Summary Pluggable/Constants.pm »