1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\EasyExtendsBundle\Mapper; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\EventSubscriber; |
17
|
|
|
use Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs; |
18
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
19
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
20
|
|
|
|
21
|
|
|
class DoctrineORMMapper implements EventSubscriber |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $associations; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $discriminators; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $discriminatorColumns; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $inheritanceTypes; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var ManagerRegistry |
45
|
|
|
*/ |
46
|
|
|
protected $doctrine; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $indexes; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $uniques; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
protected $overrides; |
62
|
|
|
|
63
|
|
|
public function __construct(ManagerRegistry $doctrine, array $associations = [], array $indexes = [], array $discriminators = [], array $discriminatorColumns = [], array $inheritanceTypes = [], array $uniques = [], array $overrides = []) |
64
|
|
|
{ |
65
|
|
|
$this->doctrine = $doctrine; |
66
|
|
|
$this->associations = $associations; |
67
|
|
|
$this->indexes = $indexes; |
68
|
|
|
$this->uniques = $uniques; |
69
|
|
|
$this->discriminatorColumns = $discriminatorColumns; |
70
|
|
|
$this->discriminators = $discriminators; |
71
|
|
|
$this->inheritanceTypes = $inheritanceTypes; |
72
|
|
|
$this->overrides = $overrides; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getSubscribedEvents(): array |
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
|
|
'loadClassMetadata', |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function addAssociation(string $class, string $field, array $options): void |
83
|
|
|
{ |
84
|
|
|
if (!isset($this->associations[$class])) { |
85
|
|
|
$this->associations[$class] = []; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->associations[$class][$field] = $options; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Add a discriminator to a class. |
93
|
|
|
* |
94
|
|
|
* @param string $class The Class |
95
|
|
|
* @param string $key Key is the database value and values are the classes |
96
|
|
|
* @param string $discriminatorClass The mapped class |
97
|
|
|
*/ |
98
|
|
|
public function addDiscriminator(string $class, string $key, string $discriminatorClass): void |
99
|
|
|
{ |
100
|
|
|
if (!isset($this->discriminators[$class])) { |
101
|
|
|
$this->discriminators[$class] = []; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (!isset($this->discriminators[$class][$key])) { |
105
|
|
|
$this->discriminators[$class][$key] = $discriminatorClass; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function addDiscriminatorColumn(string $class, array $columnDef): void |
110
|
|
|
{ |
111
|
|
|
if (!isset($this->discriminatorColumns[$class])) { |
112
|
|
|
$this->discriminatorColumns[$class] = $columnDef; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function addInheritanceType(string $class, string $type): void |
117
|
|
|
{ |
118
|
|
|
if (!isset($this->inheritanceTypes[$class])) { |
119
|
|
|
$this->inheritanceTypes[$class] = $type; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function addIndex(string $class, string $name, array $columns): void |
124
|
|
|
{ |
125
|
|
|
if (!isset($this->indexes[$class])) { |
126
|
|
|
$this->indexes[$class] = []; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (isset($this->indexes[$class][$name])) { |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$this->indexes[$class][$name] = $columns; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function addUnique(string $class, string $name, array $columns): void |
137
|
|
|
{ |
138
|
|
|
if (!isset($this->uniques[$class])) { |
139
|
|
|
$this->uniques[$class] = []; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (isset($this->uniques[$class][$name])) { |
143
|
|
|
return; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->uniques[$class][$name] = $columns; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Adds new ORM override. |
151
|
|
|
*/ |
152
|
|
|
final public function addOverride(string $class, string $type, array $options): void |
153
|
|
|
{ |
154
|
|
|
if (!isset($this->overrides[$class])) { |
155
|
|
|
$this->overrides[$class] = []; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$this->overrides[$class][$type] = $options; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void |
162
|
|
|
{ |
163
|
|
|
$metadata = $eventArgs->getClassMetadata(); |
164
|
|
|
|
165
|
|
|
$this->loadAssociations($metadata); |
|
|
|
|
166
|
|
|
$this->loadIndexes($metadata); |
|
|
|
|
167
|
|
|
$this->loadUniques($metadata); |
|
|
|
|
168
|
|
|
|
169
|
|
|
$this->loadDiscriminatorColumns($metadata); |
|
|
|
|
170
|
|
|
$this->loadDiscriminators($metadata); |
|
|
|
|
171
|
|
|
$this->loadInheritanceTypes($metadata); |
|
|
|
|
172
|
|
|
$this->loadOverrides($metadata); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @throws \RuntimeException |
177
|
|
|
*/ |
178
|
|
|
private function loadAssociations(ClassMetadataInfo $metadata): void |
179
|
|
|
{ |
180
|
|
|
if (!\array_key_exists($metadata->name, $this->associations)) { |
181
|
|
|
return; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
try { |
185
|
|
|
foreach ($this->associations[$metadata->name] as $type => $mappings) { |
186
|
|
|
foreach ($mappings as $mapping) { |
187
|
|
|
// the association is already set, skip the native one |
188
|
|
|
if ($metadata->hasAssociation($mapping['fieldName'])) { |
189
|
|
|
continue; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$metadata->$type($mapping); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} catch (\ReflectionException $e) { |
196
|
|
|
throw new \RuntimeException(sprintf( |
197
|
|
|
'Error with class %s : %s', |
198
|
|
|
$metadata->name, |
199
|
|
|
$e->getMessage() |
200
|
|
|
), 404, $e); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @throws \RuntimeException |
206
|
|
|
*/ |
207
|
|
|
private function loadDiscriminatorColumns(ClassMetadataInfo $metadata): void |
208
|
|
|
{ |
209
|
|
|
if (!\array_key_exists($metadata->name, $this->discriminatorColumns)) { |
210
|
|
|
return; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
try { |
214
|
|
|
if (isset($this->discriminatorColumns[$metadata->name])) { |
215
|
|
|
$arrayDiscriminatorColumns = $this->discriminatorColumns[$metadata->name]; |
216
|
|
|
if (isset($metadata->discriminatorColumn)) { |
217
|
|
|
$arrayDiscriminatorColumns = array_merge( |
218
|
|
|
$metadata->discriminatorColumn, |
219
|
|
|
$this->discriminatorColumns[$metadata->name] |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
$metadata->setDiscriminatorColumn($arrayDiscriminatorColumns); |
223
|
|
|
} |
224
|
|
|
} catch (\ReflectionException $e) { |
225
|
|
|
throw new \RuntimeException(sprintf( |
226
|
|
|
'Error with class %s : %s', |
227
|
|
|
$metadata->name, |
228
|
|
|
$e->getMessage() |
229
|
|
|
), 404, $e); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @throws \RuntimeException |
235
|
|
|
*/ |
236
|
|
|
private function loadInheritanceTypes(ClassMetadataInfo $metadata): void |
237
|
|
|
{ |
238
|
|
|
if (!\array_key_exists($metadata->name, $this->inheritanceTypes)) { |
239
|
|
|
return; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
try { |
243
|
|
|
if (isset($this->inheritanceTypes[$metadata->name])) { |
244
|
|
|
$metadata->setInheritanceType($this->inheritanceTypes[$metadata->name]); |
245
|
|
|
} |
246
|
|
|
} catch (\ReflectionException $e) { |
247
|
|
|
throw new \RuntimeException(sprintf( |
248
|
|
|
'Error with class %s : %s', |
249
|
|
|
$metadata->name, |
250
|
|
|
$e->getMessage() |
251
|
|
|
), 404, $e); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @throws \RuntimeException |
257
|
|
|
*/ |
258
|
|
|
private function loadDiscriminators(ClassMetadataInfo $metadata): void |
259
|
|
|
{ |
260
|
|
|
if (!\array_key_exists($metadata->name, $this->discriminators)) { |
261
|
|
|
return; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
try { |
265
|
|
|
foreach ($this->discriminators[$metadata->name] as $key => $class) { |
266
|
|
|
if (\in_array($key, $metadata->discriminatorMap, true)) { |
267
|
|
|
continue; |
268
|
|
|
} |
269
|
|
|
$metadata->setDiscriminatorMap([$key => $class]); |
270
|
|
|
} |
271
|
|
|
} catch (\ReflectionException $e) { |
272
|
|
|
throw new \RuntimeException(sprintf( |
273
|
|
|
'Error with class %s : %s', |
274
|
|
|
$metadata->name, |
275
|
|
|
$e->getMessage() |
276
|
|
|
), 404, $e); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
private function loadIndexes(ClassMetadataInfo $metadata): void |
281
|
|
|
{ |
282
|
|
|
if (!\array_key_exists($metadata->name, $this->indexes)) { |
283
|
|
|
return; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
foreach ($this->indexes[$metadata->name] as $name => $columns) { |
287
|
|
|
$metadata->table['indexes'][$name] = ['columns' => $columns]; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
private function loadUniques(ClassMetadataInfo $metadata): void |
292
|
|
|
{ |
293
|
|
|
if (!\array_key_exists($metadata->name, $this->uniques)) { |
294
|
|
|
return; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
foreach ($this->uniques[$metadata->name] as $name => $columns) { |
298
|
|
|
$metadata->table['uniqueConstraints'][$name] = ['columns' => $columns]; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @throws \RuntimeException |
304
|
|
|
*/ |
305
|
|
|
private function loadOverrides(ClassMetadataInfo $metadata): void |
306
|
|
|
{ |
307
|
|
|
if (!\array_key_exists($metadata->name, $this->overrides)) { |
308
|
|
|
return; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
try { |
312
|
|
|
foreach ($this->overrides[$metadata->name] as $type => $overrides) { |
313
|
|
|
foreach ($overrides as $override) { |
314
|
|
|
$metadata->$type($override['fieldName'], $override); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
} catch (\ReflectionException $e) { |
318
|
|
|
throw new \RuntimeException(sprintf( |
319
|
|
|
'Error with class %s : %s', |
320
|
|
|
$metadata->name, |
321
|
|
|
$e->getMessage() |
322
|
|
|
), 404, $e); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.