1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Respect\Config; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
|
7
|
|
|
class Instantiator |
8
|
|
|
{ |
9
|
|
|
const MODE_DEPENDENCY = false; |
10
|
|
|
const MODE_FACTORY = 'new'; |
11
|
|
|
|
12
|
|
|
protected $instance; |
13
|
|
|
protected $reflection; |
14
|
|
|
protected $constructor = array(); |
15
|
|
|
protected $className; |
16
|
|
|
protected $params = array(); |
17
|
|
|
protected $staticMethodCalls = array(); |
18
|
|
|
protected $methodCalls = array(); |
19
|
|
|
protected $propertySetters = array(); |
20
|
|
|
protected $mode = self::MODE_DEPENDENCY; |
21
|
|
|
|
22
|
32 |
|
public function __construct($className) |
23
|
|
|
{ |
24
|
32 |
|
if (false !== stripos($className, ' ')) { |
25
|
1 |
|
list($mode, $className) = explode(' ', $className, 2); |
26
|
1 |
|
$this->mode = $mode; |
27
|
|
|
} |
28
|
32 |
|
$this->reflection = new ReflectionClass($className); |
29
|
32 |
|
$this->constructor = $this->findConstructorParams($this->reflection); |
30
|
32 |
|
$this->className = $className; |
31
|
32 |
|
} |
32
|
|
|
|
33
|
11 |
|
public function getMode() |
34
|
|
|
{ |
35
|
11 |
|
return $this->mode; |
36
|
|
|
} |
37
|
|
|
|
38
|
12 |
|
public function __invoke() |
39
|
|
|
{ |
40
|
12 |
|
return call_user_func_array(array($this, 'getInstance'), func_get_args()); |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
public function getClassName() |
44
|
|
|
{ |
45
|
3 |
|
return $this->className; |
46
|
|
|
} |
47
|
|
|
|
48
|
24 |
|
public function getInstance($forceNew = false) |
49
|
|
|
{ |
50
|
24 |
|
if ($this->mode == static::MODE_FACTORY) { |
51
|
1 |
|
$this->instance = null; |
52
|
|
|
} |
53
|
|
|
|
54
|
24 |
|
if ($this->instance && !$forceNew) { |
55
|
2 |
|
return $this->instance; |
56
|
|
|
} |
57
|
|
|
|
58
|
24 |
|
$className = $this->className; |
59
|
24 |
|
$staticMethods = count($this->staticMethodCalls); |
60
|
24 |
|
foreach ($this->staticMethodCalls as $methodCalls) { |
61
|
2 |
|
$this->performMethodCalls( |
62
|
2 |
|
$className, |
63
|
2 |
|
$methodCalls, |
64
|
|
|
function ($result) use ($className, &$instance, $staticMethods) { |
65
|
2 |
|
if ($result instanceof $className || ($staticMethods == 1 && is_object($result))) { |
66
|
2 |
|
$instance = $result; |
67
|
|
|
} |
68
|
2 |
|
} |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
24 |
|
$constructor = $this->reflection->getConstructor(); |
73
|
24 |
|
$hasConstructor = ($constructor) ? $constructor->isPublic() : false; |
74
|
24 |
|
if (empty($instance)) { |
75
|
22 |
|
if (empty($this->constructor) || !$hasConstructor) { |
76
|
5 |
|
$instance = new $className(); |
|
|
|
|
77
|
|
|
} else { |
78
|
17 |
|
$instance = $this->reflection->newInstanceArgs( |
|
|
|
|
79
|
17 |
|
$this->cleanupParams($this->constructor) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
24 |
|
foreach ($this->propertySetters as $property => $value) { |
85
|
6 |
|
$instance->{$property} = $this->lazyLoad($value); |
86
|
|
|
} |
87
|
|
|
|
88
|
24 |
|
foreach ($this->methodCalls as $methodCalls) { |
89
|
7 |
|
$this->performMethodCalls($instance, $methodCalls); |
90
|
|
|
} |
91
|
|
|
|
92
|
24 |
|
return $this->instance = $instance; |
93
|
|
|
} |
94
|
|
|
|
95
|
4 |
|
public function getParam($name) |
96
|
|
|
{ |
97
|
4 |
|
return $this->params[$name]; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function setInstance($instance) |
101
|
|
|
{ |
102
|
1 |
|
$this->instance = $instance; |
103
|
1 |
|
} |
104
|
|
|
|
105
|
30 |
|
public function setParam($name, $value) |
106
|
|
|
{ |
107
|
30 |
|
$value = $this->processValue($value); |
108
|
|
|
|
109
|
30 |
|
if ($this->matchStaticMethod($name)) { |
110
|
2 |
|
$this->staticMethodCalls[] = array($name, $value); |
111
|
28 |
|
} elseif ($this->matchConstructorParam($name)) { |
112
|
9 |
|
$this->constructor[$name] = $value; |
113
|
21 |
|
} elseif ($this->matchFullConstructor($name, $value)) { |
114
|
4 |
|
$this->constructor = $value; |
115
|
17 |
|
} elseif ($this->matchMethod($name)) { |
116
|
7 |
|
$this->methodCalls[] = array($name, $value); |
117
|
|
|
} else { |
118
|
11 |
|
$this->propertySetters[$name] = $value; |
119
|
|
|
} |
120
|
|
|
|
121
|
30 |
|
$this->params[$name] = $value; |
122
|
30 |
|
} |
123
|
|
|
|
124
|
|
|
public function getParams() |
125
|
|
|
{ |
126
|
|
|
return $this->params; |
127
|
|
|
} |
128
|
|
|
|
129
|
20 |
|
protected function cleanupParams(array $params) |
130
|
|
|
{ |
131
|
20 |
|
while (null === end($params)) { |
132
|
11 |
|
unset($params[key($params)]); |
133
|
|
|
} |
134
|
|
|
|
135
|
20 |
|
foreach ($params as &$p) { |
136
|
16 |
|
$p = $this->lazyLoad($p); |
137
|
|
|
} |
138
|
|
|
|
139
|
20 |
|
return $params; |
140
|
|
|
} |
141
|
|
|
|
142
|
22 |
|
protected function lazyLoad($value) |
143
|
|
|
{ |
144
|
22 |
|
return $value instanceof self ? $value->getInstance() : $value; |
145
|
|
|
} |
146
|
|
|
|
147
|
32 |
|
protected function findConstructorParams(ReflectionClass $class) |
148
|
|
|
{ |
149
|
32 |
|
$params = array(); |
150
|
32 |
|
$constructor = $class->getConstructor(); |
151
|
|
|
|
152
|
32 |
|
if (!$constructor) { |
153
|
13 |
|
return array(); |
154
|
|
|
} |
155
|
|
|
|
156
|
20 |
|
foreach ($constructor->getParameters() as $param) { |
157
|
18 |
|
$params[$param->getName()] = $param->isDefaultValueAvailable() ? |
158
|
18 |
|
$param->getDefaultValue() : null; |
159
|
|
|
} |
160
|
|
|
|
161
|
20 |
|
return $params; |
162
|
|
|
} |
163
|
|
|
|
164
|
30 |
|
protected function processValue($value) |
165
|
|
|
{ |
166
|
30 |
|
if (is_array($value)) { |
167
|
15 |
|
foreach ($value as $valueKey => $subValue) { |
168
|
15 |
|
$value[$valueKey] = $this->processValue($subValue); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
30 |
|
return $value; |
173
|
|
|
} |
174
|
|
|
|
175
|
28 |
|
protected function matchConstructorParam($name) |
176
|
|
|
{ |
177
|
28 |
|
return array_key_exists($name, $this->constructor); |
178
|
|
|
} |
179
|
|
|
|
180
|
21 |
|
protected function matchFullConstructor($name, &$value) |
|
|
|
|
181
|
|
|
{ |
182
|
21 |
|
return $name == '__construct' |
183
|
21 |
|
|| ($name == $this->className && stripos($this->className, '\\')); |
184
|
|
|
} |
185
|
|
|
|
186
|
17 |
|
protected function matchMethod($name) |
187
|
|
|
{ |
188
|
17 |
|
return $this->reflection->hasMethod($name); |
189
|
|
|
} |
190
|
|
|
|
191
|
30 |
|
protected function matchStaticMethod($name) |
192
|
|
|
{ |
193
|
30 |
|
return $this->reflection->hasMethod($name) |
194
|
30 |
|
&& $this->reflection->getMethod($name)->isStatic(); |
195
|
|
|
} |
196
|
|
|
|
197
|
9 |
|
protected function performMethodCalls($class, array $methodCalls, $resultCallback = null) |
198
|
|
|
{ |
199
|
9 |
|
list($methodName, $calls) = $methodCalls; |
200
|
9 |
|
$resultCallback = $resultCallback ?: function () { |
201
|
|
|
|
202
|
9 |
|
}; |
203
|
|
|
|
204
|
9 |
|
foreach ($calls as $arguments) { |
205
|
9 |
|
if (is_array($arguments)) { |
206
|
7 |
|
$resultCallback(call_user_func_array( |
207
|
7 |
|
array($class, $methodName), |
208
|
7 |
|
$this->cleanUpParams($arguments) |
209
|
|
|
)); |
210
|
2 |
|
} elseif (!is_null($arguments)) { |
211
|
2 |
|
$resultCallback(call_user_func(array($class, $methodName), $this->lazyLoad($arguments))); |
212
|
|
|
} else { |
213
|
9 |
|
$resultCallback(call_user_func(array($class, $methodName))); |
214
|
|
|
} |
215
|
|
|
} |
216
|
9 |
|
} |
217
|
|
|
} |
218
|
|
|
|
It seems like you are assigning to a variable which was imported through a
use
statement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope