This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
/
RuntimeDefinition.php
345 lines (287 loc) · 9.73 KB
/
RuntimeDefinition.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
namespace Zend\Di\Definition;
use Zend\Di\Definition\Annotation,
Zend\Code\Annotation\AnnotationManager,
Zend\Code\Annotation\AnnotationCollection,
Zend\Code\Reflection;
class RuntimeDefinition implements Definition
{
/**
* @var array
*/
protected $classes = array();
/**
* @var bool
*/
protected $explicitLookups = false;
/**
* @var IntrospectionStrategy
*/
protected $introspectionStrategy = null;
/**
* @var array
*/
protected $injectionMethods = array();
/**
*
*/
public function __construct(IntrospectionStrategy $introspectionStrategy = null, array $explicitClasses = null)
{
$this->introspectionStrategy = ($introspectionStrategy) ?: new IntrospectionStrategy();
if ($explicitClasses) {
$this->setExplicitClasses($explicitClasses);
}
}
/**
* @param IntrospectionStrategy $introspectionStrategy
* @return void
*/
public function setIntrospectionStrategy(IntrospectionStrategy $introspectionStrategy)
{
$this->introspectionStrategy = $introspectionStrategy;
}
/**
* @return IntrospectionStrategy
*/
public function getIntrospectionStrategy()
{
return $this->introspectionStrategy;
}
public function setExplicitClasses(array $explicitClasses)
{
$this->explicitLookups = true;
foreach ($explicitClasses as $eClass) {
$this->classes[$eClass] = true;
}
$this->classes = $explicitClasses;
}
public function forceLoadClass($class)
{
$this->processClass($class);
}
/**
* Return nothing
*
* @return array
*/
public function getClasses()
{
return array_keys($this->classes);
}
/**
* Return whether the class exists
*
* @param string $class
* @return bool
*/
public function hasClass($class)
{
if ($this->explicitLookups === true) {
return (array_key_exists($class, $this->classes));
}
return class_exists($class) || interface_exists($class);
}
/**
* Return the supertypes for this class
*
* @param string $class
* @return array of types
*/
public function getClassSupertypes($class)
{
if (!array_key_exists($class, $this->classes)) {
$this->processClass($class);
}
return $this->classes[$class]['supertypes'];
}
/**
* Get the instantiator
*
* @param string $class
* @return string|callable
*/
public function getInstantiator($class)
{
if (!array_key_exists($class, $this->classes)) {
$this->processClass($class);
}
return $this->classes[$class]['instantiator'];
}
/**
* Return if there are injection methods
*
* @param string $class
* @return bool
*/
public function hasMethods($class)
{
if (!array_key_exists($class, $this->classes)) {
$this->processClass($class);
}
return (count($this->classes[$class]['methods']) > 0);
}
/**
* Return injection methods
*
* @param string $class
* @param string $method
* @return bool
*/
public function hasMethod($class, $method)
{
if (!array_key_exists($class, $this->classes)) {
$this->processClass($class);
}
return isset($this->classes[$class]['methods'][$method]);
}
/**
* Return an array of the injection methods
*
* @param string $class
* @return array
*/
public function getMethods($class)
{
if (!array_key_exists($class, $this->classes)) {
$this->processClass($class);
}
return $this->classes[$class]['methods'];
}
public function hasMethodParameters($class, $method)
{
if (!isset($this->classes[$class])) {
return false;
}
return (array_key_exists($method, $this->classes[$class]['parameters']));
}
/**
* Return the parameters for a method
*
* 3 item array:
* #1 - Class name, string if it exists, else null
* #2 - Optional?, boolean
* #3 - Instantiable, boolean if class exists, otherwise null
*
* @param string $class
* @param string $method
* @return array
*/
public function getMethodParameters($class, $method)
{
if (!is_array($this->classes[$class])) {
$this->processClass($class);
}
return $this->classes[$class]['parameters'][$method];
}
protected function processClass($class)
{
$strategy = $this->introspectionStrategy; // localize for readability
/** @var $rClass \Zend\Code\Reflection\ClassReflection */
$rClass = new Reflection\ClassReflection($class);
$className = $rClass->getName();
$matches = null; // used for regex below
// setup the key in classes
$this->classes[$className] = array(
'supertypes' => array(),
'instantiator' => null,
'methods' => array(),
'parameters' => array()
);
$def = &$this->classes[$className]; // localize for brevity
// class annotations?
if ($strategy->getUseAnnotations() == true) {
$annotations = $rClass->getAnnotations($strategy->getAnnotationManager());
if (($annotations instanceof AnnotationCollection)
&& $annotations->hasAnnotation('Zend\Di\Definition\Annotation\Instantiator')) {
// @todo Instnatiator support in annotations
}
}
$rTarget = $rClass;
$supertypes = array();
do {
$supertypes = array_merge($supertypes, $rTarget->getInterfaceNames());
if (!($rTargetParent = $rTarget->getParentClass())) {
break;
}
$supertypes[] = $rTargetParent->getName();
$rTarget = $rTargetParent;
} while (true);
$def['supertypes'] = $supertypes;
if ($def['instantiator'] == null) {
if ($rClass->isInstantiable()) {
$def['instantiator'] = '__construct';
}
}
if ($rClass->hasMethod('__construct')) {
$def['methods']['__construct'] = true; // required
$this->processParams($def, $rClass, $rClass->getMethod('__construct'));
}
foreach ($rClass->getMethods(Reflection\MethodReflection::IS_PUBLIC) as $rMethod) {
$methodName = $rMethod->getName();
if ($rMethod->getName() === '__construct') {
continue;
}
if ($strategy->getUseAnnotations() == true) {
$annotations = $rMethod->getAnnotations($strategy->getAnnotationManager());
if (($annotations instanceof AnnotationCollection)
&& $annotations->hasAnnotation('Zend\Di\Definition\Annotation\Inject')) {
$def['methods'][$methodName] = true;
$this->processParams($def, $rClass, $rMethod);
continue;
}
}
$methodPatterns = $this->introspectionStrategy->getMethodNameInclusionPatterns();
// matches a method injection pattern?
foreach ($methodPatterns as $methodInjectorPattern) {
preg_match($methodInjectorPattern, $methodName, $matches);
if ($matches) {
$def['methods'][$methodName] = false; // check ot see if this is required?
$this->processParams($def, $rClass, $rMethod);
continue 2;
}
}
// method
// by annotation
// by setter pattern,
// by interface
}
$interfaceInjectorPatterns = $this->introspectionStrategy->getInterfaceInjectionInclusionPatterns();
// matches the interface injection pattern
/** @var $rIface \ReflectionClass */
foreach ($rClass->getInterfaces() as $rIface) {
foreach ($interfaceInjectorPatterns as $interfaceInjectorPattern) {
preg_match($interfaceInjectorPattern, $rIface->getName(), $matches);
if ($matches) {
foreach ($rIface->getMethods() as $rMethod) {
if ($rMethod->getName() === '__construct') { // ctor not allowed in ifaces
continue;
}
$def['methods'][$rMethod->getName()] = true;
$this->processParams($def, $rClass, $rMethod);
}
continue 2;
}
}
}
//var_dump($this->classes);
}
protected function processParams(&$def, Reflection\ClassReflection $rClass, Reflection\MethodReflection $rMethod)
{
if (count($rMethod->getParameters()) === 0) {
return;
}
$methodName = $rMethod->getName();
// @todo annotations here for alternate names?
$def['parameters'][$methodName] = array();
foreach ($rMethod->getParameters() as $p) {
/** @var $p \ReflectionParameter */
$actualParamName = $p->getName();
$fqName = $rClass->getName() . '::' . $rMethod->getName() . ':' . $p->getPosition();
$def['parameters'][$methodName][$fqName] = array();
// set the class name, if it exists
$def['parameters'][$methodName][$fqName][] = $actualParamName;
$def['parameters'][$methodName][$fqName][] = ($p->getClass() !== null) ? $p->getClass()->getName() : null;
$def['parameters'][$methodName][$fqName][] = !$p->isOptional();
}
}
}