| 1 | |||||||||||||||||||||||||||||
| 2 | package IOC::Visitor::ServiceLocator; | ||||||||||||||||||||||||||||
| 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.02'; | |||||||||||||||||||||||||||
| 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 base 'IOC::Visitor'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 32 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 32 | my ($_class, $path, $extra_args) = @_; | |||||||||||||||||||||||||||
| 18 | 32 | ▶ | ($path) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | || throw IOC::InsufficientArguments "You must provide an path to locate a container"; | ||||||||||||||||||||||||||||
| 20 | 31 | ▶ | my $class = ref($_class) || $_class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $_class || $_class
MC/DC0%0/2
ref $_class || $_class
ref $_class
$_class
Truth table33%1/3
A || B
A= ref $_classB= $_class
| |||||||||||||||||||||||||||||
| 21 | 31 | my $visitor = { | |||||||||||||||||||||||||||
| 22 | path => $path, | ||||||||||||||||||||||||||||
| 23 | extra_args => $extra_args | ||||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||
| 25 | 31 | bless($visitor, $class); | |||||||||||||||||||||||||||
| 26 | 31 | return $visitor; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 34 | ▶ | sub visit { | ||||||||||||||||||||||||||
|
Subroutine
visit: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 34 | my ($self, $container) = @_; | |||||||||||||||||||||||||||
| 31 | 34 | ▶ | (blessed($container) && $container->isa('IOC::Container')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($container) and $container->isa("IOC::Container")
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")
| |||||||||||||||||||||||||||||
| 32 | || throw IOC::InsufficientArguments "You must provide an IOC::Container object as a sub-container"; | ||||||||||||||||||||||||||||
| 33 | 30 | my $service; | |||||||||||||||||||||||||||
| 34 | 30 | ▶ | my @extra_args = (defined $self->{extra_args} ? @{$self->{extra_args}} : ()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 30 | my @path = grep { $_ } split /\// => $self->{path}; | |||||||||||||||||||||||||||
| 36 | 30 | my $service_name = pop @path; | |||||||||||||||||||||||||||
| 37 | 30 | ▶ | if ($self->{path} =~ /^\//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | # start at the root | ||||||||||||||||||||||||||||
| 39 | 8 | my $current = $container->findRootContainer(); | |||||||||||||||||||||||||||
| 40 | 8 | foreach my $container_name (@path) { | |||||||||||||||||||||||||||
| 41 | 11 | eval { | |||||||||||||||||||||||||||
| 42 | 11 | $current = $current->getSubContainer($container_name); | |||||||||||||||||||||||||||
| 43 | }; | ||||||||||||||||||||||||||||
| 44 | 11 | ▶ | throw IOC::UnableToLocateService "Could not locate the service at path '" . $self->{path} . "' failed at '$container_name'", $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 7 | $service = $current->get($service_name, @extra_args); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | else { | ||||||||||||||||||||||||||||
| 49 | 22 | my $current = $container; | |||||||||||||||||||||||||||
| 50 | 22 | foreach my $container_name (@path) { | |||||||||||||||||||||||||||
| 51 | 12 | ▶ | if ($container_name eq '..') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 4 | $current = $current->getParentContainer(); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | else { | ||||||||||||||||||||||||||||
| 55 | 8 | eval { | |||||||||||||||||||||||||||
| 56 | 8 | $current = $current->getSubContainer($container_name); | |||||||||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||||||||||
| 58 | 8 | ▶ | throw IOC::UnableToLocateService "Could not locate the service at path '" . $self->{path} . "' failed at '$container_name'", $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 21 | $service = $current->get($service_name, @extra_args); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 27 | return $service; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 23 | 1; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | __END__ | ||||||||||||||||||||||||||||