|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Ruslan Molodyko. |
|
4
|
|
|
* Date: 10.09.2016 |
|
5
|
|
|
* Time: 15:33 |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace samsonframework\container\definition\analyzer\reflection; |
|
8
|
|
|
|
|
9
|
|
|
use samsonframework\container\definition\analyzer\DefinitionAnalyzer; |
|
10
|
|
|
use samsonframework\container\definition\analyzer\ParameterAnalyzerInterface; |
|
11
|
|
|
use samsonframework\container\definition\builder\exception\ReferenceNotImplementsException; |
|
12
|
|
|
use samsonframework\container\definition\ClassDefinition; |
|
13
|
|
|
use samsonframework\container\definition\exception\ParameterDefinitionAlreadyExistsException; |
|
14
|
|
|
use samsonframework\container\definition\MethodDefinition; |
|
15
|
|
|
use samsonframework\container\definition\ParameterDefinition; |
|
16
|
|
|
use samsonframework\container\definition\reference\ClassReference; |
|
17
|
|
|
use samsonframework\container\definition\reference\CollectionReference; |
|
18
|
|
|
use samsonframework\container\definition\reference\ConstantReference; |
|
19
|
|
|
use samsonframework\container\definition\reference\UndefinedReference; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class ReflectionParameterAnalyzer |
|
23
|
|
|
* |
|
24
|
|
|
* @author Ruslan Molodyko <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class ReflectionParameterAnalyzer implements ParameterAnalyzerInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
* @throws ParameterDefinitionAlreadyExistsException |
|
31
|
|
|
* @throws ReferenceNotImplementsException |
|
32
|
|
|
*/ |
|
33
|
6 |
|
public function analyze( |
|
34
|
|
|
DefinitionAnalyzer $analyzer, |
|
35
|
|
|
\ReflectionParameter $reflectionParameter, |
|
36
|
|
|
ClassDefinition $classDefinition, |
|
37
|
|
|
MethodDefinition $methodDefinition = null, |
|
38
|
|
|
ParameterDefinition $parameterDefinition = null |
|
39
|
|
|
) { |
|
40
|
|
|
// Define parameter only if method definition is available |
|
41
|
6 |
|
if ($methodDefinition) { |
|
42
|
|
|
// Define parameter definition if not exists |
|
43
|
6 |
|
if (!$parameterDefinition) { |
|
44
|
2 |
|
$parameterDefinition = $methodDefinition->defineParameter($reflectionParameter->getName()); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
// Set parameter metadata |
|
47
|
6 |
|
if ($reflectionParameter->isDefaultValueAvailable()) { |
|
48
|
3 |
|
$parameterDefinition->setValue($reflectionParameter->getDefaultValue()); |
|
49
|
|
|
} |
|
50
|
6 |
|
if ($reflectionParameter->getType()) { |
|
51
|
6 |
|
$parameterDefinition->setTypeHint($reflectionParameter->getType()); |
|
52
|
|
|
} |
|
53
|
6 |
|
$parameterDefinition->setIsOptional($reflectionParameter->isOptional()); |
|
54
|
|
|
|
|
55
|
6 |
|
$dependency = $parameterDefinition->getDependency(); |
|
56
|
|
|
// If dependency was not set |
|
57
|
6 |
|
if (!$dependency || ($dependency instanceof UndefinedReference)) { |
|
58
|
|
|
// If default value available |
|
59
|
3 |
|
if ($reflectionParameter->isDefaultValueAvailable()) { |
|
60
|
|
|
// There is a constant |
|
61
|
3 |
|
if ($reflectionParameter->isDefaultValueConstant()) { |
|
62
|
|
|
$parameterDefinition->setDependency( |
|
63
|
|
|
new ConstantReference($reflectionParameter->getDefaultValueConstantName()) |
|
64
|
|
|
); |
|
65
|
|
|
} else { // There is some php types |
|
66
|
3 |
|
$parameterDefinition->setDependency( |
|
67
|
3 |
|
CollectionReference::convertValueToReference($reflectionParameter->getDefaultValue()) |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
// There is class dependency |
|
71
|
|
|
} elseif ( |
|
72
|
2 |
|
is_object($parameterDefinition->getTypeHint()) && |
|
73
|
2 |
|
(string)$parameterDefinition->getTypeHint() !== '' && |
|
74
|
2 |
|
!$parameterDefinition->getTypeHint()->isBuiltin() |
|
75
|
|
|
) { |
|
76
|
2 |
|
$parameterDefinition->setDependency( |
|
77
|
2 |
|
new ClassReference((string)$parameterDefinition->getTypeHint()) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
6 |
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|