feat(httpcache): add doctrine mongodb odm purge listener by soyuka · Pull Request #8733 · api-platform/core · GitHub
Skip to content
Open
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
@@ -0,0 +1,33 @@
<?php

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

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use ApiPlatform\Symfony\Doctrine\Odm\EventListener\PurgeHttpCacheListener;

return static function (ContainerConfigurator $container) {
$services = $container->services();

$services->set('api_platform.doctrine_mongodb.odm.listener.http_cache.purge', PurgeHttpCacheListener::class)
->args([
service('api_platform.http_cache.purger'),
service('api_platform.iri_converter'),
service('api_platform.resource_class_resolver'),
service('api_platform.property_accessor'),
service('api_platform.object_mapper')->nullOnInvalid(),
service('api_platform.object_mapper.metadata_factory')->nullOnInvalid(),
service('api_platform.metadata.resource.metadata_collection_factory'),
])
->tag('doctrine_mongodb.odm.event_listener', ['event' => 'onFlush'])
->tag('doctrine_mongodb.odm.event_listener', ['event' => 'postFlush']);
};
111 changes: 1 addition & 110 deletions src/Symfony/Doctrine/EventListener/PurgeHttpCacheListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,9 @@
namespace ApiPlatform\Symfony\Doctrine\EventListener;

use ApiPlatform\HttpCache\PurgerInterface;
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\Exception\OperationNotFoundException;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\Metadata\UrlGeneratorInterface;
use ApiPlatform\Metadata\Util\ClassInfoTrait;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
Expand All @@ -43,10 +35,8 @@
*/
final class PurgeHttpCacheListener
{
use ClassInfoTrait;
use PurgeHttpCacheTagsTrait;
private readonly PropertyAccessorInterface $propertyAccessor;
private array $tags = [];

private array $scheduledInsertions = [];

public function __construct(private readonly PurgerInterface $purger,
Expand Down Expand Up @@ -127,51 +117,6 @@ public function postFlush(): void
$this->tags = [];
}

private function gatherResourceAndItemTags(object $entity, bool $purgeItem): void
{
$resources = $this->getResourcesForEntity($entity);

foreach ($resources as $resource) {
try {
foreach ($this->getCollectionOperations($resource) as $operation) {
$iri = $this->iriConverter->getIriFromResource($resource, UrlGeneratorInterface::ABS_PATH, $operation);
$this->tags[$iri] = $iri;
}

if ($purgeItem) {
$this->addTagForItem($entity);
}
} catch (OperationNotFoundException|InvalidArgumentException) {
}
}
}

/**
* @return iterable<Operation&CollectionOperationInterface>
*/
private function getCollectionOperations(object $resource): iterable
{
if (!$this->resourceMetadataCollectionFactory) {
yield new GetCollection();

return;
}

foreach ($this->resourceMetadataCollectionFactory->create($this->getObjectClass($resource)) as $apiResource) {
foreach ($apiResource->getOperations() ?? [] as $operation) {
if (!$operation instanceof CollectionOperationInterface || !$operation instanceof HttpOperation) {
continue;
}

if (!empty($operation->getUriVariables())) {
continue;
}

yield $operation;
}
}
}

private function gatherRelationTags(EntityManagerInterface $em, object $entity): void
{
$associationMappings = $em->getClassMetadata($entity::class)->getAssociationMappings();
Expand Down Expand Up @@ -229,58 +174,4 @@ private function addTagsFor(mixed $value): void
$this->addTagForItem($v);
}
}

private function addTagForItem(mixed $value): void
{
$resources = $this->getResourcesForEntity($value);

foreach ($resources as $resource) {
try {
$iri = $this->iriConverter->getIriFromResource($resource);
$this->tags[$iri] = $iri;
} catch (OperationNotFoundException|InvalidArgumentException) {
}
}
}

private function getResourcesForEntity(object $entity): array
{
$resources = [];

if (!$this->resourceClassResolver->isResourceClass($class = $this->getObjectClass($entity))) {
// is the entity mapped to resource(s)?
if (!$this->objectMapper) {
return [];
}

if ($this->objectMapperMetadata) {
$mappings = $this->objectMapperMetadata->create($entity);

if (!$mappings) {
return [];
}

$resources = array_map(
fn ($mapping) => $this->objectMapper->map($entity, $mapping->target),
$mappings
);
} else {
$mapAttributes = (new \ReflectionClass($class))->getAttributes(Map::class);

if (!$mapAttributes) {
return [];
}

// loop over all mappings to fetch all resources mapped to this entity
$resources = array_map(
fn ($mapAttribute) => $this->objectMapper->map($entity, $mapAttribute->newInstance()->target),
$mapAttributes
);
}
} else {
$resources[] = $entity;
}

return $resources;
}
}
134 changes: 134 additions & 0 deletions src/Symfony/Doctrine/EventListener/PurgeHttpCacheTagsTrait.php
Loading
Loading