Skip to content

chore(serializer): outsource cache tag collection into separate service #5757

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

Closed
wants to merge 1 commit into from
Closed
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
46 changes: 46 additions & 0 deletions src/HttpCache/TagCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\HttpCache;

use ApiPlatform\Metadata\ApiProperty;
use Symfony\Component\PropertyInfo\Type;

/**
* Collects cache tags during normalization.
*
* @author Urban Suppiger <[email protected]>
*/
class TagCollector implements TagCollectorInterface
{
public function collectTagsFromNormalize(mixed $object, string $format = null, array $context = [], string $iri = null): void
{
$this->addResourceToContext($context, $iri);
}

public function collectTagsFromNormalizeRelation(mixed $object, string $format = null, array $context = [], string $iri = null): void
{
$this->addResourceToContext($context, $iri);
}

public function collectTagsFromGetAttribute(mixed $object, string $format = null, array $context = [], string $iri = null, string $attribute = null, ApiProperty $propertyMetadata = null, Type $type = null, array $childContext = []): void
{
}

private function addResourceToContext(array $context, ?string $iri): void
{
if (isset($context['resources']) && isset($iri)) {
$context['resources'][$iri] = $iri;
}
}
}
31 changes: 31 additions & 0 deletions src/HttpCache/TagCollectorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\HttpCache;

use ApiPlatform\Metadata\ApiProperty;
use Symfony\Component\PropertyInfo\Type;

/**
* Interface for collecting cache tags during normalization.
*
* @author Urban Suppiger <[email protected]>
*/
interface TagCollectorInterface
{
public function collectTagsFromNormalize(mixed $object, string $format = null, array $context = [], string $iri = null): void;

public function collectTagsFromNormalizeRelation(mixed $object, string $format = null, array $context = [], string $iri = null): void;

public function collectTagsFromGetAttribute(mixed $object, string $format = null, array $context = [], string $iri = null, string $attribute = null, ApiProperty $propertyMetadata = null, Type $type = null, array $childContext = []): void;
}
9 changes: 5 additions & 4 deletions src/JsonApi/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ApiPlatform\Api\ResourceClassResolverInterface;
use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\Exception\ItemNotFoundException;
use ApiPlatform\HttpCache\TagCollectorInterface;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
Expand Down Expand Up @@ -52,9 +53,9 @@ final class ItemNormalizer extends AbstractItemNormalizer

private array $componentsCache = [];

public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, ResourceAccessCheckerInterface $resourceAccessChecker = null)
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, ResourceAccessCheckerInterface $resourceAccessChecker = null, protected ?TagCollectorInterface $tagCollector = null)
{
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker);
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker, $tagCollector);
}

