Skip to content

[draft] feat(mercure): #7087 - Make Mercure use provider if available when publishing updates #7171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory" />
<argument type="service" id="api_platform.serializer" />
<argument>%api_platform.formats%</argument>
<argument type="service" id="service_container" />
<argument type="service" id="messenger.default_bus" on-invalid="ignore" />
<argument type="service" id="Symfony\Component\Mercure\HubRegistry" />
<argument type="service" id="api_platform.graphql.subscription.subscription_manager" on-invalid="ignore" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory" />
<argument type="service" id="api_platform.serializer" />
<argument>%api_platform.formats%</argument>
<argument type="service" id="service_container" />
<argument type="service" id="messenger.default_bus" on-invalid="ignore" />
<argument type="service" id="Symfony\Component\Mercure\HubRegistry" />
<argument type="service" id="api_platform.graphql.subscription.subscription_manager" on-invalid="ignore" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Doctrine\Common\EventArgs;
use Doctrine\ODM\MongoDB\Event\OnFlushEventArgs as MongoDbOdmOnFlushEventArgs;
use Doctrine\ORM\Event\OnFlushEventArgs as OrmOnFlushEventArgs;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand Down Expand Up @@ -65,7 +66,7 @@
/**
* @param array<string, string[]|string> $formats
*/
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)
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could inject directly the ReadProvider instead of injecting a container? Also please add it as last argument.

I'd also probably inject the SerializeProcessor instead of writing again the whole context/serialization logic.

{
if (null === $messageBus && null === $hubRegistry) {
throw new InvalidArgumentException('A message bus or a hub registry must be provided.');
Expand Down Expand Up @@ -240,13 +241,33 @@
$data = json_encode(['@id' => $object->id] + ($this->includeType ? ['@type' => $object->type] : []), \JSON_THROW_ON_ERROR);
} else {
$resourceClass = $this->getObjectClass($object);
$context = $options['normalization_context'] ?? $this->resourceMetadataFactory->create($resourceClass)->getOperation()->getNormalizationContext() ?? [];
$operation = $this->resourceMetadataFactory->create($resourceClass)->getOperation();
$context = $options['normalization_context'] ?? $operation->getNormalizationContext() ?? [];

Check warning on line 245 in src/Symfony/Doctrine/EventListener/PublishMercureUpdatesListener.php

View check run for this annotation

Codecov / codecov/patch

src/Symfony/Doctrine/EventListener/PublishMercureUpdatesListener.php#L244-L245

Added lines #L244 - L245 were not covered by tests

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

$iri = $options['topics'] ?? $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_URL);
$data = $options['data'] ?? $this->serializer->serialize($object, key($this->formats), $context);

$data = null;
if(isset($options['data'])) {
$data = $options['data'];

Check warning on line 254 in src/Symfony/Doctrine/EventListener/PublishMercureUpdatesListener.php

View check run for this annotation

Codecov / codecov/patch

src/Symfony/Doctrine/EventListener/PublishMercureUpdatesListener.php#L252-L254

Added lines #L252 - L254 were not covered by tests
} else {
// check if has a provider
$provider = $operation->getProvider() ?? false;
if ($provider) {
if(is_string($provider)){
$provider = $this->container->get($provider);

Check warning on line 260 in src/Symfony/Doctrine/EventListener/PublishMercureUpdatesListener.php

View check run for this annotation

Codecov / codecov/patch

src/Symfony/Doctrine/EventListener/PublishMercureUpdatesListener.php#L257-L260

Added lines #L257 - L260 were not covered by tests
}
$providedData = $provider->provide($operation, [ 'id' => $object->getId() ], $context);

Check failure on line 262 in src/Symfony/Doctrine/EventListener/PublishMercureUpdatesListener.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Cannot call method provide() on (callable)|object.
if ($providedData) {
$data = $this->serializer->serialize($providedData, key($this->formats), $context);

Check warning on line 264 in src/Symfony/Doctrine/EventListener/PublishMercureUpdatesListener.php

View check run for this annotation

Codecov / codecov/patch

src/Symfony/Doctrine/EventListener/PublishMercureUpdatesListener.php#L262-L264

Added lines #L262 - L264 were not covered by tests
}
}
}
if (!$data) {
$data = $this->serializer->serialize($object, key($this->formats), $context);

Check warning on line 269 in src/Symfony/Doctrine/EventListener/PublishMercureUpdatesListener.php

View check run for this annotation

Codecov / codecov/patch

src/Symfony/Doctrine/EventListener/PublishMercureUpdatesListener.php#L268-L269

Added lines #L268 - L269 were not covered by tests
}
}

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