Board.pm

filter: Statementstmt 100% 132 / 132 Branchbran 96.7% 60 / 62 Conditioncond 91.6% 11 / 12 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 27 / 27 Podpod 100% 11 / 11 totaltotal 98.0% 247 / 252 CC 36 SCAR 19.6
CC
36
Cov
99%
container
14.0
service
13.0
typemap
5.0
CRAP 7.1
« Board/Traversable.pm ↑ Summary Board/Container.pm »
1 package Bread::Board;
2 54 our $AUTHORITY = 'cpan:STEVAN';
3 54 $Bread::Board::VERSION = '0.37';
4 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 54 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 54 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 # ABSTRACT: A solderless way to wire up your application components
9  
10 54 ▶use Bread::Board::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 54 ▶use Bread::Board::ConstructorInjection;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 54 ▶use Bread::Board::SetterInjection;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 54 ▶use Bread::Board::BlockInjection;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 54 ▶use Bread::Board::Literal;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 54 ▶use Bread::Board::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 54 ▶use Bread::Board::Container::Parameterized;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 54 ▶use Bread::Board::Dependency;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 54 ▶use Bread::Board::LifeCycle::Singleton;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 54 ▶use Bread::Board::Service::Inferred;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 54 ▶use Bread::Board::Service::Alias;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 54 ▶use Moose::Exporter 2.1200;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 54 Moose::Exporter->setup_import_methods(
24 as_is => [qw[
25 as
26 container
27 depends_on
28 service
29 alias
30 wire_names
31 include
32 typemap
33 infer
34 literal
35 ]],
36 );
37  
38 116 ▶sub as (&) { $_[0] }
Subroutine as: called CC=1 SCAR=0.0
Pod documented
39  
40 54 our $CC;
41 54 our $in_container;
42  
43 5 ▶sub set_root_container {
Subroutine set_root_container: called CC=2 SCAR=6.9
Pod documented
44 5 ▶ confess "Can't set the root container when we're already in a container"
Branch: 1 branch
BranchTrueFalseTotal
if $Bread::Board::in_container 1 4 5
45 if $in_container;
46 4 $CC = shift;
47 }
48  
49 124 ▶sub container ($;$$) {
Subroutine container: called CC=14 SCAR=26.4
Pod documented
50 124 my $name = shift;
51  
52 124 my $c;
53 124 ▶ if (blessed $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($name)) { } 15 109 124
54 15 ▶ confess 'an object used as a container must inherit from Bread::Board::Container or Bread::Board::Container::Parameterized'
Branch: 1 branch
BranchTrueFalseTotal
unless $name->isa("Bread::Board::Container") or $name->isa("Bread::Board::Container::Parameterized") 1 14 15
Condition100%3/3
$name->isa("Bread::Board::Container") or $name->isa("Bread::Board::Container::Parameterized")
l!l&&r!l&&!r
1221
MC/DC100%2/2
$name->isa("Bread::Board::Container") or $name->isa("Bread::Board::Container::Parameterized")
$name->isa("Bread::Board::Container") $name->isa("Bread::Board::Container::Parameterized")
Truth table100%3/3
A or B
A= $name->isa("Bread::Board::Container")B= $name->isa("Bread::Board::Container::Parameterized")
ABresult
1X1
011
000
55 unless $name->isa('Bread::Board::Container') || $name->isa('Bread::Board::Container::Parameterized');
56  
57 14 ▶ confess 'container($object, ...) is not supported for parameterized containers'
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 1 1 13 14
58 if scalar @_ > 1;
59  
60 # this is basically:
61 # container( A::Bread::Board::Container->new, ... )
62 # or someone using &container as a constructor
63 13 $c = $name;
64  
65 # if we're in the context of another container
66 # then we're a subcontainer of it
67 13 ▶ $CC->add_sub_container($c) if defined $CC;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Bread::Board::CC 0 13 13
68 }
69 else {
70 109 my $is_inheriting = $name =~ s/^\+//;
71 109 ▶ confess "Inheriting containers isn't possible outside of the context of a container"
Branch: 1 branch
BranchTrueFalseTotal
if $is_inheriting and not defined $Bread::Board::CC 1 108 109
Condition100%3/3
$is_inheriting and not defined $Bread::Board::CC
!ll&&!rl&&r
10351
MC/DC100%2/2
$is_inheriting and not defined $Bread::Board::CC
$is_inheriting not defined $Bread::Board::CC
Truth table100%3/3
A and not(B)
A= $is_inheritingB= not defined $Bread::Board::CC
ABresult
0X0
100
111
72 if $is_inheriting && !defined $CC;
73  
74 # if we have more than 1 argument, then we are a parameterized
75 # container, so we need to act accordingly
76 108 ▶ if (scalar @_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) { } 11 97 108
77 11 ▶ confess 'Declaring container parameters when inheriting is not supported'
Branch: 1 branch
BranchTrueFalseTotal
if $is_inheriting 1 10 11
78 if $is_inheriting;
79  
80 10 my $param_names = shift;
81 10 $c = Bread::Board::Container::Parameterized->new({
82 name => $name,
83 allowed_parameter_names => $param_names,
84 });
85 }
86 else {
87 97 ▶ $c = $is_inheriting
Branch: 1 branch
BranchTrueFalseTotal
$is_inheriting ? : 4 93 97
88 ? $CC->fetch($name)
89 : Bread::Board::Container->new({ name => $name });
90 }
91  
92 # if we're in the context of another container
93 # then we're a subcontainer of it, unless we're inheriting,
94 # in which case we already got a parent
95 106 ▶ $CC->add_sub_container($c) if !$is_inheriting && defined $CC;
Branch: 1 branch
BranchTrueFalseTotal
if not $is_inheriting and defined $Bread::Board::CC 19 87 106
Condition100%3/3
not $is_inheriting and defined $Bread::Board::CC
!ll&&!rl&&r
38419
MC/DC100%2/2
not $is_inheriting and defined $Bread::Board::CC
not $is_inheriting defined $Bread::Board::CC
Truth table100%3/3
not(A) and B
A= not $is_inheritingB= defined $Bread::Board::CC
ABresult
0X0
100
111
96 }
97  
98 119 my $body = shift;
99 # if we have more arguments
100 # then they are likely a body
101 # and so we should execute it
102 119 ▶ if (defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 113 6 119
103 113 local $_ = $c;
104 113 local $CC = $c;
105 113 local $in_container = 1;
106 113 $body->($c);
107 }
108  
109 107 return $c;
110 }
111  
112 6 ▶sub include ($) {
Subroutine include: called CC=4 SCAR=13.9
Pod documented
113 6 my $file = shift;
114 6 ▶ if (my $ret = do $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ret = do $file) { } 3 3 6
115 3 return $ret;
116 }
117 else {
118 3 ▶ confess "Couldn't compile $file: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 2 3
119 2 ▶ confess "Couldn't open $file for reading: $!" if $!;
Branch: 1 branch
BranchTrueFalseTotal
if $! 1 1 2
120 1 confess "Unknown error when compiling $file "
121 . "(or $file doesn't return a true value)";
122 }
123 }
124  
125 167 ▶sub service ($@) {
Subroutine service: called CC=13 SCAR=25.7
Pod documented
126 167 my $name = shift;
127 167 my $s;
128  
129 167 my $is_inheriting = ($name =~ s/^\+//);
130  
131 167 ▶ if (scalar @_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1) { } 56 111 167
elsif (scalar(@_) % 2 == 0) { } 110 1 111
132 56 ▶ confess "Service inheritance doesn't make sense for literal services"
Branch: 1 branch
BranchTrueFalseTotal
if $is_inheriting 1 55 56
133 if $is_inheriting;
134  
135 55 $s = Bread::Board::Literal->new(name => $name, value => $_[0]);
136 }
137 elsif (scalar(@_) % 2 == 0) {
138 110 my %params = @_;
139  
140 110 my $class = $params{service_class};
141 $class ||= defined $params{service_type} ? "Bread::Board::$params{service_type}Injection"
142 110 ▶ : exists $params{block} ? 'Bread::Board::BlockInjection'
Branch: 2 branches
BranchTrueFalseTotal
defined $params{'service_type'} ? : 2 105 107
exists $params{'block'} ? : 33 72 105
Condition66%2/3
$class ||= defined $params{"service_type"} ? qq[Bread::Board::$params{"service_type"}Injection] : (exists $params{"block"} ? "Bread::Board::BlockInjection" : "Bread::Board::ConstructorInjection")
l!l&&r!l&&!r
31070
MC/DC0%0/2
$class ||= defined $params{"service_type"} ? qq[Bread::Board::$params{"service_type"}Injection] : (exists $params{"block"} ? "Bread::Board::BlockInjection" : "Bread::Board::ConstructorInjection")
$class defined $params{"service_type"} ? qq[Bread::Board::$params{"service_type"}Injection] : (exists $params{"block"} ? "Bread::Board::BlockInjection" : "Bread::Board::ConstructorInjection")
Truth table66%2/3
A ||= B
A= $classB= defined $params{"service_type"} ? qq[Bread::Board::$params{"service_type"}Injection] : (exists $params{"block"} ? "Bread::Board::BlockInjection" : "Bread::Board::ConstructorInjection")
ABresult
1X1
011
000
143 : 'Bread::Board::ConstructorInjection';
144  
145 110 ▶ $class->does('Bread::Board::Service')
Branch: 1 branch
BranchTrueFalseTotal
$class->does("Bread::Board::Service") or confess("The service class must do the Bread::Board::Service role") 109 1 110
146 or confess "The service class must do the Bread::Board::Service role";
147  
148 109 ▶ if ($is_inheriting) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_inheriting) { } 9 100 109
149 9 ▶ confess "Inheriting services isn't possible outside of the context of a container"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Bread::Board::CC 0 9 9
150 unless defined $CC;
151  
152 9 ▶ my $container = ($CC->isa('Bread::Board::Container::Parameterized') ? $CC->container : $CC);
Branch: 1 branch
BranchTrueFalseTotal
$Bread::Board::CC->isa('Bread::Board::Container::Parameterized') ? : 1 8 9
153 9 my $prototype_service = $container->fetch($name);
154  
155 9 ▶ confess sprintf(
Branch: 1 branch
BranchTrueFalseTotal
unless $prototype_service->does("Bread::Board::Service") 1 8 9
156 "Trying to inherit from service '%s', but found a %s",
157 $name, blessed $prototype_service,
158 ) unless $prototype_service->does('Bread::Board::Service');
159  
160 8 $s = $prototype_service->clone_and_inherit_params(
161 service_class => $class,
162 %params,
163 );
164 }
165 else {
166 100 $s = $class->new(name => $name, %params);
167 }
168 }
169 else {
170 1 confess "A service is defined by a name and either a single value or hash of parameters; you have supplied neither";
171 }
172 158 ▶ return $s unless defined $CC;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Bread::Board::CC 3 155 158
173 155 $CC->add_service($s);
174 }
175  
176 24 ▶sub alias ($$@) {
Subroutine alias: called CC=2 SCAR=6.9
Pod documented
177 24 my $name = shift;
178 24 my $path = shift;
179 24 my %params = @_;
180  
181 24 my $s = Bread::Board::Service::Alias->new(
182 name => $name,
183 aliased_from_path => $path,
184 %params,
185 );
186 24 ▶ return $s unless defined $CC;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Bread::Board::CC 1 23 24
187 23 $CC->add_service($s);
188 }
189  
190 33 ▶sub typemap ($@) {
Subroutine typemap: called CC=5 SCAR=16.1
Pod documented
191 33 my $type = shift;
192  
193 33 ▶ (scalar @_ == 1)
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 or confess("typemap takes a single argument") 32 1 33
194 || confess "typemap takes a single argument";
195  
196 32 my $service;
197 32 ▶ if (blessed $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0])) { } 26 6 32
198 26 ▶ if ($_[0]->does('Bread::Board::Service')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0]->does('Bread::Board::Service')) { } 1 25 26
elsif ($_[0]->isa('Bread::Board::Service::Inferred')) { } 24 1 25
199 1 $service = $_[0];
200 }
201 elsif ($_[0]->isa('Bread::Board::Service::Inferred')) {
202 24 $service = $_[0]->infer_service( $type );
203 }
204 else {
205 1 confess $_[0] . " isn't a service";
206 }
207 }
208 else {
209 6 $service = $CC->fetch( $_[0] );
210 }
211  
212 29 $CC->add_type_mapping_for( $type, $service );
213 }
214  
215 24 ▶sub infer {
Subroutine infer: called CC=2 SCAR=6.9
Pod documented
216 24 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 1 23 24
217 1 return Bread::Board::Service::Inferred->new(
218 current_container => $CC,
219 service => $_[0],
220 infer_params => 1,
221 );
222 }
223 else {
224 23 my %params = @_;
225 23 return Bread::Board::Service::Inferred->new(
226 current_container => $CC,
227 service_args => \%params,
228 infer_params => 1,
229 );
230 }
231 }
232  
233 2 ▶sub wire_names { +{ map { $_ => depends_on($_) } @_ }; }
Subroutine wire_names: called CC=1 SCAR=0.0
Pod documented
234  
235 65 ▶sub depends_on ($) {
Subroutine depends_on: called CC=1 SCAR=0.0
Pod documented
236 65 my $path = shift;
237 65 Bread::Board::Dependency->new(service_path => $path);
238 }
239  
240 54 my $LITERAL_ANON = 0;
241 2 ▶sub literal($) {
Subroutine literal: called CC=1 SCAR=0.0
Pod documented
242 2 my $value = shift;
243 2 Bread::Board::Literal->new(
244 name => 'LITERAL_ANON_' . $LITERAL_ANON++,
245 value => $value,
246 );
247 }
248  
249 54 1;
250  
251 __END__
« Board/Traversable.pm ↑ Summary Board/Container.pm »