/**
Expand Down Expand Up @@ -223,8 +224,8 @@ protected function normalizeRelation(ApiProperty $propertyMetadata, ?object $rel
$iri = $this->iriConverter->getIriFromResource($relatedObject);
$context['iri'] = $iri;

if (isset($context['resources'])) {
$context['resources'][$iri] = $iri;
if ($this->tagCollector) {
$this->tagCollector->collectTagsFromNormalizeRelation($relatedObject, $format, $context, $iri);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/JsonLd/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Api\ResourceClassResolverInterface;
use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\HttpCache\TagCollectorInterface;
use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
use ApiPlatform\JsonLd\ContextBuilderInterface;
use ApiPlatform\Metadata\HttpOperation;
Expand Down Expand Up @@ -45,9 +46,9 @@ final class ItemNormalizer extends AbstractItemNormalizer

public const FORMAT = 'jsonld';

public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, private readonly ContextBuilderInterface $contextBuilder, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ResourceAccessCheckerInterface $resourceAccessChecker = null)
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, private readonly ContextBuilderInterface $contextBuilder, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ResourceAccessCheckerInterface $resourceAccessChecker = null, protected ?TagCollectorInterface $tagCollector = null)
{
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker);
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker, $tagCollector);
}

/**
Expand Down
19 changes: 14 additions & 5 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\Exception\InvalidArgumentException;
use ApiPlatform\Exception\ItemNotFoundException;
use ApiPlatform\HttpCache\TagCollectorInterface;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Exception\OperationNotFoundException;
Expand Down Expand Up @@ -61,7 +62,7 @@ abstract class AbstractItemNormalizer extends AbstractObjectNormalizer
protected array $localCache = [];
protected array $localFactoryOptionsCache = [];

public function __construct(protected PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, protected PropertyMetadataFactoryInterface $propertyMetadataFactory, protected IriConverterInterface $iriConverter, protected ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, protected ?ResourceAccessCheckerInterface $resourceAccessChecker = null)
public function __construct(protected PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, protected PropertyMetadataFactoryInterface $propertyMetadataFactory, protected IriConverterInterface $iriConverter, protected ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, protected ?ResourceAccessCheckerInterface $resourceAccessChecker = null, protected ?TagCollectorInterface $tagCollector = null)
{
if (!isset($defaultContext['circular_reference_handler'])) {
$defaultContext['circular_reference_handler'] = fn ($object): ?string => $this->iriConverter->getIriFromResource($object);
Expand Down Expand Up @@ -160,8 +161,8 @@ public function normalize(mixed $object, string $format = null, array $context =
$emptyResourceAsIri = $context['api_empty_resource_as_iri'] ?? false;
unset($context['api_empty_resource_as_iri']);

if (isset($context['resources'])) {
$context['resources'][$iri] = $iri;
if ($this->tagCollector) {
$this->tagCollector->collectTagsFromNormalize($object, $format, $context, $iri);
}

$data = parent::normalize($object, $format, $context);
Expand Down Expand Up @@ -635,6 +636,10 @@ protected function getAttributeValue(object $object, string $attribute, string $
$resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className);
$childContext = $this->createChildContext($this->createOperationContext($context, $resourceClass), $attribute, $format);

if ($this->tagCollector) {
$this->tagCollector->collectTagsFromGetAttribute($object, $format, $context, $context['iri'], $attribute, $propertyMetadata, $type, $childContext);
}

return $this->normalizeCollectionOfRelations($propertyMetadata, $attributeValue, $resourceClass, $format, $childContext);
}

Expand All @@ -650,6 +655,10 @@ protected function getAttributeValue(object $object, string $attribute, string $
$resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className);
$childContext = $this->createChildContext($this->createOperationContext($context, $resourceClass), $attribute, $format);

if ($this->tagCollector) {
$this->tagCollector->collectTagsFromGetAttribute($object, $format, $context, $context['iri'], $attribute, $propertyMetadata, $type, $childContext);
}

return $this->normalizeRelation($propertyMetadata, $attributeValue, $resourceClass, $format, $childContext);
}

Expand Down Expand Up @@ -728,8 +737,8 @@ protected function normalizeRelation(ApiProperty $propertyMetadata, ?object $rel

$iri = $this->iriConverter->getIriFromResource($relatedObject);

if (isset($context['resources'])) {
$context['resources'][$iri] = $iri;
if ($this->tagCollector) {
$this->tagCollector->collectTagsFromNormalizeRelation($relatedObject, $format, $context, $iri);
}

$push = $propertyMetadata->getPush() ?? false;
Expand Down
5 changes: 3 additions & 2 deletions src/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ApiPlatform\Api\ResourceClassResolverInterface;
use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\Exception\InvalidArgumentException;
use ApiPlatform\HttpCache\TagCollectorInterface;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
Expand All @@ -38,9 +39,9 @@ class ItemNormalizer extends AbstractItemNormalizer
{
private readonly LoggerInterface $logger;

public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, LoggerInterface $logger = null, ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, ResourceAccessCheckerInterface $resourceAccessChecker = null, array $defaultContext = [])
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, LoggerInterface $logger = null, ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, ResourceAccessCheckerInterface $resourceAccessChecker = null, array $defaultContext = [], protected ?TagCollectorInterface $tagCollector = null)
{
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataFactory, $resourceAccessChecker);
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataFactory, $resourceAccessChecker, $tagCollector);

$this->logger = $logger ?: new NullLogger();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/Resources/config/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
<argument>null</argument>
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory" on-invalid="ignore" />
<argument type="service" id="api_platform.security.resource_access_checker" on-invalid="ignore" />
<argument type="collection" />
<argument type="service" id="api_platform.http_cache.tag_collector" on-invalid="ignore" />

<!-- Run before serializer.normalizer.json_serializable -->
<tag name="serializer.normalizer" priority="-895" />
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Bundle/Resources/config/http_cache_purger.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@
<argument type="service" id="api_platform.http_cache.purger" on-invalid="null" />
<tag name="kernel.event_listener" event="kernel.response" method="onKernelResponse" priority="-2" />
</service>

<service id="api_platform.http_cache.tag_collector" class="ApiPlatform\HttpCache\TagCollector" />

</services>
</container>
1 change: 1 addition & 0 deletions src/Symfony/Bundle/Resources/config/jsonapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<argument type="collection" />
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory" />
<argument type="service" id="api_platform.security.resource_access_checker" on-invalid="ignore" />
<argument type="service" id="api_platform.http_cache.tag_collector" on-invalid="ignore" />

<!-- Run before serializer.normalizer.json_serializable -->
<tag name="serializer.normalizer" priority="-890" />
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/Resources/config/jsonld.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<argument type="service" id="serializer.mapping.class_metadata_factory" on-invalid="ignore" />
<argument type="collection" />
<argument type="service" id="api_platform.security.resource_access_checker" on-invalid="ignore" />
<argument type="service" id="api_platform.http_cache.tag_collector" on-invalid="ignore" />

<!-- Run before serializer.normalizer.json_serializable -->
<tag name="serializer.normalizer" priority="-890" />
Expand Down
Loading