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\Metadata\Storage\ResultMapMetadata; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class AnnotationReader |
19
|
|
|
* |
20
|
|
|
* @package OldTown\Workflow\ZF2\Service\Metadata\Reader |
21
|
|
|
*/ |
22
|
|
|
class AnnotationReader implements ReaderInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
const READER_NAME = 'annotation'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var DoctrineAnnotationsReaderInterface |
31
|
|
|
*/ |
32
|
|
|
protected $reader; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Имя класса адаптера для чтения анотаций |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $doctrineAnnotationReaderClassName = DoctrineAnnotationReader::class; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Кеш загруженных метаданных |
43
|
|
|
* |
44
|
|
|
* @var MetadataInterface[] |
45
|
|
|
*/ |
46
|
|
|
protected $classAnnotations = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* @throws Exception\AnnotationReaderException |
51
|
|
|
*/ |
52
|
|
|
public function __construct() |
53
|
|
|
{ |
54
|
|
|
$this->init(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Иницилазия |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
* @throws Exception\AnnotationReaderException |
62
|
|
|
*/ |
63
|
|
|
protected function init() |
64
|
|
|
{ |
65
|
|
|
try { |
66
|
|
|
AnnotationRegistry::registerLoader(function ($class) { |
67
|
|
|
return (bool) class_exists($class); |
68
|
|
|
}); |
69
|
|
|
} catch (\Exception $e) { |
70
|
|
|
throw new Exception\AnnotationReaderException($e->getMessage(), $e->getCode(), $e); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Получение метаданных для сервиса, по имени класса сервиса и вызываемому методу |
76
|
|
|
* |
77
|
|
|
* @param string $serviceClassName |
78
|
|
|
* @param string $serviceMethod |
79
|
|
|
* |
80
|
|
|
* @return MetadataInterface |
81
|
|
|
* |
82
|
|
|
* @throws Exception\AnnotationReaderException |
83
|
|
|
* |
84
|
|
|
* @throws \OldTown\Workflow\ZF2\Service\Metadata\Reader\Exception\InvalidDoctrineAnnotationsReaderException |
85
|
|
|
* @throws \OldTown\Workflow\ZF2\Service\Metadata\Storage\Exception\InvalidArgumentMapException |
86
|
|
|
* @throws Exception\InvalidMetadataException |
87
|
|
|
* @throws \OldTown\Workflow\ZF2\Service\Metadata\Storage\Exception\InvalidResultMapException |
88
|
|
|
*/ |
89
|
|
|
public function loadMetadataForClassService($serviceClassName, $serviceMethod) |
90
|
|
|
{ |
91
|
|
|
$key = $serviceClassName . '_' . $serviceMethod; |
92
|
|
|
if (array_key_exists($key, $this->classAnnotations)) { |
93
|
|
|
return $this->classAnnotations[$key]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$r = new ReflectionClass($serviceClassName); |
97
|
|
|
$rMethod = $r->getMethod($serviceMethod); |
98
|
|
|
|
99
|
|
|
$metadata = new Metadata(); |
100
|
|
|
|
101
|
|
|
/** @var Annotation\ResultVariable|null $resultVariableAnnotation */ |
102
|
|
|
$resultVariableAnnotation = $this->getReader()->getMethodAnnotation($rMethod, Annotation\ResultVariable::class); |
103
|
|
|
if (null !== $resultVariableAnnotation) { |
104
|
|
|
$metadata->setResultVariableName($resultVariableAnnotation->name); |
105
|
|
|
$metadata->setAllowOverrideResult($resultVariableAnnotation->override); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** @var Annotation\ResultsMap|null $resultsMapAnnotation */ |
109
|
|
|
$resultsMapAnnotation = $this->getReader()->getMethodAnnotation($rMethod, Annotation\ResultsMap::class); |
110
|
|
|
if (null !== $resultsMapAnnotation && is_array($resultsMapAnnotation->map) && count($resultsMapAnnotation->map) > 0) { |
111
|
|
|
foreach ($resultsMapAnnotation->map as $resultMap) { |
112
|
|
|
if (!$resultMap instanceof Annotation\ResultMap) { |
113
|
|
|
$errMsg = sprintf('Result map not implements %s', Annotation\ResultMap::class); |
114
|
|
|
throw new Exception\InvalidMetadataException($errMsg); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$resultMap = new ResultMapMetadata($resultMap->from, $resultMap->to, $resultMap->override); |
118
|
|
|
$metadata->addResultMap($resultMap); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $metadata; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return DoctrineAnnotationsReaderInterface |
127
|
|
|
* |
128
|
|
|
* @throws Exception\InvalidDoctrineAnnotationsReaderException |
129
|
|
|
*/ |
130
|
|
|
public function getReader() |
131
|
|
|
{ |
132
|
|
|
if ($this->reader) { |
133
|
|
|
return $this->reader; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$className = $this->getDoctrineAnnotationReaderClassName(); |
137
|
|
|
|
138
|
|
|
$r = new ReflectionClass($className); |
139
|
|
|
|
140
|
|
|
$instance = $r->newInstance(); |
141
|
|
|
|
142
|
|
|
if (!$instance instanceof DoctrineAnnotationsReaderInterface) { |
143
|
|
|
$errMsg = sprintf('Reader not implement %s', DoctrineAnnotationsReaderInterface::class); |
144
|
|
|
throw new Exception\InvalidDoctrineAnnotationsReaderException($errMsg); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->reader = $instance; |
148
|
|
|
|
149
|
|
|
return $this->reader; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param DoctrineAnnotationsReaderInterface $reader |
154
|
|
|
* |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
|
|
public function setReader(DoctrineAnnotationsReaderInterface $reader) |
158
|
|
|
{ |
159
|
|
|
$this->reader = $reader; |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Имя класса адаптера для чтения анотаций |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function getDoctrineAnnotationReaderClassName() |
170
|
|
|
{ |
171
|
|
|
return $this->doctrineAnnotationReaderClassName; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Устанавливает имя класса адаптера для чтения анотаций |
176
|
|
|
* |
177
|
|
|
* @param string $doctrineAnnotationReaderClassName |
178
|
|
|
* |
179
|
|
|
* @return $this |
180
|
|
|
*/ |
181
|
|
|
public function setDoctrineAnnotationReaderClassName($doctrineAnnotationReaderClassName) |
182
|
|
|
{ |
183
|
|
|
$this->doctrineAnnotationReaderClassName = (string)$doctrineAnnotationReaderClassName; |
184
|
|
|
|
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
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: