-
-
Notifications
You must be signed in to change notification settings - Fork 586
/
GraphNavigator.php
346 lines (287 loc) · 15.3 KB
/
GraphNavigator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
namespace JMS\Serializer;
use JMS\Serializer\Construction\ObjectConstructorInterface;
use JMS\Serializer\EventDispatcher\EventDispatcherInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use JMS\Serializer\EventDispatcher\PreDeserializeEvent;
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
use JMS\Serializer\Exception\ExpressionLanguageRequiredException;
use JMS\Serializer\Exception\InvalidArgumentException;
use JMS\Serializer\Exception\RuntimeException;
use JMS\Serializer\Exclusion\ExpressionLanguageExclusionStrategy;
use JMS\Serializer\Expression\ExpressionEvaluatorInterface;
use JMS\Serializer\Handler\HandlerRegistryInterface;
use JMS\Serializer\Metadata\ClassMetadata;
use Metadata\MetadataFactoryInterface;
/**
* Handles traversal along the object graph.
*
* This class handles traversal along the graph, and calls different methods
* on visitors, or custom handlers to process its nodes.
*
* @author Johannes M. Schmitt <[email protected]>
*/
final class GraphNavigator implements GraphNavigatorInterface
{
/**
* @var ExpressionLanguageExclusionStrategy
*/
private $expressionExclusionStrategy;
private $dispatcher;
private $metadataFactory;
private $handlerRegistry;
private $objectConstructor;
/**
* Parses a direction string to one of the direction constants.
*
* @param string $dirStr
*
* @return integer
*/
public static function parseDirection($dirStr)
{
switch (strtolower($dirStr)) {
case 'serialization':
return self::DIRECTION_SERIALIZATION;
case 'deserialization':
return self::DIRECTION_DESERIALIZATION;
default:
throw new InvalidArgumentException(sprintf('The direction "%s" does not exist.', $dirStr));
}
}
public function __construct(
MetadataFactoryInterface $metadataFactory,
HandlerRegistryInterface $handlerRegistry,
ObjectConstructorInterface $objectConstructor,
EventDispatcherInterface $dispatcher = null,
ExpressionEvaluatorInterface $expressionEvaluator = null
)
{
$this->dispatcher = $dispatcher;
$this->metadataFactory = $metadataFactory;
$this->handlerRegistry = $handlerRegistry;
$this->objectConstructor = $objectConstructor;
if ($expressionEvaluator) {
$this->expressionExclusionStrategy = new ExpressionLanguageExclusionStrategy($expressionEvaluator);
}
}
/**
* Called for each node of the graph that is being traversed.
*
* @param mixed $data the data depends on the direction, and type of visitor
* @param null|array $type array has the format ["name" => string, "params" => array]
* @param Context $context
* @return mixed the return value depends on the direction, and type of visitor
*/
public function accept($data, array $type = null, Context $context)
{
$visitor = $context->getVisitor();
// If the type was not given, we infer the most specific type from the
// input data in serialization mode.
if (null === $type) {
if ($context instanceof DeserializationContext) {
throw new RuntimeException('The type must be given for all properties when deserializing.');
}
$typeName = \gettype($data);
if ('object' === $typeName) {
$typeName = \get_class($data);
}
$type = array('name' => $typeName, 'params' => array());
}
// If the data is null, we have to force the type to null regardless of the input in order to
// guarantee correct handling of null values, and not have any internal auto-casting behavior.
else if ($context instanceof SerializationContext && null === $data) {
$type = array('name' => 'NULL', 'params' => array());
}
// Sometimes data can convey null but is not of a null type.
// Visitors can have the power to add this custom null evaluation
if ($visitor instanceof NullAwareVisitorInterface && $visitor->isNull($data) === true) {
$type = array('name' => 'NULL', 'params' => array());
}
switch ($type['name']) {
case 'NULL':
return $visitor->visitNull($data, $type, $context);
case 'string':
return $visitor->visitString($data, $type, $context);
case 'int':
case 'integer':
return $visitor->visitInteger($data, $type, $context);
case 'bool':
case 'boolean':
return $visitor->visitBoolean($data, $type, $context);
case 'double':
case 'float':
return $visitor->visitDouble($data, $type, $context);
case 'array':
return $visitor->visitArray($data, $type, $context);
case 'resource':
$msg = 'Resources are not supported in serialized data.';
if ($context instanceof SerializationContext && null !== $path = $context->getPath()) {
$msg .= ' Path: ' . $path;
}
throw new RuntimeException($msg);
default:
// TODO: The rest of this method needs some refactoring.
if ($context instanceof SerializationContext) {
if (null !== $data) {
if ($context->isVisiting($data)) {
return null;
}
$context->startVisiting($data);
}
// If we're serializing a polymorphic type, then we'll be interested in the
// metadata for the actual type of the object, not the base class.
if (class_exists($type['name'], false) || interface_exists($type['name'], false)) {
if (is_subclass_of($data, $type['name'], false)) {
$type = array('name' => \get_class($data), 'params' => array());
}
}
} elseif ($context instanceof DeserializationContext) {
$context->increaseDepth();
}
// Trigger pre-serialization callbacks, and listeners if they exist.
// Dispatch pre-serialization event before handling data to have ability change type in listener
if ($context instanceof SerializationContext) {
if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.pre_serialize', $type['name'], $context->getFormat())) {
$this->dispatcher->dispatch('serializer.pre_serialize', $type['name'], $context->getFormat(), $event = new PreSerializeEvent($context, $data, $type));
$type = $event->getType();
}
} elseif ($context instanceof DeserializationContext) {
if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.pre_deserialize', $type['name'], $context->getFormat())) {
$this->dispatcher->dispatch('serializer.pre_deserialize', $type['name'], $context->getFormat(), $event = new PreDeserializeEvent($context, $data, $type));
$type = $event->getType();
$data = $event->getData();
}
}
// First, try whether a custom handler exists for the given type. This is done
// before loading metadata because the type name might not be a class, but
// could also simply be an artifical type.
if (null !== $handler = $this->handlerRegistry->getHandler($context->getDirection(), $type['name'], $context->getFormat())) {
$rs = \call_user_func($handler, $visitor, $data, $type, $context);
$this->leaveScope($context, $data);
return $rs;
}
$exclusionStrategy = $context->getExclusionStrategy();
/** @var $metadata ClassMetadata */
$metadata = $this->metadataFactory->getMetadataForClass($type['name']);
if ($metadata->usingExpression && !$this->expressionExclusionStrategy) {
throw new ExpressionLanguageRequiredException("To use conditional exclude/expose in {$metadata->name} you must configure the expression language.");
}
if ($context instanceof DeserializationContext && !empty($metadata->discriminatorMap) && $type['name'] === $metadata->discriminatorBaseClass) {
$metadata = $this->resolveMetadata($data, $metadata);
}
if (null !== $exclusionStrategy && $exclusionStrategy->shouldSkipClass($metadata, $context)) {
$this->leaveScope($context, $data);
return null;
}
$context->pushClassMetadata($metadata);
if ($context instanceof SerializationContext) {
foreach ($metadata->preSerializeMethods as $method) {
$method->invoke($data);
}
}
$object = $data;
if ($context instanceof DeserializationContext) {
$object = $this->objectConstructor->construct($visitor, $metadata, $data, $type, $context);
}
if (isset($metadata->handlerCallbacks[$context->getDirection()][$context->getFormat()])) {
$rs = $object->{$metadata->handlerCallbacks[$context->getDirection()][$context->getFormat()]}(
$visitor,
$context instanceof SerializationContext ? null : $data,
$context
);
$this->afterVisitingObject($metadata, $object, $type, $context);
return $context instanceof SerializationContext ? $rs : $object;
}
$visitor->startVisitingObject($metadata, $object, $type, $context);
foreach ($metadata->propertyMetadata as $propertyMetadata) {
if (null !== $exclusionStrategy && $exclusionStrategy->shouldSkipProperty($propertyMetadata, $context)) {
continue;
}
if (null !== $this->expressionExclusionStrategy && $this->expressionExclusionStrategy->shouldSkipProperty($propertyMetadata, $context)) {
continue;
}
if ($context instanceof DeserializationContext && $propertyMetadata->readOnly) {
continue;
}
$context->pushPropertyMetadata($propertyMetadata);
$visitor->visitProperty($propertyMetadata, $data, $context);
$context->popPropertyMetadata();
}
if ($context instanceof SerializationContext) {
$this->afterVisitingObject($metadata, $data, $type, $context);
return $visitor->endVisitingObject($metadata, $data, $type, $context);
}
$rs = $visitor->endVisitingObject($metadata, $data, $type, $context);
$this->afterVisitingObject($metadata, $rs, $type, $context);
return $rs;
}
}
private function resolveMetadata($data, ClassMetadata $metadata)
{
switch (true) {
case \is_array($data) && isset($data[$metadata->discriminatorFieldName]):
$typeValue = (string)$data[$metadata->discriminatorFieldName];
break;
// Check XML attribute without namespace for discriminatorFieldName
case \is_object($data) && $metadata->xmlDiscriminatorAttribute && null === $metadata->xmlDiscriminatorNamespace && isset($data->attributes()->{$metadata->discriminatorFieldName}):
$typeValue = (string)$data->attributes()->{$metadata->discriminatorFieldName};
break;
// Check XML attribute with namespace for discriminatorFieldName
case \is_object($data) && $metadata->xmlDiscriminatorAttribute && null !== $metadata->xmlDiscriminatorNamespace && isset($data->attributes($metadata->xmlDiscriminatorNamespace)->{$metadata->discriminatorFieldName}):
$typeValue = (string)$data->attributes($metadata->xmlDiscriminatorNamespace)->{$metadata->discriminatorFieldName};
break;
// Check XML element with namespace for discriminatorFieldName
case \is_object($data) && !$metadata->xmlDiscriminatorAttribute && null !== $metadata->xmlDiscriminatorNamespace && isset($data->children($metadata->xmlDiscriminatorNamespace)->{$metadata->discriminatorFieldName}):
$typeValue = (string)$data->children($metadata->xmlDiscriminatorNamespace)->{$metadata->discriminatorFieldName};
break;
// Check XML element for discriminatorFieldName
case \is_object($data) && isset($data->{$metadata->discriminatorFieldName}):
$typeValue = (string)$data->{$metadata->discriminatorFieldName};
break;
default:
throw new \LogicException(sprintf(
'The discriminator field name "%s" for base-class "%s" was not found in input data.',
$metadata->discriminatorFieldName,
$metadata->name
));
}
if (!isset($metadata->discriminatorMap[$typeValue])) {
throw new \LogicException(sprintf(
'The type value "%s" does not exist in the discriminator map of class "%s". Available types: %s',
$typeValue,
$metadata->name,
implode(', ', array_keys($metadata->discriminatorMap))
));
}
return $this->metadataFactory->getMetadataForClass($metadata->discriminatorMap[$typeValue]);
}
private function leaveScope(Context $context, $data)
{
if ($context instanceof SerializationContext) {
$context->stopVisiting($data);
} elseif ($context instanceof DeserializationContext) {
$context->decreaseDepth();
}
}
private function afterVisitingObject(ClassMetadata $metadata, $object, array $type, Context $context)
{
$this->leaveScope($context, $object);
$context->popClassMetadata();
if ($context instanceof SerializationContext) {
foreach ($metadata->postSerializeMethods as $method) {
$method->invoke($object);
}
if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.post_serialize', $metadata->name, $context->getFormat())) {
$this->dispatcher->dispatch('serializer.post_serialize', $metadata->name, $context->getFormat(), new ObjectEvent($context, $object, $type));
}
return;
}
foreach ($metadata->postDeserializeMethods as $method) {
$method->invoke($object);
}
if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.post_deserialize', $metadata->name, $context->getFormat())) {
$this->dispatcher->dispatch('serializer.post_deserialize', $metadata->name, $context->getFormat(), new ObjectEvent($context, $object, $type));
}
}
}