Skip to content

Commit 275cfab

Browse files
committed
feat(mecure): #7087 - Make Mercure use provider if avaible when publishing updates
Adresses issue #7087
1 parent 79edced commit 275cfab

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/Symfony/Bundle/Resources/config/doctrine_odm_mercure_publisher.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory" />
1515
<argument type="service" id="api_platform.serializer" />
1616
<argument>%api_platform.formats%</argument>
17+
<argument type="service" id="service_container" />
1718
<argument type="service" id="messenger.default_bus" on-invalid="ignore" />
1819
<argument type="service" id="Symfony\Component\Mercure\HubRegistry" />
1920
<argument type="service" id="api_platform.graphql.subscription.subscription_manager" on-invalid="ignore" />

src/Symfony/Bundle/Resources/config/doctrine_orm_mercure_publisher.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory" />
1515
<argument type="service" id="api_platform.serializer" />
1616
<argument>%api_platform.formats%</argument>
17+
<argument type="service" id="service_container" />
1718
<argument type="service" id="messenger.default_bus" on-invalid="ignore" />
1819
<argument type="service" id="Symfony\Component\Mercure\HubRegistry" />
1920
<argument type="service" id="api_platform.graphql.subscription.subscription_manager" on-invalid="ignore" />

src/Symfony/Doctrine/EventListener/PublishMercureUpdatesListener.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Doctrine\Common\EventArgs;
3030
use Doctrine\ODM\MongoDB\Event\OnFlushEventArgs as MongoDbOdmOnFlushEventArgs;
3131
use Doctrine\ORM\Event\OnFlushEventArgs as OrmOnFlushEventArgs;
32+
use Symfony\Component\DependencyInjection\ContainerInterface;
3233
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
3334
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
3435
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -65,7 +66,7 @@ final class PublishMercureUpdatesListener
6566
/**
6667
* @param array<string, string[]|string> $formats
6768
*/
68-
public function __construct(ResourceClassResolverInterface $resourceClassResolver, private readonly IriConverterInterface $iriConverter, ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly SerializerInterface $serializer, private readonly array $formats, ?MessageBusInterface $messageBus = null, private readonly ?HubRegistry $hubRegistry = null, private readonly ?GraphQlSubscriptionManagerInterface $graphQlSubscriptionManager = null, private readonly ?GraphQlMercureSubscriptionIriGeneratorInterface $graphQlMercureSubscriptionIriGenerator = null, ?ExpressionLanguage $expressionLanguage = null, private bool $includeType = false)
69+
public function __construct(ResourceClassResolverInterface $resourceClassResolver, private readonly IriConverterInterface $iriConverter, ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly SerializerInterface $serializer, private readonly array $formats, private readonly ContainerInterface $container, ?MessageBusInterface $messageBus = null, private readonly ?HubRegistry $hubRegistry = null, private readonly ?GraphQlSubscriptionManagerInterface $graphQlSubscriptionManager = null, private readonly ?GraphQlMercureSubscriptionIriGeneratorInterface $graphQlMercureSubscriptionIriGenerator = null, ?ExpressionLanguage $expressionLanguage = null, private bool $includeType = false)
6970
{
7071
if (null === $messageBus && null === $hubRegistry) {
7172
throw new InvalidArgumentException('A message bus or a hub registry must be provided.');
@@ -240,13 +241,33 @@ private function publishUpdate(object $object, array $options, string $type): vo
240241
$data = json_encode(['@id' => $object->id] + ($this->includeType ? ['@type' => $object->type] : []), \JSON_THROW_ON_ERROR);
241242
} else {
242243
$resourceClass = $this->getObjectClass($object);
243-
$context = $options['normalization_context'] ?? $this->resourceMetadataFactory->create($resourceClass)->getOperation()->getNormalizationContext() ?? [];
244+
$operation = $this->resourceMetadataFactory->create($resourceClass)->getOperation();
245+
$context = $options['normalization_context'] ?? $operation->getNormalizationContext() ?? [];
244246

245247
// We need to evaluate it here, because in storeObjectToPublish() the resource would not have been persisted yet
246248
$this->evaluateTopics($options, $object);
247249

248250
$iri = $options['topics'] ?? $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_URL);
249-
$data = $options['data'] ?? $this->serializer->serialize($object, key($this->formats), $context);
251+
252+
$data = null;
253+
if(isset($options['data'])) {
254+
$data = $options['data'];
255+
} else {
256+
// check if has a provider
257+
$provider = $operation->getProvider() ?? false;
258+
if ($provider) {
259+
if(is_string($provider)){
260+
$provider = $this->container->get($provider);
261+
}
262+
$providedData = $provider->provide($operation, [ 'id' => $object->getId() ], $context);
263+
if ($providedData) {
264+
$data = $this->serializer->serialize($providedData, key($this->formats), $context);
265+
}
266+
}
267+
}
268+
if (!$data) {
269+
$data = $this->serializer->serialize($object, key($this->formats), $context);
270+
}
250271
}
251272

252273
$updates = array_merge([$this->buildUpdate($iri, $data, $options)], $this->getGraphQlSubscriptionUpdates($object, $options, $type));

0 commit comments

Comments
 (0)