1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Di |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Di\Traits; |
16
|
|
|
|
17
|
|
|
use Phossa2\Di\Container; |
18
|
|
|
use Phossa2\Di\Message\Message; |
19
|
|
|
use Phossa2\Di\Exception\LogicException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* FactoryTrait |
23
|
|
|
* |
24
|
|
|
* Create service instance here |
25
|
|
|
* |
26
|
|
|
* @package Phossa2\Di |
27
|
|
|
* @author Hong Zhang <[email protected]> |
28
|
|
|
* @version 2.0.0 |
29
|
|
|
* @since 2.0.0 added |
30
|
|
|
*/ |
31
|
|
|
trait FactoryTrait |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var Container |
35
|
|
|
* @access protected |
36
|
|
|
*/ |
37
|
|
|
protected $master; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get service definition (and fix it) |
41
|
|
|
* |
42
|
|
|
* @param string $rawId |
43
|
|
|
* @param array $args |
44
|
|
|
* @return array |
45
|
|
|
* @access protected |
46
|
|
|
*/ |
47
|
|
|
protected function getDefinition( |
48
|
|
|
/*# string */ $rawId, |
49
|
|
|
array $args |
50
|
|
|
)/*# : array */ { |
51
|
|
|
// get the definition |
52
|
|
|
$def = $this->master->getResolver()->getService($rawId); |
53
|
|
|
|
54
|
|
|
// fix class |
55
|
|
|
if (!is_array($def) || !isset($def['class'])) { |
56
|
|
|
$def = ['class' => $def]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// resolve arguments |
60
|
|
|
if (!empty($args)) { |
61
|
|
|
$this->master->resolve($args); |
62
|
|
|
$def['args'] = $args; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return (array) $def; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Instantiate service object from classname |
70
|
|
|
* |
71
|
|
|
* @param string $class |
72
|
|
|
* @param array $args |
73
|
|
|
* @return object |
74
|
|
|
* @throws LogicException if something goes wrong |
75
|
|
|
* @access protected |
76
|
|
|
*/ |
77
|
|
|
protected function constructObject(/*# string */ $class, array $args) |
78
|
|
|
{ |
79
|
|
|
$reflector = new \ReflectionClass($class); |
80
|
|
|
$constructor = $reflector->getConstructor(); |
81
|
|
|
|
82
|
|
|
// not constructor defined |
83
|
|
|
if (is_null($constructor)) { |
84
|
|
|
$obj = $reflector->newInstanceWithoutConstructor(); |
85
|
|
|
|
86
|
|
|
// normal class with constructor |
87
|
|
|
} else { |
88
|
|
|
$args = $this->matchArguments( |
89
|
|
|
$constructor->getParameters(), |
90
|
|
|
$args |
91
|
|
|
); |
92
|
|
|
$obj = $reflector->newInstanceArgs($args); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $obj; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Match provided arguments with a method/function's reflection parameters |
100
|
|
|
* |
101
|
|
|
* @param \ReflectionParameter[] $reflectionParameters |
102
|
|
|
* @param array $providedArguments |
103
|
|
|
* @return array the resolved arguments |
104
|
|
|
* @throws LogicException |
105
|
|
|
* @throws NotFoundException |
106
|
|
|
* @access protected |
107
|
|
|
*/ |
108
|
|
|
protected function matchArguments( |
109
|
|
|
array $reflectionParameters, |
110
|
|
|
array $providedArguments |
111
|
|
|
)/*# : array */ { |
112
|
|
|
// result |
113
|
|
|
$resolvedArguments = []; |
114
|
|
|
|
115
|
|
|
// go thru each predefined parameter |
116
|
|
|
foreach ($reflectionParameters as $i => $param) { |
117
|
|
|
// arg to match with |
118
|
|
|
$argument = isset($providedArguments[0]) ? $providedArguments[0] : null; |
119
|
|
|
|
120
|
|
|
// check the class |
121
|
|
|
$class = $param->getClass(); |
122
|
|
|
|
123
|
|
|
if ($this->isTypeMatched($param, $argument, $class)) { |
124
|
|
|
// type matched |
125
|
|
|
$resolvedArguments[$i] = array_shift($providedArguments); |
126
|
|
|
|
127
|
|
|
} elseif (null !== $class) { |
128
|
|
|
// not matched, but $param is an interface or class |
129
|
|
|
$resolvedArguments[$i] = $this->getObjectByClass($class->getName()); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return array_merge($resolvedArguments, $providedArguments); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Is $parameter same type as the $argument ? |
138
|
|
|
* |
139
|
|
|
* @param \ReflectionParameter $parameter |
140
|
|
|
* @param mixed $argument |
141
|
|
|
* @param null|string $class |
142
|
|
|
* @return bool |
143
|
|
|
* @access protected |
144
|
|
|
*/ |
145
|
|
|
protected function isTypeMatched( |
146
|
|
|
\ReflectionParameter $parameter, |
147
|
|
|
$argument, |
148
|
|
|
$class |
149
|
|
|
)/*# : bool */ { |
150
|
|
|
if (null === $argument) { |
151
|
|
|
return false; |
152
|
|
|
} elseif (null !== $class) { |
153
|
|
|
return is_a($argument, $parameter->getClass()->getName()); |
154
|
|
|
} else { |
155
|
|
|
return true; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get an object base on provided classname or interface name |
161
|
|
|
* |
162
|
|
|
* @param string $classname class or interface name |
163
|
|
|
* @return object |
164
|
|
|
* @throws \Exception if something goes wrong |
165
|
|
|
* @access protected |
166
|
|
|
*/ |
167
|
|
|
protected function getObjectByClass(/*# string */ $classname) |
168
|
|
|
{ |
169
|
|
|
// mapping exists |
170
|
|
|
if ($this->master->getResolver()->hasMapping($classname)) { |
171
|
|
|
$classname = $this->master->getResolver()->getMapping($classname); |
172
|
|
|
if (is_object($classname)) { |
173
|
|
|
return $classname; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
return $this->master->getResolver()->get($classname); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get callable parameters |
181
|
|
|
* |
182
|
|
|
* @param callable $callable |
183
|
|
|
* @return \ReflectionParameter[] |
184
|
|
|
* @throws LogicException if something goes wrong |
185
|
|
|
* @access protected |
186
|
|
|
*/ |
187
|
|
|
protected function getCallableParameters(callable $callable)/*# : array */ |
188
|
|
|
{ |
189
|
|
|
// array type |
190
|
|
|
if (is_array($callable)) { |
191
|
|
|
$reflector = new \ReflectionClass($callable[0]); |
192
|
|
|
$method = $reflector->getMethod($callable[1]); |
193
|
|
|
|
194
|
|
|
// object with __invoke() defined |
195
|
|
|
} elseif ($this->isInvocable($callable)) { |
196
|
|
|
$reflector = new \ReflectionClass($callable); |
197
|
|
|
$method = $reflector->getMethod('__invoke'); |
198
|
|
|
|
199
|
|
|
// simple function |
200
|
|
|
} else { |
201
|
|
|
$method = new \ReflectionFunction($callable); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $method->getParameters(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Is $var an object with '__invoke()' defined ? |
209
|
|
|
* |
210
|
|
|
* @param mixed $var |
211
|
|
|
* @return bool |
212
|
|
|
* @access protected |
213
|
|
|
*/ |
214
|
|
|
protected function isInvocable($var)/*# : bool */ |
215
|
|
|
{ |
216
|
|
|
return is_object($var) && method_exists($var, '__invoke'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Merge different sections of a node |
221
|
|
|
* |
222
|
|
|
* convert |
223
|
|
|
* `['section1' => [[1], [2]], 'section2' => [[3], [4]]]` |
224
|
|
|
* |
225
|
|
|
* to |
226
|
|
|
* `[[1], [2], [3], [4]]` |
227
|
|
|
* |
228
|
|
|
* @param array $nodeData |
229
|
|
|
* @return array |
230
|
|
|
* @access protected |
231
|
|
|
*/ |
232
|
|
|
protected function mergeMethods(array $nodeData)/*# : array */ |
233
|
|
|
{ |
234
|
|
|
// no merge |
235
|
|
|
if (empty($nodeData) || isset($nodeData[0])) { |
236
|
|
|
return $nodeData; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
// in sections |
240
|
|
|
$result = []; |
241
|
|
|
foreach ($nodeData as $data) { |
242
|
|
|
$result = array_merge($result, $data); |
243
|
|
|
} |
244
|
|
|
return $result; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Execute common methods defined in 'di.common' for all objects |
249
|
|
|
* |
250
|
|
|
* @param object $object |
251
|
|
|
* @return $this |
252
|
|
|
* @access protected |
253
|
|
|
*/ |
254
|
|
|
protected function executeCommonBatch($object) |
255
|
|
|
{ |
256
|
|
|
$methods = []; |
257
|
|
|
|
258
|
|
|
// get from 'di.common' |
259
|
|
|
if ($this->master->getResolver()->hasInSection('', 'common')) { |
260
|
|
|
$methods = $this->mergeMethods( |
261
|
|
|
$this->master->getResolver()->getInSection('', 'common') |
262
|
|
|
); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
foreach ($methods as $method) { |
266
|
|
|
if (call_user_func($method[0], $object, $this->master)) { |
267
|
|
|
call_user_func($method[1], $object, $this->master); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
return $this; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|