|
29 | 29 | use Doctrine\Common\EventArgs;
|
30 | 30 | use Doctrine\ODM\MongoDB\Event\OnFlushEventArgs as MongoDbOdmOnFlushEventArgs;
|
31 | 31 | use Doctrine\ORM\Event\OnFlushEventArgs as OrmOnFlushEventArgs;
|
| 32 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
32 | 33 | use Symfony\Component\ExpressionLanguage\ExpressionFunction;
|
33 | 34 | use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
34 | 35 | use Symfony\Component\HttpFoundation\JsonResponse;
|
@@ -65,7 +66,7 @@ final class PublishMercureUpdatesListener
|
65 | 66 | /**
|
66 | 67 | * @param array<string, string[]|string> $formats
|
67 | 68 | */
|
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) |
69 | 70 | {
|
70 | 71 | if (null === $messageBus && null === $hubRegistry) {
|
71 | 72 | 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
|
240 | 241 | $data = json_encode(['@id' => $object->id] + ($this->includeType ? ['@type' => $object->type] : []), \JSON_THROW_ON_ERROR);
|
241 | 242 | } else {
|
242 | 243 | $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() ?? []; |
244 | 246 |
|
245 | 247 | // We need to evaluate it here, because in storeObjectToPublish() the resource would not have been persisted yet
|
246 | 248 | $this->evaluateTopics($options, $object);
|
247 | 249 |
|
248 | 250 | $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 | + } |
250 | 271 | }
|
251 | 272 |
|
252 | 273 | $updates = array_merge([$this->buildUpdate($iri, $data, $options)], $this->getGraphQlSubscriptionUpdates($object, $options, $type));
|
|
0 commit comments