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;
|
8
|
|
|
|
9
|
|
|
use samsonframework\container\definition\analyzer\exception\ParameterNotFoundException;
|
10
|
|
|
use samsonframework\container\definition\builder\DefinitionBuilder;
|
11
|
|
|
use samsonframework\container\definition\ClassDefinition;
|
12
|
|
|
|
13
|
|
|
/**
|
14
|
|
|
* Class DefinitionAnalyzer
|
15
|
|
|
* Fill metadata(definition)
|
16
|
|
|
*
|
17
|
|
|
* @author Ruslan Molodyko <[email protected]>
|
18
|
|
|
*/
|
19
|
|
|
class DefinitionAnalyzer
|
20
|
|
|
{
|
21
|
|
|
/** ClassAnalyzerInterface[] */
|
22
|
|
|
protected $classAnalyzers = [];
|
23
|
|
|
/** MethodAnalyzerInterface[] */
|
24
|
|
|
protected $methodAnalyzers = [];
|
25
|
|
|
/** PropertyAnalyzerInterface[] */
|
26
|
|
|
protected $propertyAnalyzers = [];
|
27
|
|
|
/** ParameterAnalyzerInterface[] */
|
28
|
|
|
protected $parameterAnalyzers = [];
|
29
|
|
|
|
30
|
|
|
/**
|
31
|
|
|
* Add class analyzer
|
32
|
|
|
*
|
33
|
|
|
* @param ClassAnalyzerInterface $classAnalyzer
|
34
|
|
|
* @return DefinitionAnalyzer
|
35
|
|
|
*/
|
36
|
5 |
|
public function addClassAnalyzer(ClassAnalyzerInterface $classAnalyzer): DefinitionAnalyzer
|
37
|
|
|
{
|
38
|
5 |
|
$this->classAnalyzers[] = $classAnalyzer;
|
39
|
|
|
|
40
|
5 |
|
return $this;
|
41
|
|
|
}
|
42
|
|
|
|
43
|
|
|
/**
|
44
|
|
|
* Add method analyzer
|
45
|
|
|
*
|
46
|
|
|
* @param MethodAnalyzerInterface $methodAnalyzer
|
47
|
|
|
* @return DefinitionAnalyzer
|
48
|
|
|
*/
|
49
|
5 |
|
public function addMethodAnalyzer(MethodAnalyzerInterface $methodAnalyzer): DefinitionAnalyzer
|
50
|
|
|
{
|
51
|
5 |
|
$this->methodAnalyzers[] = $methodAnalyzer;
|
52
|
|
|
|
53
|
5 |
|
return $this;
|
54
|
|
|
}
|
55
|
|
|
|
56
|
|
|
/**
|
57
|
|
|
* Add property analyzer
|
58
|
|
|
*
|
59
|
|
|
* @param PropertyAnalyzerInterface $propertyAnalyzer
|
60
|
|
|
* @return DefinitionAnalyzer
|
61
|
|
|
*/
|
62
|
5 |
|
public function addPropertyAnalyzer(PropertyAnalyzerInterface $propertyAnalyzer): DefinitionAnalyzer
|
63
|
|
|
{
|
64
|
5 |
|
$this->propertyAnalyzers[] = $propertyAnalyzer;
|
65
|
|
|
|
66
|
5 |
|
return $this;
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
/**
|
70
|
|
|
* Add parameter analyzer
|
71
|
|
|
*
|
72
|
|
|
* @param ParameterAnalyzerInterface $parameterAnalyzer
|
73
|
|
|
* @return DefinitionAnalyzer
|
74
|
|
|
*/
|
75
|
5 |
|
public function addParameterAnalyzer(ParameterAnalyzerInterface $parameterAnalyzer): DefinitionAnalyzer
|
76
|
|
|
{
|
77
|
5 |
|
$this->parameterAnalyzers[] = $parameterAnalyzer;
|
78
|
|
|
|
79
|
5 |
|
return $this;
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
|
/**
|
83
|
|
|
* Analyze definition builder
|
84
|
|
|
*
|
85
|
|
|
* @param DefinitionBuilder $definitionBuilder
|
86
|
|
|
* @throws ParameterNotFoundException
|
87
|
|
|
* @return bool
|
88
|
|
|
*/
|
89
|
7 |
|
public function analyze(DefinitionBuilder $definitionBuilder): bool
|
90
|
|
|
{
|
91
|
7 |
|
$isAnalyzed = false;
|
92
|
|
|
// Analyze class definitions
|
93
|
7 |
|
foreach ($definitionBuilder->getDefinitionCollection() as $classDefinition) {
|
94
|
|
|
// Analyze only not analyzed classes
|
95
|
7 |
|
if (!$classDefinition->isAnalyzed()) {
|
96
|
|
|
// Get reflection
|
97
|
7 |
|
$reflectionClass = new \ReflectionClass($classDefinition->getClassName());
|
98
|
|
|
|
99
|
|
|
// Analyze class
|
100
|
6 |
|
$this->analyzeClass($classDefinition, $reflectionClass);
|
101
|
|
|
// Analyze properties
|
102
|
6 |
|
$this->analyzeProperty($reflectionClass, $classDefinition);
|
103
|
|
|
// Analyze methods
|
104
|
6 |
|
$this->analyzeMethod($reflectionClass, $classDefinition);
|
105
|
|
|
|
106
|
|
|
// Class was analyzed
|
107
|
6 |
|
$classDefinition->setIsAnalyzed(true);
|
108
|
|
|
|
109
|
|
|
// Do not analyze this definition
|
110
|
6 |
|
$isAnalyzed = true;
|
111
|
|
|
}
|
112
|
|
|
}
|
113
|
|
|
|
114
|
6 |
|
return $isAnalyzed;
|
115
|
|
|
}
|
116
|
|
|
|
117
|
|
|
/**
|
118
|
|
|
* Analyze class
|
119
|
|
|
*
|
120
|
|
|
* @param ClassDefinition $classDefinition
|
121
|
|
|
* @param \ReflectionClass $reflectionClass
|
122
|
|
|
*/
|
123
|
6 |
|
protected function analyzeClass(ClassDefinition $classDefinition, \ReflectionClass $reflectionClass)
|
124
|
|
|
{
|
125
|
|
|
// Iterate analyzers
|
126
|
6 |
|
foreach ($this->classAnalyzers as $classAnalyzer) {
|
127
|
4 |
|
$classAnalyzer->analyze($this, $classDefinition, $reflectionClass);
|
128
|
|
|
}
|
129
|
6 |
|
}
|
130
|
|
|
|
131
|
|
|
/**
|
132
|
|
|
* Analyze method
|
133
|
|
|
*
|
134
|
|
|
* @param \ReflectionClass $reflectionClass
|
135
|
|
|
* @param ClassDefinition $classDefinition
|
136
|
|
|
* @throws ParameterNotFoundException
|
137
|
|
|
*/
|
138
|
6 |
|
protected function analyzeMethod(\ReflectionClass $reflectionClass, ClassDefinition $classDefinition)
|
139
|
|
|
{
|
140
|
|
|
// Analyze method definitions
|
141
|
6 |
|
foreach ($reflectionClass->getMethods() as $reflectionMethod) {
|
142
|
6 |
|
foreach ($this->methodAnalyzers as $methodAnalyzer) {
|
143
|
4 |
|
$methodAnalyzer->analyze($this, $classDefinition, $reflectionMethod);
|
144
|
|
|
}
|
145
|
6 |
|
$this->analyzeParameter($reflectionMethod, $classDefinition);
|
146
|
|
|
}
|
147
|
6 |
|
}
|
148
|
|
|
|
149
|
|
|
/**
|
150
|
|
|
* Analyze property
|
151
|
|
|
*
|
152
|
|
|
* @param \ReflectionClass $reflectionClass
|
153
|
|
|
* @param ClassDefinition $classDefinition
|
154
|
|
|
*/
|
155
|
6 |
|
protected function analyzeProperty(\ReflectionClass $reflectionClass, ClassDefinition $classDefinition)
|
156
|
|
|
{
|
157
|
|
|
// Iterate class properties
|
158
|
6 |
|
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
|
159
|
|
|
// Analyze property definition
|
160
|
6 |
|
foreach ($this->propertyAnalyzers as $propertyAnalyzer) {
|
161
|
6 |
|
$propertyAnalyzer->analyze($this, $classDefinition, $reflectionProperty);
|
162
|
|
|
}
|
163
|
|
|
}
|
164
|
6 |
|
}
|
165
|
|
|
|
166
|
|
|
/**
|
167
|
|
|
* Analyze parameter
|
168
|
|
|
*
|
169
|
|
|
* @param \ReflectionMethod $reflectionMethod
|
170
|
|
|
* @param ClassDefinition $classDefinition
|
171
|
|
|
* @throws ParameterNotFoundException
|
172
|
|
|
*/
|
173
|
6 |
|
protected function analyzeParameter(\ReflectionMethod $reflectionMethod, ClassDefinition $classDefinition) {
|
174
|
|
|
// Get methods parameters
|
175
|
6 |
|
foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
|
176
|
|
|
// Analyze parameters
|
177
|
6 |
|
foreach ($this->parameterAnalyzers as $parameterAnalyzer) {
|
178
|
6 |
|
$parameterAnalyzer->analyze($this, $classDefinition, $reflectionParameter);
|
179
|
|
|
}
|
180
|
|
|
}
|
181
|
6 |
|
}
|
182
|
|
|
}
|
183
|
|
|
|