| 1 | |||||||||||||||||||||||||||||
| 2 | package IOC::Registry; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 11 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use IOC::Exceptions; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 11 | ▶ | use IOC::Interfaces; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 11 | ▶ | use IOC::Visitor::SearchForService; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 11 | ▶ | use IOC::Visitor::SearchForContainer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 11 | ▶ | use base 'Class::StrongSingleton'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 5 | my ($_class) = @_; | |||||||||||||||||||||||||||
| 21 | 5 | ▶ | 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
| |||||||||||||||||||||||||||||
| 22 | 5 | my $registry = { | |||||||||||||||||||||||||||
| 23 | containers => {}, | ||||||||||||||||||||||||||||
| 24 | service_aliases => {} | ||||||||||||||||||||||||||||
| 25 | }; | ||||||||||||||||||||||||||||
| 26 | 5 | bless($registry, $class); | |||||||||||||||||||||||||||
| 27 | 5 | $registry->_init(); | |||||||||||||||||||||||||||
| 28 | 5 | return $registry; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # add and remove containers | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 11 | ▶ | sub registerContainer { | ||||||||||||||||||||||||||
|
Subroutine
registerContainer: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 11 | my ($self, $container) = @_; | |||||||||||||||||||||||||||
| 35 | 11 | ▶ | (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")
| |||||||||||||||||||||||||||||
| 36 | || throw IOC::InsufficientArguments "You must supply a valid IOC::Container object"; | ||||||||||||||||||||||||||||
| 37 | 7 | my $name = $container->name(); | |||||||||||||||||||||||||||
| 38 | 7 | ▶ | (!exists ${$self->{containers}}{$name}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | || throw IOC::ContainerAlreadyExists "Duplicate Container '$name'"; | ||||||||||||||||||||||||||||
| 40 | 6 | $self->{containers}->{$name} = $container; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 6 | ▶ | sub unregisterContainer { | ||||||||||||||||||||||||||
|
Subroutine
unregisterContainer: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 6 | my ($self, $container_or_name) = @_; | |||||||||||||||||||||||||||
| 45 | 6 | ▶ | (defined($container_or_name)) || throw IOC::InsufficientArguments "You must supply a name or a container"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 5 | my $name; | |||||||||||||||||||||||||||
| 47 | 5 | ▶ | if (ref($container_or_name)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 3 | ▶ | (blessed($container_or_name) && $container_or_name->isa('IOC::Container')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($container_or_name) and $container_or_name->isa("IOC::Container")
MC/DC100%2/2
builtin::blessed($container_or_name) and $container_or_name->isa("IOC::Container")
builtin::blessed($container_or_name)
$container_or_name->isa("IOC::Container")
Truth table100%3/3
A and B
A= builtin::blessed($container_or_name)B= $container_or_name->isa("IOC::Container")
| |||||||||||||||||||||||||||||
| 49 | || throw IOC::InsufficientArguments "You must supply a valid IOC::Container object"; | ||||||||||||||||||||||||||||
| 50 | 1 | $name = $container_or_name->name(); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | else { | ||||||||||||||||||||||||||||
| 53 | 2 | $name = $container_or_name; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 3 | ▶ | (exists ${$self->{containers}}{$name}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | || throw IOC::ContainerNotFound "Cannot unregister a container we do not have"; | ||||||||||||||||||||||||||||
| 57 | 2 | my $container = $self->{containers}->{$name}; | |||||||||||||||||||||||||||
| 58 | 2 | delete $self->{containers}->{$name}; | |||||||||||||||||||||||||||
| 59 | 2 | return $container; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # fetching the containers | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 7 | ▶ | sub getRegisteredContainer { | ||||||||||||||||||||||||||
|
Subroutine
getRegisteredContainer: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 7 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 66 | 7 | ▶ | (defined($name)) || throw IOC::InsufficientArguments "You must supply a name of a container"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 6 | ▶ | (exists ${$self->{containers}}{$name}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | || throw IOC::ContainerNotFound "There is no container by the name '${name}'"; | ||||||||||||||||||||||||||||
| 69 | 3 | return $self->{containers}->{$name}; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 4 | ▶ | sub getRegisteredContainerList { | ||||||||||||||||||||||||||
|
Subroutine
getRegisteredContainerList: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 74 | 4 | return keys %{$self->{containers}}; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 5 | ▶ | sub hasRegisteredContainer { | ||||||||||||||||||||||||||
|
Subroutine
hasRegisteredContainer: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 5 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 79 | 5 | ▶ | (defined($name)) || throw IOC::InsufficientArguments "You must supply a name of a container"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 4 | ▶ | return (exists ${$self->{containers}}{$name}) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # aliasing | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 3 | ▶ | sub aliasService { | ||||||||||||||||||||||||||
|
Subroutine
aliasService: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 3 | my ($self, $real_path, $alias_path) = @_; | |||||||||||||||||||||||||||
| 87 | 3 | ▶ | (defined($alias_path) && defined($real_path)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $alias_path and defined $real_path
MC/DC50%1/2
defined $alias_path and defined $real_path
defined $alias_path
defined $real_path
Truth table66%2/3
A and B
A= defined $alias_pathB= defined $real_path
| |||||||||||||||||||||||||||||
| 88 | || throw IOC::InsufficientArguments "You must supply a alias path and a real path"; | ||||||||||||||||||||||||||||
| 89 | 1 | $self->{service_aliases}->{$alias_path} = $real_path; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # locate Service by path | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 19 | ▶ | sub locateService { | ||||||||||||||||||||||||||
|
Subroutine
locateService: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 19 | my ($self, $path, @extra_args) = @_; | |||||||||||||||||||||||||||
| 96 | 19 | ▶ | (defined($path)) || throw IOC::InsufficientArguments "You must supply a path to a service"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | # if the service has been aliased, get the real path ... | ||||||||||||||||||||||||||||
| 98 | 18 | ▶ | $path = $self->{service_aliases}->{$path} if exists ${$self->{service_aliases}}{$path}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | # and go about your normal business ... | ||||||||||||||||||||||||||||
| 100 | 18 | my @path = grep { $_ } split /\// => $path; | |||||||||||||||||||||||||||
| 101 | 18 | my $registered_container_name = shift @path; | |||||||||||||||||||||||||||
| 102 | 18 | ▶ | (exists ${$self->{containers}}{$registered_container_name}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | || throw IOC::ContainerNotFound "There is no registered container found at '$registered_container_name' for the path '${path}'"; | ||||||||||||||||||||||||||||
| 104 | 17 | my $service; | |||||||||||||||||||||||||||
| 105 | 17 | eval { | |||||||||||||||||||||||||||
| 106 | 17 | $service = $self->{containers}->{$registered_container_name}->find((join "/" => @path), \@extra_args); | |||||||||||||||||||||||||||
| 107 | }; | ||||||||||||||||||||||||||||
| 108 | 17 | ▶ | throw IOC::ServiceNotFound "There is no service found at the path '${path}'" => $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 16 | return $service; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 9 | ▶ | sub locateContainer { | ||||||||||||||||||||||||||
|
Subroutine
locateContainer: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 9 | my ($self, $path) = @_; | |||||||||||||||||||||||||||
| 114 | 9 | ▶ | (defined($path)) || throw IOC::InsufficientArguments "You must supply a path to a container"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 8 | my @path = grep { $_ } split /\// => $path; | |||||||||||||||||||||||||||
| 116 | 8 | my $registered_container_name = shift @path; | |||||||||||||||||||||||||||
| 117 | 8 | ▶ | (exists ${$self->{containers}}{$registered_container_name}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | || throw IOC::ContainerNotFound "There is no container found at the path '${path}'"; | ||||||||||||||||||||||||||||
| 119 | 7 | my $current = $self->{containers}->{$registered_container_name}; | |||||||||||||||||||||||||||
| 120 | 7 | eval { | |||||||||||||||||||||||||||
| 121 | 7 | ▶ | $current = $current->getSubContainer(shift @path) while @path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | }; | ||||||||||||||||||||||||||||
| 123 | 7 | ▶ | throw IOC::ContainerNotFound "There is no container found at the path '${path}'" => $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | # otherwise ... | ||||||||||||||||||||||||||||
| 125 | 6 | return $current; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # searching for containers | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 2 | ▶ | sub searchForContainer { | ||||||||||||||||||||||||||
|
Subroutine
searchForContainer: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | 2 | my ($self, $container_to_find) = @_; | |||||||||||||||||||||||||||
| 132 | 2 | my $container_found; | |||||||||||||||||||||||||||
| 133 | 2 | foreach my $container (values %{$self->{containers}}) { | |||||||||||||||||||||||||||
| 134 | 4 | $container_found = $container->accept(IOC::Visitor::SearchForContainer->new($container_to_find)); | |||||||||||||||||||||||||||
| 135 | 4 | ▶ | last if defined $container_found; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 2 | return $container_found; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 2 | ▶ | sub searchForService { | ||||||||||||||||||||||||||
|
Subroutine
searchForService: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 2 | my ($self, $service_to_find) = @_; | |||||||||||||||||||||||||||
| 142 | 2 | my $service; | |||||||||||||||||||||||||||
| 143 | 2 | foreach my $container (values %{$self->{containers}}) { | |||||||||||||||||||||||||||
| 144 | 4 | $service = $container->accept(IOC::Visitor::SearchForService->new($service_to_find)); | |||||||||||||||||||||||||||
| 145 | 4 | ▶ | last if $service; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 2 | return $service; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 2 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 151 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 152 | # get rid of all our containers | ||||||||||||||||||||||||||||
| 153 | 2 | foreach my $container (values %{$self->{containers}}) { | |||||||||||||||||||||||||||
| 154 | 2 | ▶ | defined $container && $container->DESTROY; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | # let the Singleton do its work | ||||||||||||||||||||||||||||
| 157 | 2 | $self->SUPER::DESTROY(); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 11 | 1; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | __END__ | ||||||||||||||||||||||||||||