IOC/Container.pm

filter: Statementstmt 100% 150 / 150 Branchbran 96.7% 60 / 62 Conditioncond 91.3% 21 / 23 MC/DCmcdc 86.6% 13 / 15 Subroutinesub 100% 34 / 34 Podpod 100% 22 / 22 totaltotal 98.0% 300 / 306 CC 42 SCAR 12.2
CC
42
Cov
98%
get
9.0
addProxy
5.0
addSubContainer
4.0
CRAP 3.4
« IOC/Registry.pm ↑ Summary IOC/Proxy/Interfaces.pm »
1  
2 package IOC::Container;
3  
4 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 23 our $VERSION = '0.14';
8  
9 23 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 23 ▶use IOC::Interfaces;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use IOC::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 23 ▶use IOC::Visitor::ServiceLocator;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 23 ▶use base 'IOC::Visitable';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 47 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
19 47 my ($_class, $name) = @_;
20 47 ▶ my $class = ref($_class) || $_class;
Condition33%1/3
ref $_class || $_class
l!l&&r!l&&!r
0470
MC/DC0%0/2
ref $_class || $_class
ref $_class $_class
Truth table33%1/3
A || B
A= ref $_classB= $_class
ABresult
1X1
011
000
21 47 my $container = {};
22 47 bless($container, $class);
23 47 $container->_init($name);
24 47 return $container;
25 }
26  
27 47 ▶sub _init {
Subroutine _init: called CC=2 SCAR=6.9
28 47 my ($self, $name) = @_;
29 47 $self->{services} = {};
30 47 $self->{service_locks} = {};
31 47 $self->{proxies} = {};
32 47 $self->{sub_containers} = {};
33 47 $self->{parent_container} = undef;
34 47 ▶ $self->{name} = $name || 'default';
Condition100%2/2
$name || 'default'
l!l
3215
MC/DC100%1/1
$name || 'default'
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
35 }
36  
37 32 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
38 32 my ($self) = @_;
39 32 return $self->{name};
40 }
41  
42 # parent containers
43  
44 25 ▶sub setParentContainer {
Subroutine setParentContainer: called CC=3 SCAR=11.0
Pod documented
45 25 my ($self, $parent_container) = @_;
46 25 ▶ (blessed($parent_container) && $parent_container->isa('IOC::Container'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($parent_container) and $parent_container->isa("IOC::Container") or "IOC::InsufficientArguments"->throw("You must provide an IOC::Container object as a parent container") 21 4 25
Condition100%3/3
builtin::blessed($parent_container) and $parent_container->isa("IOC::Container")
!ll&&!rl&&r
3121
MC/DC100%2/2
builtin::blessed($parent_container) and $parent_container->isa("IOC::Container")
builtin::blessed($parent_container) $parent_container->isa("IOC::Container")
Truth table100%3/3
A and B
A= builtin::blessed($parent_container)B= $parent_container->isa("IOC::Container")
ABresult
0X0
100
111
47 || throw IOC::InsufficientArguments "You must provide an IOC::Container object as a parent container";
48 21 $self->{parent_container} = $parent_container;
49 }
50  
51 5 ▶sub getParentContainer {
Subroutine getParentContainer: called CC=1 SCAR=0.0
Pod documented
52 5 my ($self) = @_;
53 5 return $self->{parent_container};
54 }
55  
56 11 ▶sub isRootContainer {
Subroutine isRootContainer: called CC=2 SCAR=6.9
Pod documented
57 11 my ($self) = @_;
58 11 ▶ return defined($self->{parent_container}) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'parent_container'} ? : 2 9 11
59 }
60  
61 9 ▶sub findRootContainer {
Subroutine findRootContainer: called CC=3 SCAR=11.0
Pod documented
62 9 my ($self) = @_;
63 9 ▶ return $self if $self->isRootContainer();
Branch: 1 branch
BranchTrueFalseTotal
if $self->isRootContainer 8 1 9
64 1 my $current = $self;
65 1 ▶ $current = $current->getParentContainer() until $current->isRootContainer();
Branch: 1 branch
BranchTrueFalseTotal
$current->isRootContainer or do { $current = $current->getParentContainer } 1 1 2
66 1 return $current;
67 }
68  
69 # sub containers
70  
71 26 ▶sub addSubContainer {
Subroutine addSubContainer: called CC=4 SCAR=13.9
Pod documented
72 26 my ($self, $container) = @_;
73 26 ▶ (blessed($container) && $container->isa('IOC::Container'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($container) and $container->isa("IOC::Container") or "IOC::InsufficientArguments"->throw("You must provide an IOC::Container object as a sub-container") 22 4 26
Condition100%3/3
builtin::blessed($container) and $container->isa("IOC::Container")
!ll&&!rl&&r
3122
MC/DC100%2/2
builtin::blessed($container) and $container->isa("IOC::Container")
builtin::blessed($container) $container->isa("IOC::Container")
Truth table100%3/3
A and B
A= builtin::blessed($container)B= $container->isa("IOC::Container")
ABresult
0X0
100
111
74 || throw IOC::InsufficientArguments "You must provide an IOC::Container object as a sub-container";
75 22 my $name = $container->name();
76 22 (!exists ${$self->{sub_containers}}{$name})
77 22 ▶ || throw IOC::ContainerAlreadyExists "Duplicate Sub-Container Name '${name}' in container '" . $self->{name} . "'";
Branch: 1 branch
BranchTrueFalseTotal
exists do { $$self{"sub_containers"} }->{$name} and "IOC::ContainerAlreadyExists"->throw("Duplicate Sub-Container Name '${name}' in container '" . $self->{"name"} . q[']) 1 21 22
78 21 $self->{sub_containers}->{$name} = $container;
79 21 $container->setParentContainer($self);
80 21 $self;
81 }
82  
83 7 ▶sub addSubContainers {
Subroutine addSubContainers: called CC=3 SCAR=11.0
Pod documented
84 7 my ($self, @containers) = @_;
85 7 ▶ (@containers) || throw IOC::InsufficientArguments "You must provide at least one IOC::Container to add";
Branch: 1 branch
BranchTrueFalseTotal
@containers or "IOC::InsufficientArguments"->throw("You must provide at least one IOC::Container to add") 6 1 7
86 6 $self->addSubContainer($_) foreach @containers;
87 6 $self;
88 }
89  
90 9 ▶sub hasSubContainer {
Subroutine hasSubContainer: called CC=3 SCAR=11.0
Pod documented
91 9 my ($self, $name) = @_;
92 9 ▶ (defined($name)) || throw IOC::InsufficientArguments "You must supply a name of a sub-container";
Branch: 1 branch
BranchTrueFalseTotal
defined $name or "IOC::InsufficientArguments"->throw("You must supply a name of a sub-container") 8 1 9
93 8 ▶ return (exists ${$self->{sub_containers}}{$name}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists do { $$self{'sub_containers'} }->{$name} ? : 2 6 8
94 }
95  
96 23 ▶sub hasSubContainers {
Subroutine hasSubContainers: called CC=2 SCAR=6.9
Pod documented
97 23 my ($self) = @_;
98 23 ▶ return scalar(keys(%{$self->{sub_containers}})) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %{$$self{'sub_containers'};} ? : 9 14 23
99 }
100  
101 6 ▶sub getSubContainerList {
Subroutine getSubContainerList: called CC=1 SCAR=0.0
Pod documented
102 6 my ($self) = @_;
103 6 return keys %{$self->{sub_containers}};
104 }
105  
106 37 ▶sub getSubContainer {
Subroutine getSubContainer: called CC=3 SCAR=11.0
Pod documented
107 37 my ($self, $name) = @_;
108 37 ▶ (defined($name)) || throw IOC::InsufficientArguments "You must supply a name of a sub-container";
Branch: 1 branch
BranchTrueFalseTotal
defined $name or "IOC::InsufficientArguments"->throw("You must supply a name of a sub-container") 36 1 37
109 36 (exists ${$self->{sub_containers}}{$name})
110 36 ▶ || throw IOC::ContainerNotFound "There is no subcontainer by the name '${name}' in container '" . $self->{name} . "'";
Branch: 1 branch
BranchTrueFalseTotal
exists do { $$self{"sub_containers"} }->{$name} or "IOC::ContainerNotFound"->throw("There is no subcontainer by the name '${name}' in container '" . $self->{"name"} . q[']) 33 3 36
111 33 return $self->{sub_containers}->{$name};
112 }
113  
114 7 ▶sub getAllSubContainers {
Subroutine getAllSubContainers: called CC=1 SCAR=0.0
Pod documented
115 7 my ($self) = @_;
116 7 return values %{$self->{sub_containers}};
117 }
118  
119 42 ▶sub accept {
Subroutine accept: called CC=3 SCAR=11.0
Pod documented
120 42 my ($self, $visitor) = @_;
121 42 ▶ (blessed($visitor) && $visitor->isa('IOC::Visitor'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($visitor) and $visitor->isa("IOC::Visitor") or "IOC::InsufficientArguments"->throw("You must pass an IOC::Visitor object to the visit method") 38 4 42
Condition100%3/3
builtin::blessed($visitor) and $visitor->isa("IOC::Visitor")
!ll&&!rl&&r
3138
MC/DC100%2/2
builtin::blessed($visitor) and $visitor->isa("IOC::Visitor")
builtin::blessed($visitor) $visitor->isa("IOC::Visitor")
Truth table100%3/3
A and B
A= builtin::blessed($visitor)B= $visitor->isa("IOC::Visitor")
ABresult
0X0
100
111
122 || throw IOC::InsufficientArguments "You must pass an IOC::Visitor object to the visit method";
123 38 return $visitor->visit($self);
124 }
125  
126 # services
127  
128 69 ▶sub register {
Subroutine register: called CC=4 SCAR=13.9
Pod documented
129 69 my ($self, $service) = @_;
130 69 ▶ (blessed($service) && $service->isa('IOC::Service'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($service) and $service->isa("IOC::Service") or "IOC::InsufficientArguments"->throw("You must provide a valid IOC::Service object to register") 65 4 69
Condition100%3/3
builtin::blessed($service) and $service->isa("IOC::Service")
!ll&&!rl&&r
3165
MC/DC100%2/2
builtin::blessed($service) and $service->isa("IOC::Service")
builtin::blessed($service) $service->isa("IOC::Service")
Truth table100%3/3
A and B
A= builtin::blessed($service)B= $service->isa("IOC::Service")
ABresult
0X0
100
111
131 || throw IOC::InsufficientArguments "You must provide a valid IOC::Service object to register";
132 65 my $name = $service->name();
133 65 ▶ (!exists ${$self->{services}}{$name})
Branch: 1 branch
BranchTrueFalseTotal
exists do { $$self{"services"} }->{$name} and "IOC::ServiceAlreadyExists"->throw("Duplicate Service Name '${name}'") 1 64 65
134 || throw IOC::ServiceAlreadyExists "Duplicate Service Name '${name}'";
135 64 $service->setContainer($self);
136 64 $self->{services}->{$name} = $service;
137 64 $self;
138 }
139  
140 3 ▶sub unregister {
Subroutine unregister: called CC=3 SCAR=11.0
Pod documented
141 3 my ($self, $name) = @_;
142 3 ▶ (defined($name)) || throw IOC::InsufficientArguments "You must provide a service name to unregister";
Branch: 1 branch
BranchTrueFalseTotal
defined $name or "IOC::InsufficientArguments"->throw("You must provide a service name to unregister") 2 1 3
143 2 ▶ (exists ${$self->{services}}{$name})
Branch: 1 branch
BranchTrueFalseTotal
exists do { $$self{"services"} }->{$name} or "IOC::ServiceNotFound"->throw("Unknown Service '${name}'") 1 1 2
144 || throw IOC::ServiceNotFound "Unknown Service '${name}'";
145 1 my $service = $self->{services}->{$name};
146 1 $service->removeContainer();
147 1 delete $self->{services}->{$name};
148 1 return $service;
149 }
150  
151 1 ▶sub registerWithProxy {
Subroutine registerWithProxy: called CC=1 SCAR=0.0
Pod documented
152 1 my ($self, $service, $proxy) = @_;
153 1 $self->register($service);
154 1 $self->addProxy($service->name(), $proxy);
155 1 $self;
156 }
157  
158 8 ▶sub addProxy {
Subroutine addProxy: called CC=5 SCAR=16.1
Pod documented
159 8 my ($self, $name, $proxy) = @_;
160 8 ▶ (defined($name)) || throw IOC::InsufficientArguments "You must provide a valid service name";
Branch: 1 branch
BranchTrueFalseTotal
defined $name or "IOC::InsufficientArguments"->throw("You must provide a valid service name") 7 1 8
161 7 ▶ (blessed($proxy) && $proxy->isa('IOC::Proxy'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($proxy) and $proxy->isa("IOC::Proxy") or "IOC::InsufficientArguments"->throw("You must provide a valid IOC::Proxy object to register") 3 4 7
Condition100%3/3
builtin::blessed($proxy) and $proxy->isa("IOC::Proxy")
!ll&&!rl&&r
313
MC/DC100%2/2
builtin::blessed($proxy) and $proxy->isa("IOC::Proxy")
builtin::blessed($proxy) $proxy->isa("IOC::Proxy")
Truth table100%3/3
A and B
A= builtin::blessed($proxy)B= $proxy->isa("IOC::Proxy")
ABresult
0X0
100
111
162 || throw IOC::InsufficientArguments "You must provide a valid IOC::Proxy object to register";
163 3 ▶ (exists ${$self->{services}}{$name})
Branch: 1 branch
BranchTrueFalseTotal
exists do { $$self{"services"} }->{$name} or "IOC::ServiceNotFound"->throw("Unknown Service '${name}'") 2 1 3
164 || throw IOC::ServiceNotFound "Unknown Service '${name}'";
165 2 $self->{proxies}->{$name} = $proxy;
166 2 $self;
167 }
168  
169 84 ▶sub get {
Subroutine get: called CC=9 SCAR=22.0
Pod documented
170 84 my ($self, $name, %params) = @_;
171 84 ▶ (defined($name)) || throw IOC::InsufficientArguments "You must provide a name of the service";
Branch: 1 branch
BranchTrueFalseTotal
defined $name or "IOC::InsufficientArguments"->throw("You must provide a name of the service") 83 1 84
172 83 ▶ (exists ${$self->{services}}{$name})
Branch: 1 branch
BranchTrueFalseTotal
exists do { $$self{"services"} }->{$name} or "IOC::ServiceNotFound"->throw("Unknown Service '${name}'") 81 2 83
173 || throw IOC::ServiceNotFound "Unknown Service '${name}'";
174 # a literal object can have no dependencies,
175 # and therefore can have no circular refs, so
176 # we can optimize their usage there as well
177 return $self->{services}->{$name}->instance()
178 81 ▶ if $self->{services}->{$name}->isa('IOC::Service::Literal');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"services"}{$name}->isa("IOC::Service::Literal") 14 67 81
179 67 ▶ if ($self->_isServiceLocked($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isServiceLocked($name)) 7 60 67
180 # NOTE:
181 # if the service is parameterized
182 # then we cannot defer it - SL
183 7 ▶ ($self->{services}->{$name}->isa('IOC::Service::Parameterized'))
Branch: 1 branch
BranchTrueFalseTotal
$self->{"services"}{$name}->isa("IOC::Service::Parameterized") and "IOC::IllegalOperation"->throw("The service '${name}' is locked, cannot defer a parameterized instance") 0 7 7
184 && throw IOC::IllegalOperation "The service '$name' is locked, cannot defer a parameterized instance";
185 # otherwise ...
186 7 return $self->{services}->{$name}->deferred();
187 }
188 60 $self->_lockService($name);
189 60 my $instance = $self->{services}->{$name}->instance(%params);
190 60 $self->_unlockService($name);
191 60 ▶ if (blessed($instance) && ref($instance) !~ /\:\:\_\:\:Proxy$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($instance) and not ref($instance) =~ /\:\:\_\:\:Proxy$/) 44 16 60
Condition100%3/3
builtin::blessed($instance) and not ref($instance) =~ /\:\:\_\:\:Proxy$/
!ll&&!rl&&r
15144
MC/DC100%2/2
builtin::blessed($instance) and not ref($instance) =~ /\:\:\_\:\:Proxy$/
builtin::blessed($instance) not ref($instance) =~ /\:\:\_\:\:Proxy$/
Truth table100%3/3
A and not(B)
A= builtin::blessed($instance)B= not ref($instance) =~ /\:\:\_\:\:Proxy$/
ABresult
0X0
100
111
192 44 ▶ return $self->{proxies}->{$name}->wrap($instance) if exists ${$self->{proxies}}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists do { $$self{"proxies"} }->{$name} 2 42 44
193 }
194 58 return $instance;
195 }
196  
197 25 ▶sub find {
Subroutine find: called CC=2 SCAR=6.9
Pod documented
198 25 my ($self, $path, $extra_args) = @_;
199 25 ▶ (defined($path))
Branch: 1 branch
BranchTrueFalseTotal
defined $path or "IOC::InsufficientArguments"->throw("You must provide a path of find a service") 24 1 25
200 || throw IOC::InsufficientArguments "You must provide a path of find a service";
201 24 return $self->accept(IOC::Visitor::ServiceLocator->new($path, $extra_args));
202 }
203  
204 20 ▶sub hasService {
Subroutine hasService: called CC=3 SCAR=11.0
Pod documented
205 20 my ($self, $name) = @_;
206 20 ▶ (defined($name)) || throw IOC::InsufficientArguments "You must provide a name of the service";
Branch: 1 branch
BranchTrueFalseTotal
defined $name or "IOC::InsufficientArguments"->throw("You must provide a name of the service") 19 1 20
207 19 ▶ return (exists ${$self->{services}}{$name}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists do { $$self{'services'} }->{$name} ? : 4 15 19
208 }
209  
210 3 ▶sub getServiceList {
Subroutine getServiceList: called CC=1 SCAR=0.0
Pod documented
211 3 my ($self) = @_;
212 3 return keys %{$self->{services}};
213 }
214  
215 8 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
216 8 my ($self) = @_;
217 # this will not DESTROY all the
218 # sub-containers it holds, since
219 # a sub-container might be still
220 # refered to elsewhere.
221 8 $self->{sub_containers} = undef;
222 # and the same with the parent
223 8 $self->{parent_container} = undef;
224 # this will DESTROY all the
225 # services it holds, since
226 # a service can only have one
227 # container, then this is okay
228 # to do that, otherwise we would
229 # need to deal with that.
230 8 foreach my $service (values %{$self->{services}}) {
231 2 ▶ defined $service && $service->DESTROY;
Branch: 1 branch
BranchTrueFalseTotal
defined $service and $service->DESTROY 2 0 2
232 }
233 }
234  
235 # private methods
236  
237 60 ▶sub _lockService {
Subroutine _lockService: called CC=1 SCAR=0.0
238 60 my ($self, $name) = @_;
239 60 $self->{lock_level}++;
240 60 $self->{service_locks}->{$name} = $self->{lock_level};
241 # use Data::Dumper;
242 # print "locking '$name' -> our locks are: " . Dumper($self->{service_locks});
243 }
244  
245 60 ▶sub _unlockService {
Subroutine _unlockService: called CC=1 SCAR=0.0
246 60 my ($self, $name) = @_;
247 60 $self->{lock_level}--;
248 60 delete $self->{service_locks}->{$name};
249 }
250  
251 67 ▶sub _isServiceLocked {
Subroutine _isServiceLocked: called CC=1 SCAR=0.0
252 67 my ($self, $name) = @_;
253 67 return (exists ${$self->{service_locks}}{$name});
254 }
255  
256 23 1;
257  
258 __END__
« IOC/Registry.pm ↑ Summary IOC/Proxy/Interfaces.pm »