| 1 | |||||||||||||||||||||
| 2 | package IOC::Service::ConstructorInjection; | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 8 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 8 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | our $VERSION = '0.07'; | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 8 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 8 | ▶ | use IOC::Exceptions; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 8 | ▶ | use base 'IOC::Service'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 16 | 0 | my ($_class, $name, $component_class, $component_constructor, $parameters) = @_; | |||||||||||||||||||
| 17 | 0 | ▶ | my $class = ref($_class) || $_class; | ||||||||||||||||||
|
Condition0%0/3
ref $_class || $_class
MC/DC0%0/2
ref $_class || $_class
ref $_class
$_class
Truth table0%0/3
A || B
A= ref $_classB= $_class
| |||||||||||||||||||||
| 18 | 0 | my $service = {}; | |||||||||||||||||||
| 19 | 0 | bless($service, $class); | |||||||||||||||||||
| 20 | 0 | $service->_init($name, $component_class, $component_constructor, $parameters); | |||||||||||||||||||
| 21 | 0 | return $service; | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | sub _init { | ||||||||||||||||||||
| 25 | my ($self, $name, $component_class, $component_constructor, $parameters) = @_; | ||||||||||||||||||||
| 26 | (defined($component_class) && defined($component_constructor)) | ||||||||||||||||||||
| 27 | || throw IOC::InsufficientArguments "You must provide a class and a constructor method"; | ||||||||||||||||||||
| 28 | (defined($parameters) && ref($parameters) eq 'ARRAY') | ||||||||||||||||||||
| 29 | || throw IOC::InsufficientArguments "You must provide a set of parameters for the constructor as an Array reference"; | ||||||||||||||||||||
| 30 | $self->{component_class} = $component_class; | ||||||||||||||||||||
| 31 | $self->{component_constructor} = $component_constructor; | ||||||||||||||||||||
| 32 | $self->{parameters} = $parameters; | ||||||||||||||||||||
| 33 | $self->SUPER::_init( | ||||||||||||||||||||
| 34 | $name, | ||||||||||||||||||||
| 35 | sub { | ||||||||||||||||||||
| 36 | # get the IOC::Container object | ||||||||||||||||||||
| 37 | my $c = shift; | ||||||||||||||||||||
| 38 | # check if there is an entry in the | ||||||||||||||||||||
| 39 | # symbol table for this class already | ||||||||||||||||||||
| 40 | # (meaning it has been loaded) and if | ||||||||||||||||||||
| 41 | # not, then require it | ||||||||||||||||||||
| 42 | eval { | ||||||||||||||||||||
| 43 | 8 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 44 | # check for the symbol table itself ... | ||||||||||||||||||||
| 45 | (keys %{"${component_class}::"} || | ||||||||||||||||||||
| 46 | # and then to be sure, lets look for | ||||||||||||||||||||
| 47 | # either the VERSION or the ISA variables | ||||||||||||||||||||
| 48 | (defined ${"${component_class}::VERSION"} | ||||||||||||||||||||
| 49 | || defined @{"${component_class}::ISA"})) ? 1 : 0; | ||||||||||||||||||||
| 50 | } || eval "use $component_class"; | ||||||||||||||||||||
| 51 | # throw our exception if the class fails to load | ||||||||||||||||||||
| 52 | throw IOC::ClassLoadingError "The class '$component_class' could not be loaded" => $@ if $@; | ||||||||||||||||||||
| 53 | # check to see if the specified | ||||||||||||||||||||
| 54 | # constructor is there | ||||||||||||||||||||
| 55 | my $constructor = $component_class->can($component_constructor); | ||||||||||||||||||||
| 56 | # if it is not, then throw our exception | ||||||||||||||||||||
| 57 | (defined($constructor)) | ||||||||||||||||||||
| 58 | || throw IOC::ConstructorNotFound "The constructor '$component_constructor' could not be found for class '$component_class'"; | ||||||||||||||||||||
| 59 | # now take care of the parameters | ||||||||||||||||||||
| 60 | # NOTE: | ||||||||||||||||||||
| 61 | # we must be sure to copy this otherwise it | ||||||||||||||||||||
| 62 | # will not work correctly with the prototype | ||||||||||||||||||||
| 63 | # verisons, this has to do with the scope of | ||||||||||||||||||||
| 64 | # this array, and how long it lives | ||||||||||||||||||||
| 65 | my @parameters = @{$parameters}; | ||||||||||||||||||||
| 66 | for (my $i = 0; $i < scalar @parameters; $i++) { | ||||||||||||||||||||
| 67 | # if the parameter is not been blessed | ||||||||||||||||||||
| 68 | # into the pseudo-package ComponentParameter | ||||||||||||||||||||
| 69 | # then we skip it, however ... | ||||||||||||||||||||
| 70 | next unless blessed($parameters[$i]) && $parameters[$i]->isa("ComponentParameter"); | ||||||||||||||||||||
| 71 | # if it has been, then we derference it | ||||||||||||||||||||
| 72 | # into the name of the service expected | ||||||||||||||||||||
| 73 | # and use the IOC::Container to get an | ||||||||||||||||||||
| 74 | # instance of that service and replace | ||||||||||||||||||||
| 75 | # that in the parameters array | ||||||||||||||||||||
| 76 | if (${$parameters[$i]} =~ /\//) { | ||||||||||||||||||||
| 77 | $parameters[$i] = $c->find(${$parameters[$i]}); | ||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | else { | ||||||||||||||||||||
| 80 | $parameters[$i] = $c->get(${$parameters[$i]}); | ||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | # now we have the class loaded, | ||||||||||||||||||||
| 84 | # the constructor confirmed, and | ||||||||||||||||||||
| 85 | # the parameters realized, so we | ||||||||||||||||||||
| 86 | # can create the instance now | ||||||||||||||||||||
| 87 | return $component_class->$constructor(@parameters); | ||||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | # class method | ||||||||||||||||||||
| 93 | sub ComponentParameter { shift; bless(\(my $param = shift), "ComponentParameter") }; | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 1; | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||