Skip to content

Commit 7f0e00c

Browse files
committed
fix: add evaluateTopics() method
1 parent 074c1f2 commit 7f0e00c

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

src/Doctrine/EventListener/PublishMercureUpdatesListener.php

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -190,35 +190,15 @@ private function storeObjectToPublish(object $object, string $property): void
190190

191191
$options['enable_async_update'] ??= true;
192192

193-
if ($options['topics'] ?? false) {
194-
$topics = [];
195-
foreach ((array) $options['topics'] as $topic) {
196-
if (!\is_string($topic)) {
197-
$topics[] = $topic;
198-
continue;
199-
}
200-
201-
if (!str_starts_with($topic, '@=')) {
202-
$topics[] = $topic;
203-
continue;
204-
}
205-
206-
if (null === $this->expressionLanguage) {
207-
throw new \LogicException('The "@=" expression syntax cannot be used without the Expression Language component. Try running "composer require symfony/expression-language".');
208-
}
209-
210-
$topics[] = $this->expressionLanguage->evaluate(substr($topic, 2), ['object' => $object]);
211-
}
212-
213-
$options['topics'] = $topics;
214-
}
215-
216193
if ('deletedObjects' === $property) {
217194
$types = $operation instanceof HttpOperation ? $operation->getTypes() : null;
218195
if (null === $types) {
219196
$types = [$operation->getShortName()];
220197
}
221198

199+
// We need to evaluate it here, because in publishUpdate() the resource would be already deleted
200+
$this->evaluateTopics($options, $object);
201+
222202
$this->deletedObjects[(object) [
223203
'id' => $this->iriConverter->getIriFromResource($object),
224204
'iri' => $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_URL),
@@ -244,6 +224,9 @@ private function publishUpdate(object $object, array $options, string $type): vo
244224
$resourceClass = $this->getObjectClass($object);
245225
$context = $options['normalization_context'] ?? $this->resourceMetadataFactory->create($resourceClass)->getOperation()->getNormalizationContext() ?? [];
246226

227+
// We need to evaluate it here, because in storeObjectToPublish() the resource would not have been persisted yet
228+
$this->evaluateTopics($options, $object);
229+
247230
$iri = $options['topics'] ?? $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_URL);
248231
$data = $options['data'] ?? $this->serializer->serialize($object, key($this->formats), $context);
249232
}
@@ -260,6 +243,32 @@ private function publishUpdate(object $object, array $options, string $type): vo
260243
}
261244
}
262245

246+
private function evaluateTopics(array &$options, object $object): void
247+
{
248+
if ($options['topics'] ?? false) {
249+
$topics = [];
250+
foreach ((array) $options['topics'] as $topic) {
251+
if (!\is_string($topic)) {
252+
$topics[] = $topic;
253+
continue;
254+
}
255+
256+
if (!str_starts_with($topic, '@=')) {
257+
$topics[] = $topic;
258+
continue;
259+
}
260+
261+
if (null === $this->expressionLanguage) {
262+
throw new \LogicException('The "@=" expression syntax cannot be used without the Expression Language component. Try running "composer require symfony/expression-language".');
263+
}
264+
265+
$topics[] = $this->expressionLanguage->evaluate(substr($topic, 2), ['object' => $object]);
266+
}
267+
268+
$options['topics'] = $topics;
269+
}
270+
}
271+
263272
/**
264273
* @return Update[]
265274
*/

0 commit comments

Comments
 (0)