line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::DI::Factory; |
2
|
1
|
|
|
1
|
|
547
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
3
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
20
|
|
4
|
1
|
|
|
1
|
|
429
|
use UNIVERSAL::require; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub new{ |
7
|
|
|
|
|
|
|
my $class = shift; |
8
|
|
|
|
|
|
|
my $self = { |
9
|
|
|
|
|
|
|
_instance_pool => {}, |
10
|
|
|
|
|
|
|
}; |
11
|
|
|
|
|
|
|
bless $self,$class; |
12
|
|
|
|
|
|
|
return $self; |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub get_instance{ |
17
|
|
|
|
|
|
|
my $self = shift; |
18
|
|
|
|
|
|
|
my $definition = shift; |
19
|
|
|
|
|
|
|
if($definition->get_instance_type eq "singleton" |
20
|
|
|
|
|
|
|
and $self->_get_instance_pool($definition->get_name) |
21
|
|
|
|
|
|
|
){ |
22
|
|
|
|
|
|
|
return $self->_get_instance_pool($definition->get_name); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
my $instance = $self->_create_instance($definition); |
25
|
|
|
|
|
|
|
if($definition->get_instance_type eq "singleton"){ |
26
|
|
|
|
|
|
|
$self->_set_instance_pool( |
27
|
|
|
|
|
|
|
$definition->get_name, |
28
|
|
|
|
|
|
|
$instance, |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
return $instance; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _get_instance_pool{ |
35
|
|
|
|
|
|
|
my $self = shift; |
36
|
|
|
|
|
|
|
my $name = shift; |
37
|
|
|
|
|
|
|
return $self->{_instance_pool}->{$name} || undef; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub _set_instance_pool{ |
41
|
|
|
|
|
|
|
my $self = shift; |
42
|
|
|
|
|
|
|
my $name = shift; |
43
|
|
|
|
|
|
|
$self->{_instance_pool}->{$name} = shift; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub _create_instance{ |
47
|
|
|
|
|
|
|
my $self = shift; |
48
|
|
|
|
|
|
|
my $definition = shift; |
49
|
|
|
|
|
|
|
unless($definition->isa("Class::DI::Definition")){ |
50
|
|
|
|
|
|
|
die "Class::DI::Definition class only"; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
my $class = $definition->get_class_name; |
53
|
|
|
|
|
|
|
$class->require or die "cant load class $class"; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my %properties; |
56
|
|
|
|
|
|
|
foreach my $property (keys %{$definition->get_properties}){ |
57
|
|
|
|
|
|
|
my $property_value = $definition->get_properties->{$property}; |
58
|
|
|
|
|
|
|
$property_value = $self->_create_property($property_value); |
59
|
|
|
|
|
|
|
$properties{$property} = $property_value; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
if($definition->get_injection_type eq "setter"){ |
62
|
|
|
|
|
|
|
my $instance = $class->new; |
63
|
|
|
|
|
|
|
foreach my $property (keys %properties){ |
64
|
|
|
|
|
|
|
my $method = "set_". $property; |
65
|
|
|
|
|
|
|
$instance->$method($properties{$property}); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
return $instance; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
elsif($definition->get_injection_type eq "constructer"){ |
70
|
|
|
|
|
|
|
my $instance = $class->new(\%properties); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _create_property{ |
75
|
|
|
|
|
|
|
my $self = shift; |
76
|
|
|
|
|
|
|
my $property_value = shift; |
77
|
|
|
|
|
|
|
if(ref \$property_value eq "SCALAR"){ |
78
|
|
|
|
|
|
|
return $property_value; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
elsif(ref $property_value eq "ARRAY"){ |
81
|
|
|
|
|
|
|
my @values = map { $self->_create_property($_) } @{$property_value}; |
82
|
|
|
|
|
|
|
return \@values; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
elsif(ref $property_value eq "HASH"){ |
85
|
|
|
|
|
|
|
foreach my $key (keys %{$property_value}){ |
86
|
|
|
|
|
|
|
$property_value->{$key} = $self->_create_property($property_value->{$key}); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
return $property_value; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
elsif($property_value->isa("Class::DI::Definition")){ |
91
|
|
|
|
|
|
|
return $self->get_instance($property_value); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
1; |