1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Doctrine\Mapping\Driver; |
4
|
|
|
|
5
|
|
|
use Bankiru\Api\Doctrine\Exception\MappingException; |
6
|
|
|
use Bankiru\Api\Doctrine\Mapping\ApiMetadata; |
7
|
|
|
use Bankiru\Api\Doctrine\Mapping\EntityMetadata; |
8
|
|
|
use Bankiru\Api\Doctrine\Rpc\Method\EntityMethodProvider; |
9
|
|
|
use Bankiru\Api\Doctrine\Rpc\Method\MethodProvider; |
10
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
11
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\FileDriver; |
12
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
13
|
|
|
use Symfony\Component\Yaml\Yaml; |
14
|
|
|
|
15
|
|
|
class YmlMetadataDriver extends FileDriver |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Loads the metadata for the specified class into the provided container. |
19
|
|
|
* |
20
|
|
|
* @param string $className |
21
|
|
|
* @param EntityMetadata|ClassMetadata $metadata |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
* @throws MappingException |
25
|
|
|
*/ |
26
|
28 |
|
public function loadMetadataForClass($className, ClassMetadata $metadata) |
27
|
|
|
{ |
28
|
28 |
|
$element = $this->getElement($className); |
29
|
|
|
|
30
|
28 |
|
switch ($element['type']) { |
31
|
28 |
|
case 'entity': |
32
|
28 |
|
if (array_key_exists('repositoryClass', $element)) { |
33
|
2 |
|
$metadata->setCustomRepositoryClass($element['repositoryClass']); |
34
|
2 |
|
} |
35
|
28 |
|
break; |
36
|
|
|
case 'mappedSuperclass': |
37
|
|
|
$metadata->isMappedSuperclass = true; |
|
|
|
|
38
|
|
|
$metadata->setCustomRepositoryClass( |
39
|
|
|
array_key_exists('repositoryClass', $element) ? $element['repositoryClass'] : null |
40
|
|
|
); |
41
|
|
|
break; |
42
|
28 |
|
} |
43
|
|
|
|
44
|
|
|
// Configure API |
45
|
28 |
|
if (array_key_exists('api', $element)) { |
46
|
28 |
|
if (array_key_exists('factory', $element['api'])) { |
47
|
28 |
|
$metadata->apiFactory = $element['api']['factory']; |
|
|
|
|
48
|
28 |
|
} |
49
|
28 |
|
} |
50
|
|
|
|
51
|
|
|
// Evaluate discriminatorColumn |
52
|
28 |
|
if (isset($element['discriminatorField'])) { |
53
|
4 |
|
$discrColumn = $element['discriminatorField']; |
54
|
4 |
|
$metadata->setDiscriminatorField( |
55
|
|
|
[ |
56
|
4 |
|
'name' => isset($discrColumn['name']) ? (string)$discrColumn['name'] : null, |
57
|
4 |
|
'type' => isset($discrColumn['type']) ? (string)$discrColumn['type'] : 'string', |
58
|
|
|
] |
59
|
4 |
|
); |
60
|
4 |
|
} else { |
|
|
|
|
61
|
|
|
// $metadata->setDiscriminatorField(['name' => 'dtype', 'type' => 'string']); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
// Evaluate discriminatorMap |
64
|
28 |
|
if (isset($element['discriminatorMap'])) { |
65
|
|
|
$metadata->setDiscriminatorMap($element['discriminatorMap']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Configure Client |
69
|
28 |
|
if (array_key_exists('client', $element)) { |
70
|
28 |
|
if (array_key_exists('name', $element['client'])) { |
71
|
28 |
|
$metadata->clientName = $element['client']['name']; |
|
|
|
|
72
|
28 |
|
} |
73
|
|
|
|
74
|
28 |
|
$methodProvider = null; |
75
|
28 |
|
if (array_key_exists('methods', $element['client'])) { |
76
|
|
|
$methodProvider = new MethodProvider($element['client']['methods']); |
77
|
1 |
|
} |
78
|
28 |
|
if (array_key_exists('entityPath', $element['client'])) { |
79
|
|
|
$pathSeparator = |
80
|
28 |
|
array_key_exists('entityPathSeparator', $element['client']) ? |
81
|
28 |
|
$element['client']['entityPathSeparator'] : |
82
|
28 |
|
EntityMethodProvider::DEFAULT_PATH_SEPARATOR; |
83
|
|
|
$methodProvider = |
84
|
28 |
|
new EntityMethodProvider($element['client']['entityPath'], $pathSeparator, $methodProvider); |
85
|
28 |
|
} |
86
|
|
|
|
87
|
28 |
|
if (null === $methodProvider && null === $metadata->methodProvider) { |
|
|
|
|
88
|
|
|
throw MappingException::noMethods(); |
89
|
|
|
} |
90
|
|
|
|
91
|
28 |
|
if (null !== $methodProvider) { |
92
|
28 |
|
$metadata->methodProvider = $methodProvider; |
|
|
|
|
93
|
28 |
|
} |
94
|
28 |
|
} |
95
|
|
|
|
96
|
|
|
// Configure fields |
97
|
28 |
|
if (array_key_exists('fields', $element)) { |
98
|
28 |
|
foreach ($element['fields'] as $field => $mapping) { |
99
|
28 |
|
$mapping = $this->fieldToArray($field, $mapping); |
100
|
28 |
|
$metadata->mapField($mapping); |
101
|
28 |
|
} |
102
|
28 |
|
} |
103
|
|
|
|
104
|
|
|
// Configure identifiers |
105
|
28 |
|
$associationIds = []; |
106
|
28 |
|
if (array_key_exists('id', $element)) { |
107
|
|
|
// Evaluate identifier settings |
108
|
28 |
|
$defaults = ['generator' => ['strategy' => 'NATURAL']]; |
109
|
28 |
|
foreach ($element['id'] as $name => $idElement) { |
110
|
28 |
|
if (isset($idElement['associationKey']) && (bool)$idElement['associationKey'] === true) { |
111
|
|
|
$associationIds[$name] = true; |
112
|
|
|
continue; |
113
|
|
|
} |
114
|
|
|
|
115
|
28 |
|
$mapping = $this->fieldToArray($name, $idElement); |
116
|
|
|
|
117
|
28 |
|
$mapping['id'] = true; |
118
|
28 |
|
$idElement = array_replace_recursive($defaults, $idElement); |
119
|
|
|
|
120
|
28 |
|
$mapping['generator']['strategy'] = |
121
|
28 |
|
constant(ApiMetadata::class . '::GENERATOR_TYPE_' . $idElement['generator']['strategy']); |
122
|
|
|
|
123
|
28 |
|
$metadata->mapIdentifier($mapping); |
124
|
28 |
|
} |
125
|
28 |
|
} |
126
|
|
|
|
127
|
28 |
|
foreach (['oneToOne', 'manyToOne', 'oneToMany', 'manyToMany'] as $type) { |
128
|
28 |
|
if (array_key_exists($type, $element)) { |
129
|
18 |
|
$associations = $element[$type]; |
130
|
18 |
|
foreach ($associations as $name => $association) { |
131
|
18 |
|
$this->mapAssociation($metadata, $type, $name, $association, $associationIds); |
|
|
|
|
132
|
18 |
|
} |
133
|
18 |
|
} |
134
|
28 |
|
} |
135
|
28 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param EntityMetadata $metadata |
139
|
|
|
* @param string $type |
140
|
|
|
* @param string $name |
141
|
|
|
* @param array $association |
142
|
|
|
* @param int[] $associationIds |
143
|
|
|
*/ |
144
|
18 |
|
protected function mapAssociation(EntityMetadata $metadata, $type, $name, $association, $associationIds) |
145
|
|
|
{ |
146
|
18 |
|
$mapping = $this->fieldToArray($name, $association); |
147
|
18 |
|
$mapping['target'] = $association['target']; |
148
|
18 |
|
$mapping['sourceEntity'] = $metadata->getName(); |
149
|
18 |
|
if (isset($association['fetch'])) { |
150
|
|
|
$mapping['fetch'] = constant(ApiMetadata::class . '::FETCH_' . $association['fetch']); |
151
|
|
|
} |
152
|
|
|
switch ($type) { |
153
|
18 |
|
case 'oneToOne': |
154
|
|
|
$mapping['type'] = EntityMetadata::ONE_TO_ONE; |
155
|
|
|
if (isset($associationIds[$mapping['field']])) { |
156
|
|
|
$mapping['id'] = true; |
157
|
|
|
} |
158
|
|
|
if (array_key_exists('mappedBy', $association)) { |
159
|
|
|
$mapping['mappedBy'] = $association['mappedBy']; |
160
|
|
|
} |
161
|
|
|
if (array_key_exists('inversedBy', $association)) { |
162
|
|
|
$mapping['inversedBy'] = $association['inversedBy']; |
163
|
|
|
} |
164
|
|
|
$metadata->mapOneToOne($mapping); |
165
|
|
|
break; |
166
|
18 |
|
case 'manyToOne': |
167
|
17 |
|
$mapping['type'] = EntityMetadata::MANY_TO_ONE; |
168
|
17 |
|
if (array_key_exists('inversedBy', $association)) { |
169
|
17 |
|
$mapping['inversedBy'] = $association['inversedBy']; |
170
|
17 |
|
} |
171
|
17 |
|
$metadata->mapManyToOne($mapping); |
172
|
17 |
|
break; |
173
|
18 |
View Code Duplication |
case 'oneToMany': |
|
|
|
|
174
|
17 |
|
$mapping['type'] = EntityMetadata::ONE_TO_MANY; |
175
|
17 |
|
if (array_key_exists('mappedBy', $association)) { |
176
|
17 |
|
$mapping['mappedBy'] = $association['mappedBy']; |
177
|
17 |
|
} |
178
|
17 |
|
if (array_key_exists('orderBy', $association)) { |
179
|
|
|
$mapping['orderBy'] = $association['orderBy']; |
180
|
|
|
} |
181
|
17 |
|
if (array_key_exists('indexBy', $association)) { |
182
|
|
|
$mapping['indexBy'] = $association['indexBy']; |
183
|
|
|
} |
184
|
17 |
|
$metadata->mapOneToMany($mapping); |
185
|
17 |
|
break; |
186
|
1 |
View Code Duplication |
case 'manyToMany': |
|
|
|
|
187
|
1 |
|
$mapping['type'] = EntityMetadata::MANY_TO_MANY; |
188
|
1 |
|
if (array_key_exists('api_field', $association)) { |
189
|
1 |
|
$mapping['api_field'] = $association['api_field']; |
190
|
1 |
|
} |
191
|
1 |
|
if (array_key_exists('orderBy', $association)) { |
192
|
|
|
$mapping['orderBy'] = $association['orderBy']; |
193
|
|
|
} |
194
|
1 |
|
if (array_key_exists('indexBy', $association)) { |
195
|
|
|
$mapping['indexBy'] = $association['indexBy']; |
196
|
|
|
} |
197
|
1 |
|
$metadata->mapManyToMany($mapping); |
198
|
1 |
|
break; |
199
|
|
|
} |
200
|
18 |
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Loads a mapping file with the given name and returns a map |
204
|
|
|
* from class/entity names to their corresponding file driver elements. |
205
|
|
|
* |
206
|
|
|
* @param string $file The mapping file to load. |
207
|
|
|
* |
208
|
|
|
* @return array |
209
|
|
|
* @throws ParseException |
210
|
|
|
*/ |
211
|
28 |
|
protected function loadMappingFile($file) |
212
|
|
|
{ |
213
|
28 |
|
return Yaml::parse(file_get_contents($file)); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
28 |
|
private function fieldToArray($field, $source) |
217
|
|
|
{ |
218
|
|
|
$mapping = [ |
219
|
28 |
|
'field' => $field, |
220
|
28 |
|
'type' => 'string', |
221
|
28 |
|
'nullable' => true, |
222
|
28 |
|
'options' => [], |
223
|
28 |
|
]; |
224
|
|
|
|
225
|
28 |
|
if (array_key_exists('type', $source)) { |
226
|
28 |
|
$mapping['type'] = $source['type']; |
227
|
28 |
|
} |
228
|
|
|
|
229
|
28 |
|
if (array_key_exists('nullable', $source)) { |
230
|
10 |
|
$mapping['nullable'] = $source['nullable']; |
231
|
10 |
|
} |
232
|
|
|
|
233
|
28 |
|
if (array_key_exists('api_field', $source)) { |
234
|
26 |
|
$mapping['api_field'] = $source['api_field']; |
235
|
26 |
|
} |
236
|
|
|
|
237
|
28 |
|
if (array_key_exists('options', $source)) { |
238
|
2 |
|
$mapping['options'] = $source['options']; |
239
|
2 |
|
} |
240
|
|
|
|
241
|
28 |
|
return $mapping; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: