|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/di package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Di\Definition\Resolver; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Common\Base; |
|
13
|
|
|
use Slick\Common\Inspector; |
|
14
|
|
|
use Slick\Di\Definition\ObjectDefinitionInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Object definition resolver |
|
18
|
|
|
* |
|
19
|
|
|
* @package Slick\Di\Definition\Resolver |
|
20
|
|
|
*/ |
|
21
|
|
|
class Object extends Base implements ObjectResolver |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @readwrite |
|
25
|
|
|
* @var ObjectDefinitionInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $definition; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Resolves provided definition |
|
31
|
|
|
* |
|
32
|
|
|
* @return mixed |
|
33
|
|
|
*/ |
|
34
|
58 |
|
public function resolve() |
|
35
|
|
|
{ |
|
36
|
58 |
|
return $this->setProperties() |
|
37
|
58 |
|
->setMethods() |
|
38
|
58 |
|
->definition->getInstance(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Set the definition that this resolver will resolve |
|
43
|
|
|
* |
|
44
|
|
|
* @param ObjectDefinitionInterface $definition Definition to resolve |
|
45
|
|
|
* |
|
46
|
|
|
* @return $this|self |
|
47
|
|
|
*/ |
|
48
|
58 |
|
public function setDefinition($definition) |
|
49
|
|
|
{ |
|
50
|
58 |
|
$this->definition = $definition; |
|
51
|
58 |
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Call definition methods with parameters |
|
56
|
|
|
* |
|
57
|
|
|
* @return $this|self |
|
58
|
|
|
*/ |
|
59
|
58 |
|
private function setMethods() |
|
60
|
|
|
{ |
|
61
|
58 |
|
foreach ($this->definition->getMethods() as $data) { |
|
62
|
10 |
|
list($name, $params) = array_values($data); |
|
63
|
10 |
|
call_user_func_array( |
|
64
|
10 |
|
[$this->definition->getInstance(), $name], |
|
65
|
10 |
|
$this->checkValues($params) |
|
66
|
10 |
|
); |
|
67
|
58 |
|
} |
|
68
|
58 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Sets property values |
|
73
|
|
|
* |
|
74
|
|
|
* @return $this|self |
|
75
|
|
|
*/ |
|
76
|
58 |
|
private function setProperties() |
|
77
|
|
|
{ |
|
78
|
58 |
|
foreach ($this->definition->getProperties() as $name => $value) { |
|
79
|
14 |
|
$this->setProperty($name, $this->checkValue($value)); |
|
80
|
58 |
|
} |
|
81
|
58 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Set a value to provided property |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $name Property name |
|
88
|
|
|
* @param mixed $value Property value |
|
89
|
|
|
*/ |
|
90
|
14 |
|
private function setProperty($name, $value) |
|
91
|
|
|
{ |
|
92
|
14 |
|
$property = Inspector::forClass($this->definition->getInstance()) |
|
93
|
14 |
|
->getReflection() |
|
94
|
14 |
|
->getProperty($name); |
|
95
|
|
|
|
|
96
|
14 |
|
if (!$property->isPublic()) { |
|
97
|
12 |
|
$property->setAccessible(true); |
|
98
|
12 |
|
} |
|
99
|
14 |
|
$property->setValue($this->definition->getInstance(), $value); |
|
100
|
14 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Check values to resolve dependencies |
|
104
|
|
|
* |
|
105
|
|
|
* @param array $params |
|
106
|
|
|
* |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
14 |
|
public function checkValues(array $params) |
|
110
|
|
|
{ |
|
111
|
14 |
|
$values = []; |
|
112
|
14 |
|
foreach ($params as $value) { |
|
113
|
14 |
|
$values[] = $this->checkValue($value); |
|
114
|
14 |
|
} |
|
115
|
14 |
|
return $values; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Check if value is a container entry and change it |
|
120
|
|
|
* |
|
121
|
|
|
* @param mixed $param |
|
122
|
|
|
* |
|
123
|
|
|
* @return mixed |
|
124
|
|
|
*/ |
|
125
|
20 |
|
private function checkValue($param) |
|
126
|
|
|
{ |
|
127
|
20 |
|
$value = $param; |
|
128
|
|
|
if ( |
|
129
|
20 |
|
is_scalar($param) && |
|
130
|
20 |
|
preg_match('/^@(?P<key>.*)$/i', $param, $result) && |
|
131
|
4 |
|
$this->definition->getContainer()->has($result['key']) |
|
132
|
20 |
|
) { |
|
133
|
4 |
|
$value = $this->definition->getContainer()->get($result['key']); |
|
134
|
4 |
|
} |
|
135
|
20 |
|
return $value; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|