1 | <?php |
||
36 | class JmsSerializerSubscriber implements EventSubscriberInterface |
||
37 | { |
||
38 | /** @var StorageInterface */ |
||
39 | private $storage; |
||
40 | |||
41 | /** @var RequestContext */ |
||
42 | private $requestContext; |
||
43 | |||
44 | /** @var Reader */ |
||
45 | private $annotationReader; |
||
46 | |||
47 | /** @var PropertyAccessorInterface */ |
||
48 | private $propertyAccessor; |
||
49 | |||
50 | /** @var LoggerInterface */ |
||
51 | private $logger; |
||
52 | |||
53 | /** @var array */ |
||
54 | private $serializedObjects = []; |
||
55 | |||
56 | /** |
||
57 | * @param StorageInterface $storage |
||
58 | * @param RequestContext $requestContext |
||
59 | * @param Reader $annotationReader |
||
60 | * @param PropertyAccessorInterface $propertyAccessor |
||
61 | * @param LoggerInterface $logger |
||
62 | */ |
||
63 | public function __construct(StorageInterface $storage, RequestContext $requestContext, Reader $annotationReader, PropertyAccessorInterface $propertyAccessor, LoggerInterface $logger) |
||
71 | |||
72 | /** |
||
73 | * @return array |
||
74 | */ |
||
75 | public static function getSubscribedEvents(): array |
||
82 | |||
83 | /** |
||
84 | * @param PreSerializeEvent $event |
||
85 | * |
||
86 | * @throws IncompatibleUploadableAndSerializableFieldAnnotationException |
||
87 | */ |
||
88 | public function onPreSerialize(PreSerializeEvent $event): void |
||
89 | { |
||
90 | $object = $event->getObject(); |
||
91 | |||
92 | if ($object instanceof Proxy && !$object->__isInitialized()) { |
||
93 | $object->__load(); |
||
94 | } |
||
95 | |||
96 | $objectUid = \spl_object_hash($object); |
||
97 | if (\array_key_exists($objectUid, $this->serializedObjects)) { |
||
98 | return; |
||
99 | } |
||
100 | |||
101 | $classAnnotation = $this->annotationReader->getClassAnnotation( |
||
102 | new \ReflectionClass(ClassUtils::getClass($object)), |
||
103 | VichSerializableClass::class |
||
104 | ); |
||
105 | |||
106 | if ($classAnnotation instanceof VichSerializableClass) { |
||
107 | $reflectionClass = ClassUtils::newReflectionClass(\get_class($object)); |
||
108 | $this->logger->debug(\sprintf( |
||
109 | 'Found @VichSerializableClass annotation for the class "%s"', |
||
110 | $reflectionClass->getName() |
||
|
|||
111 | )); |
||
112 | |||
113 | foreach ($reflectionClass->getProperties() as $property) { |
||
114 | $vichSerializableAnnotation = $this->annotationReader->getPropertyAnnotation($property, VichSerializableField::class); |
||
115 | |||
116 | if ($vichSerializableAnnotation instanceof VichSerializableField) { |
||
117 | $vichUploadableFileAnnotation = $this->annotationReader->getPropertyAnnotation($property, UploadableField::class); |
||
118 | |||
119 | if ($vichUploadableFileAnnotation instanceof UploadableField) { |
||
120 | throw new IncompatibleUploadableAndSerializableFieldAnnotationException(\sprintf( |
||
121 | 'The field "%s" in the class "%s" cannot have @UploadableField and @VichSerializableField annotations at the same moment.', |
||
122 | $property->getName(), |
||
123 | $reflectionClass->getName() |
||
124 | )); |
||
125 | } |
||
126 | $this->logger->debug(\sprintf( |
||
127 | 'Found @VichSerializableField annotation for the field "%s" in the class "%s"', |
||
128 | $property->getName(), |
||
129 | $reflectionClass->getName() |
||
130 | )); |
||
131 | |||
132 | $uri = null; |
||
133 | $property->setAccessible(true); |
||
134 | |||
135 | if ($property->getValue($event->getObject())) { |
||
136 | $uri = $this->storage->resolveUri($object, $vichSerializableAnnotation->getField()); |
||
137 | if ($vichSerializableAnnotation->isIncludeHost() && false === \filter_var($uri, FILTER_VALIDATE_URL)) { |
||
138 | $uri = $this->getHostUrl().$uri; |
||
139 | } |
||
140 | } |
||
141 | $this->serializedObjects[$objectUid][$property->getName()] = $property->getValue($event->getObject()); |
||
142 | $property->setValue($object, $uri); |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param ObjectEvent $event |
||
150 | */ |
||
151 | public function onPostSerialize(ObjectEvent $event): void |
||
169 | |||
170 | /** |
||
171 | * Get host url (scheme://host:port). |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | private function getHostUrl(): string |
||
192 | } |
||
193 |