1 | <?php |
||
36 | class JmsSerializerSubscriber implements EventSubscriberInterface |
||
37 | { |
||
38 | private const HTTP_PORT = 80; |
||
39 | private const HTTPS_PORT = 443; |
||
40 | |||
41 | /** @var StorageInterface */ |
||
42 | private $storage; |
||
43 | |||
44 | /** @var RequestContext */ |
||
45 | private $requestContext; |
||
46 | |||
47 | /** @var Reader */ |
||
48 | private $annotationReader; |
||
49 | |||
50 | /** @var PropertyAccessorInterface */ |
||
51 | private $propertyAccessor; |
||
52 | |||
53 | /** @var LoggerInterface */ |
||
54 | private $logger; |
||
55 | |||
56 | /** @var mixed[] */ |
||
57 | private $serializedObjects = []; |
||
58 | |||
59 | /** |
||
60 | * @param StorageInterface $storage |
||
61 | * @param RequestContext $requestContext |
||
62 | * @param Reader $annotationReader |
||
63 | * @param PropertyAccessorInterface $propertyAccessor |
||
64 | * @param LoggerInterface $logger |
||
65 | */ |
||
66 | public function __construct(StorageInterface $storage, RequestContext $requestContext, Reader $annotationReader, PropertyAccessorInterface $propertyAccessor, LoggerInterface $logger) |
||
67 | { |
||
68 | $this->storage = $storage; |
||
69 | $this->requestContext = $requestContext; |
||
70 | $this->annotationReader = $annotationReader; |
||
71 | $this->propertyAccessor = $propertyAccessor; |
||
72 | $this->logger = $logger; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @return array |
||
77 | */ |
||
78 | public static function getSubscribedEvents(): \Generator |
||
79 | { |
||
80 | yield => ['event' => Events::PRE_SERIALIZE, 'method' => 'onPreSerialize']; |
||
|
|||
81 | yield => ['event' => Events::POST_SERIALIZE, 'method' => 'onPostSerialize']; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param PreSerializeEvent $event |
||
86 | * |
||
87 | * @throws IncompatibleUploadableAndSerializableFieldAnnotationException |
||
88 | */ |
||
89 | public function onPreSerialize(PreSerializeEvent $event): void |
||
90 | { |
||
91 | $object = $event->getObject(); |
||
92 | if (!\is_object($object)) { |
||
93 | return; |
||
94 | } |
||
95 | |||
96 | if ($object instanceof Proxy && !$object->__isInitialized()) { |
||
97 | $object->__load(); |
||
98 | } |
||
99 | |||
100 | $objectUid = \spl_object_hash($object); |
||
101 | if (\array_key_exists($objectUid, $this->serializedObjects)) { |
||
102 | return; |
||
103 | } |
||
104 | |||
105 | /** @var class-string<object> $className */ |
||
106 | $className = ClassUtils::getClass($object); |
||
107 | |||
108 | $classAnnotation = $this->annotationReader->getClassAnnotation( |
||
109 | new \ReflectionClass($className), |
||
110 | VichSerializableClass::class |
||
111 | ); |
||
112 | |||
113 | if ($classAnnotation instanceof VichSerializableClass) { |
||
114 | $reflectionClass = ClassUtils::newReflectionClass(\get_class($object)); |
||
115 | $this->logger->debug(\sprintf( |
||
116 | 'Found @VichSerializableClass annotation for the class "%s"', |
||
117 | $reflectionClass->getName() |
||
118 | )); |
||
119 | |||
120 | foreach ($reflectionClass->getProperties() as $property) { |
||
121 | $vichSerializableAnnotation = $this->annotationReader->getPropertyAnnotation($property, VichSerializableField::class); |
||
122 | |||
123 | if ($vichSerializableAnnotation instanceof VichSerializableField) { |
||
124 | $vichUploadableFileAnnotation = $this->annotationReader->getPropertyAnnotation($property, UploadableField::class); |
||
125 | |||
126 | if ($vichUploadableFileAnnotation instanceof UploadableField) { |
||
127 | $exceptionMessage = \sprintf( |
||
128 | 'The field "%s" in the class "%s" cannot have @UploadableField and @VichSerializableField annotations at the same moment.', |
||
129 | $property->getName(), |
||
130 | $reflectionClass->getName() |
||
131 | ); |
||
132 | |||
133 | throw new IncompatibleUploadableAndSerializableFieldAnnotationException($exceptionMessage); |
||
134 | } |
||
135 | $this->logger->debug(\sprintf( |
||
136 | 'Found @VichSerializableField annotation for the field "%s" in the class "%s"', |
||
137 | $property->getName(), |
||
138 | $reflectionClass->getName() |
||
139 | )); |
||
140 | |||
141 | $uri = null; |
||
142 | $property->setAccessible(true); |
||
143 | |||
144 | if ($property->getValue($event->getObject())) { |
||
145 | $uri = $this->storage->resolveUri($object, $vichSerializableAnnotation->getField()); |
||
146 | if ($vichSerializableAnnotation->isIncludeHost() && false === \filter_var($uri, FILTER_VALIDATE_URL)) { |
||
147 | $uri = $this->getHostUrl().$uri; |
||
148 | } |
||
149 | } |
||
150 | $this->serializedObjects[$objectUid][$property->getName()] = $property->getValue($event->getObject()); |
||
151 | $property->setValue($object, $uri); |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param ObjectEvent $event |
||
159 | */ |
||
160 | public function onPostSerialize(ObjectEvent $event): void |
||
161 | { |
||
162 | $object = $event->getObject(); |
||
163 | if (!\is_object($object)) { |
||
164 | return; |
||
165 | } |
||
166 | |||
167 | if ($object instanceof Proxy && !$object->__isInitialized()) { |
||
168 | $object->__load(); |
||
169 | } |
||
170 | |||
171 | $objectUid = \spl_object_hash($object); |
||
172 | if (!\array_key_exists($objectUid, $this->serializedObjects)) { |
||
173 | return; |
||
174 | } |
||
175 | |||
176 | foreach ($this->serializedObjects[$objectUid] as $propertyName => $propertyValue) { |
||
177 | $this->propertyAccessor->setValue($object, $propertyName, $propertyValue); |
||
178 | } |
||
179 | unset($this->serializedObjects[$objectUid]); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Get host url (scheme://host:port). |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | private function getHostUrl(): string |
||
188 | { |
||
189 | $scheme = $this->requestContext->getScheme(); |
||
190 | $url = $scheme.'://'.$this->requestContext->getHost(); |
||
191 | |||
192 | $httpPort = $this->requestContext->getHttpPort(); |
||
193 | if ('http' === $scheme && $httpPort && self::HTTP_PORT !== $httpPort) { |
||
194 | return $url.':'.$httpPort; |
||
195 | } |
||
196 | |||
197 | $httpsPort = $this->requestContext->getHttpsPort(); |
||
198 | if ('https' === $scheme && $httpsPort && self::HTTPS_PORT !== $httpsPort) { |
||
199 | return $url.':'.$httpsPort; |
||
200 | } |
||
201 | |||
205 |