1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/old-town/workflow-zf2-service |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace OldTown\Workflow\ZF2\Service\Metadata\Reader; |
7
|
|
|
|
8
|
|
|
use OldTown\Workflow\ZF2\Service\Metadata\Storage\MetadataInterface; |
9
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
10
|
|
|
use Doctrine\Common\Annotations\Reader as DoctrineAnnotationsReaderInterface; |
11
|
|
|
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader; |
12
|
|
|
use OldTown\Workflow\ZF2\Service\Metadata\Storage\Metadata; |
13
|
|
|
use ReflectionClass; |
14
|
|
|
use OldTown\Workflow\ZF2\Service\Annotation; |
15
|
|
|
use OldTown\Workflow\ZF2\Service\Annotation\Map; |
16
|
|
|
use OldTown\Workflow\ZF2\Service\Metadata\Storage\ResultMapMetadata; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class AnnotationReader |
20
|
|
|
* |
21
|
|
|
* @package OldTown\Workflow\ZF2\Service\Metadata\Reader |
22
|
|
|
*/ |
23
|
|
|
class AnnotationReader implements ReaderInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
const READER_NAME = 'annotation'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var DoctrineAnnotationsReaderInterface |
32
|
|
|
*/ |
33
|
|
|
protected $reader; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Имя класса адаптера для чтения анотаций |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $doctrineAnnotationReaderClassName = DoctrineAnnotationReader::class; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Кеш загруженных метаданных |
44
|
|
|
* |
45
|
|
|
* @var MetadataInterface[] |
46
|
|
|
*/ |
47
|
|
|
protected $classAnnotations = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* |
51
|
|
|
* @throws Exception\AnnotationReaderException |
52
|
|
|
*/ |
53
|
|
|
public function __construct() |
54
|
|
|
{ |
55
|
|
|
$this->init(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Иницилазия |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
* @throws Exception\AnnotationReaderException |
63
|
|
|
*/ |
64
|
|
|
protected function init() |
65
|
|
|
{ |
66
|
|
|
try { |
67
|
|
|
AnnotationRegistry::registerLoader(function ($class) { |
68
|
|
|
return (bool) class_exists($class); |
69
|
|
|
}); |
70
|
|
|
} catch (\Exception $e) { |
71
|
|
|
throw new Exception\AnnotationReaderException($e->getMessage(), $e->getCode(), $e); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Получение метаданных для сервиса, по имени класса сервиса и вызываемому методу |
77
|
|
|
* |
78
|
|
|
* @param string $serviceClassName |
79
|
|
|
* @param string $serviceMethod |
80
|
|
|
* |
81
|
|
|
* @return MetadataInterface |
82
|
|
|
* |
83
|
|
|
* @throws Exception\AnnotationReaderException |
84
|
|
|
* |
85
|
|
|
* @throws \OldTown\Workflow\ZF2\Service\Metadata\Reader\Exception\InvalidDoctrineAnnotationsReaderException |
86
|
|
|
* @throws \OldTown\Workflow\ZF2\Service\Metadata\Storage\Exception\InvalidArgumentMapException |
87
|
|
|
* @throws Exception\InvalidMetadataException |
88
|
|
|
* @throws \OldTown\Workflow\ZF2\Service\Metadata\Storage\Exception\InvalidResultMapException |
89
|
|
|
*/ |
90
|
|
|
public function loadMetadataForClassService($serviceClassName, $serviceMethod) |
91
|
|
|
{ |
92
|
|
|
$key = $serviceClassName . '_' . $serviceMethod; |
93
|
|
|
if (array_key_exists($key, $this->classAnnotations)) { |
94
|
|
|
return $this->classAnnotations[$key]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$r = new ReflectionClass($serviceClassName); |
98
|
|
|
$rMethod = $r->getMethod($serviceMethod); |
99
|
|
|
|
100
|
|
|
$metadata = new Metadata(); |
101
|
|
|
|
102
|
|
|
/** @var Annotation\ArgumentsMap|null $argumentsMapAnnotation */ |
103
|
|
|
$argumentsMapAnnotation = $this->getReader()->getMethodAnnotation($rMethod, Annotation\ArgumentsMap::class); |
104
|
|
|
|
105
|
|
|
if (null !== $argumentsMapAnnotation && is_array($argumentsMapAnnotation->argumentsMap)) { |
106
|
|
|
foreach ($argumentsMapAnnotation->argumentsMap as $map) { |
107
|
|
|
if ($map instanceof Map) { |
108
|
|
|
$metadata->addArgumentMap($map->to, $map->fromArgName); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** @var Annotation\ResultVariable|null $resultVariableAnnotation */ |
115
|
|
|
$resultVariableAnnotation = $this->getReader()->getMethodAnnotation($rMethod, Annotation\ResultVariable::class); |
116
|
|
|
if (null !== $resultVariableAnnotation) { |
117
|
|
|
$metadata->setResultVariableName($resultVariableAnnotation->name); |
118
|
|
|
$metadata->setAllowOverrideResult($resultVariableAnnotation->override); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** @var Annotation\ResultsMap|null $resultsMapAnnotation */ |
122
|
|
|
$resultsMapAnnotation = $this->getReader()->getMethodAnnotation($rMethod, Annotation\ResultsMap::class); |
123
|
|
|
if (null !== $resultsMapAnnotation && is_array($resultsMapAnnotation->map) && count($resultsMapAnnotation->map) > 0) { |
124
|
|
|
foreach ($resultsMapAnnotation->map as $resultMap) { |
125
|
|
|
if (!$resultMap instanceof Annotation\ResultMap) { |
126
|
|
|
$errMsg = sprintf('Result map not implements %s', Annotation\ResultMap::class); |
127
|
|
|
throw new Exception\InvalidMetadataException($errMsg); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$resultMap = new ResultMapMetadata($resultMap->from, $resultMap->to, $resultMap->override); |
131
|
|
|
$metadata->addResultMap($resultMap); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $metadata; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return DoctrineAnnotationsReaderInterface |
140
|
|
|
* |
141
|
|
|
* @throws Exception\InvalidDoctrineAnnotationsReaderException |
142
|
|
|
*/ |
143
|
|
|
public function getReader() |
144
|
|
|
{ |
145
|
|
|
if ($this->reader) { |
146
|
|
|
return $this->reader; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$className = $this->getDoctrineAnnotationReaderClassName(); |
150
|
|
|
|
151
|
|
|
$r = new ReflectionClass($className); |
152
|
|
|
|
153
|
|
|
$instance = $r->newInstance(); |
154
|
|
|
|
155
|
|
|
if (!$instance instanceof DoctrineAnnotationsReaderInterface) { |
156
|
|
|
$errMsg = sprintf('Reader not implement %s', DoctrineAnnotationsReaderInterface::class); |
157
|
|
|
throw new Exception\InvalidDoctrineAnnotationsReaderException($errMsg); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$this->reader = $instance; |
161
|
|
|
|
162
|
|
|
return $this->reader; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param DoctrineAnnotationsReaderInterface $reader |
167
|
|
|
* |
168
|
|
|
* @return $this |
169
|
|
|
*/ |
170
|
|
|
public function setReader(DoctrineAnnotationsReaderInterface $reader) |
171
|
|
|
{ |
172
|
|
|
$this->reader = $reader; |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Имя класса адаптера для чтения анотаций |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function getDoctrineAnnotationReaderClassName() |
183
|
|
|
{ |
184
|
|
|
return $this->doctrineAnnotationReaderClassName; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Устанавливает имя класса адаптера для чтения анотаций |
189
|
|
|
* |
190
|
|
|
* @param string $doctrineAnnotationReaderClassName |
191
|
|
|
* |
192
|
|
|
* @return $this |
193
|
|
|
*/ |
194
|
|
|
public function setDoctrineAnnotationReaderClassName($doctrineAnnotationReaderClassName) |
195
|
|
|
{ |
196
|
|
|
$this->doctrineAnnotationReaderClassName = (string)$doctrineAnnotationReaderClassName; |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: