1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Newage\Annotations\Entity\Annotation; |
4
|
|
|
|
5
|
|
|
use Newage\Annotations\Config\AnnotationBuilderConfig; |
6
|
|
|
use Newage\Annotations\Mapper\Annotation\EntityAnnotationListener; |
7
|
|
|
use Newage\Annotations\Mapper\Annotation\PropertyAnnotationListener; |
8
|
|
|
use Newage\Annotations\Mapper\MapperBuilder; |
9
|
|
|
use Zend\Code\Annotation\AnnotationCollection; |
10
|
|
|
use Zend\Code\Annotation\AnnotationManager; |
11
|
|
|
use Zend\Code\Annotation\Parser; |
12
|
|
|
use Zend\Code\Reflection\ClassReflection; |
13
|
|
|
use Zend\Code\Reflection\PropertyReflection; |
14
|
|
|
use Zend\EventManager\EventManager; |
15
|
|
|
use Zend\EventManager\EventManagerAwareInterface; |
16
|
|
|
use Zend\EventManager\EventManagerInterface; |
17
|
|
|
use Zend\Stdlib\ArrayObject; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @package Newage\Annotations\Mapper\Annotation |
21
|
|
|
*/ |
22
|
|
|
class AnnotationBuilder implements EventManagerAwareInterface, AnnotationBuilderInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var MapperBuilder |
26
|
|
|
*/ |
27
|
|
|
protected $mapperBuilder; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Parser\DoctrineAnnotationParser |
31
|
|
|
*/ |
32
|
|
|
protected $annotationParser; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var AnnotationManager |
36
|
|
|
*/ |
37
|
|
|
protected $annotationManager; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var EventManagerInterface |
41
|
|
|
*/ |
42
|
|
|
protected $events; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var AnnotationBuilderConfig |
46
|
|
|
*/ |
47
|
|
|
private $config; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array Default annotations to register |
51
|
|
|
*/ |
52
|
|
|
protected $defaultAnnotations = [ |
53
|
|
|
'Table', |
54
|
|
|
'Id', |
55
|
|
|
'Column', |
56
|
|
|
'OneToOne', |
57
|
|
|
'OneToMany', |
58
|
|
|
'ManyToOne', |
59
|
|
|
'ManyToMany' |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* AnnotationBuilder constructor. |
64
|
|
|
* |
65
|
|
|
* @param AnnotationBuilderConfig $config |
66
|
|
|
* @param MapperBuilder $mapperBuilder |
67
|
|
|
*/ |
68
|
|
|
public function __construct(AnnotationBuilderConfig $config, MapperBuilder $mapperBuilder) |
69
|
|
|
{ |
70
|
|
|
$this->mapperBuilder = $mapperBuilder; |
71
|
|
|
$this->config = $config; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Create annotations from entity |
76
|
|
|
* @return ArrayObject |
77
|
|
|
*/ |
78
|
|
|
public function create() |
79
|
|
|
{ |
80
|
|
|
$spec = new ArrayObject(['entities' => []]); |
81
|
|
|
$factory = $this->mapperBuilder; |
82
|
|
|
$modelsOptions = $this->config->getConfig('models'); |
83
|
|
|
foreach ($modelsOptions as $modelOption) { |
84
|
|
|
foreach (new \FilesystemIterator($modelOption['path']) as $file) { |
85
|
|
|
$entityName = substr($file->getFileName(), 0, -4); |
86
|
|
|
$entityNamespace = '\\' . $modelOption['namespace'] . '\\' . $entityName; |
87
|
|
|
|
88
|
|
|
if (!class_exists($entityNamespace)) { |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$entity = new $entityNamespace(); |
93
|
|
|
$spec['entities'][] = $this->getSpecification($entity); |
94
|
|
|
} |
95
|
|
|
}; |
96
|
|
|
$factory->create($spec); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param $entity |
101
|
|
|
* @return ArrayObject |
102
|
|
|
* @internal param $spec |
103
|
|
|
*/ |
104
|
|
|
protected function getSpecification($entity) |
105
|
|
|
{ |
106
|
|
|
$annotationManager = $this->getAnnotationManager(); |
107
|
|
|
|
108
|
|
|
$spec = new ArrayObject(); |
109
|
|
|
$reflection = new ClassReflection($entity); |
110
|
|
|
$annotations = $reflection->getAnnotations($annotationManager); |
111
|
|
|
|
112
|
|
|
if ($annotations instanceof AnnotationCollection) { |
113
|
|
|
$this->configureEntity($annotations, $reflection, $spec); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
foreach ($reflection->getProperties() as $property) { |
117
|
|
|
$annotations = $property->getAnnotations($annotationManager); |
118
|
|
|
|
119
|
|
|
if ($annotations instanceof AnnotationCollection) { |
120
|
|
|
$this->configureProperty($annotations, $property, $spec); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $spec; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param AnnotationCollection $annotations |
129
|
|
|
* @param ClassReflection $reflection |
130
|
|
|
* @param ArrayObject $spec |
131
|
|
|
*/ |
132
|
|
|
protected function configureEntity($annotations, $reflection, $spec) |
133
|
|
|
{ |
134
|
|
|
$name = $reflection->getName(); |
135
|
|
|
$spec['entity'] = $name; |
136
|
|
|
|
137
|
|
|
$events = $this->getEventManager(); |
138
|
|
View Code Duplication |
foreach ($annotations as $annotation) { |
|
|
|
|
139
|
|
|
$events->trigger( |
140
|
|
|
__FUNCTION__, |
141
|
|
|
$this, |
142
|
|
|
[ |
143
|
|
|
'annotation' => $annotation, |
144
|
|
|
'spec' => $spec, |
145
|
|
|
'name' => $name |
146
|
|
|
] |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param AnnotationCollection $annotations |
153
|
|
|
* @param PropertyReflection $reflection |
154
|
|
|
* @param ArrayObject $spec |
155
|
|
|
*/ |
156
|
|
|
protected function configureProperty($annotations, $reflection, $spec) |
157
|
|
|
{ |
158
|
|
|
$name = $reflection->getName(); |
159
|
|
|
$propertySpec = new ArrayObject([ |
160
|
|
|
'property' => $name |
161
|
|
|
]); |
162
|
|
|
|
163
|
|
|
$events = $this->getEventManager(); |
164
|
|
View Code Duplication |
foreach ($annotations as $annotation) { |
|
|
|
|
165
|
|
|
$events->trigger( |
166
|
|
|
__FUNCTION__, |
167
|
|
|
$this, |
168
|
|
|
[ |
169
|
|
|
'annotation' => $annotation, |
170
|
|
|
'spec' => $propertySpec, |
171
|
|
|
'name' => $name |
172
|
|
|
] |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if (!isset($spec['properties'])) { |
177
|
|
|
$spec['properties'] = []; |
178
|
|
|
} |
179
|
|
|
$spec['properties'][] = $propertySpec; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Set event manager instance |
184
|
|
|
* |
185
|
|
|
* @param EventManagerInterface $events |
186
|
|
|
* @return AnnotationBuilder |
187
|
|
|
*/ |
188
|
|
|
public function setEventManager(EventManagerInterface $events) |
189
|
|
|
{ |
190
|
|
|
$events->setIdentifiers([ |
191
|
|
|
__CLASS__, |
192
|
|
|
get_class($this), |
193
|
|
|
]); |
194
|
|
|
$events->attach(new EntityAnnotationListener()); |
|
|
|
|
195
|
|
|
$events->attach(new PropertyAnnotationListener()); |
|
|
|
|
196
|
|
|
$this->events = $events; |
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get event manager |
202
|
|
|
* |
203
|
|
|
* @return EventManagerInterface |
204
|
|
|
*/ |
205
|
|
|
public function getEventManager() |
206
|
|
|
{ |
207
|
|
|
if (null === $this->events) { |
208
|
|
|
$this->setEventManager(new EventManager()); |
209
|
|
|
} |
210
|
|
|
return $this->events; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return mixed |
215
|
|
|
*/ |
216
|
|
|
public function getAnnotationManager() |
217
|
|
|
{ |
218
|
|
|
if (null === $this->annotationManager) { |
219
|
|
|
$this->setAnnotationManager(new AnnotationManager()); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this->annotationManager; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param mixed $annotationManager |
227
|
|
|
*/ |
228
|
|
|
public function setAnnotationManager(AnnotationManager $annotationManager) |
229
|
|
|
{ |
230
|
|
|
$parser = $this->getAnnotationParser(); |
231
|
|
|
foreach ($this->defaultAnnotations as $annotationName) { |
232
|
|
|
$class = __NAMESPACE__ . '\\' . $annotationName; |
233
|
|
|
$parser->registerAnnotation($class); |
234
|
|
|
} |
235
|
|
|
$annotationManager->attach($parser); |
236
|
|
|
$this->annotationManager = $annotationManager; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return \Zend\Code\Annotation\Parser\DoctrineAnnotationParser |
241
|
|
|
*/ |
242
|
|
|
public function getAnnotationParser() |
243
|
|
|
{ |
244
|
|
|
if (null === $this->annotationParser) { |
245
|
|
|
$this->annotationParser = new Parser\DoctrineAnnotationParser(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $this->annotationParser; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.