Skip to content
Merged
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
20 changes: 10 additions & 10 deletions src/Symfony/Doctrine/EventListener/PurgeHttpCacheListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ final class PurgeHttpCacheListener
private readonly PropertyAccessorInterface $propertyAccessor;
private array $tags = [];

private array $scheduledInsertions = [];

public function __construct(private readonly PurgerInterface $purger,
private readonly IriConverterInterface $iriConverter,
private readonly ResourceClassResolverInterface $resourceClassResolver,
Expand Down Expand Up @@ -75,23 +77,16 @@ public function preUpdate(PreUpdateEventArgs $eventArgs): void
}

/**
* Collects tags from inserted and deleted entities, including relations.
* Collects tags from updated and deleted entities, including relations.
*/
public function onFlush(OnFlushEventArgs $eventArgs): void
{
// @phpstan-ignore-next-line
$em = method_exists($eventArgs, 'getObjectManager') ? $eventArgs->getObjectManager() : $eventArgs->getEntityManager();
$uow = $em->getUnitOfWork();

foreach ($uow->getScheduledEntityInsertions() as $entity) {
// For new entities, only purge the collection IRI
try {
if ($this->resourceClassResolver->isResourceClass($this->getObjectClass($entity))) {
$iri = $this->iriConverter->getIriFromResource($entity, UrlGeneratorInterface::ABS_PATH, new GetCollection());
$this->tags[$iri] = $iri;
}
} catch (OperationNotFoundException|InvalidArgumentException) {
}
foreach ($this->scheduledInsertions = $uow->getScheduledEntityInsertions() as $entity) {
// inserts shouldn't add new related entities, we should be able to gather related tags already
$this->gatherRelationTags($em, $entity);
}

Expand All @@ -111,6 +106,11 @@ public function onFlush(OnFlushEventArgs $eventArgs): void
*/
public function postFlush(): void
{
// since IRIs can't always be generated for new entities (missing auto-generated IDs), we need to gather the related IRIs after flush()
foreach ($this->scheduledInsertions as $entity) {
$this->gatherResourceAndItemTags($entity, false);
}

if (empty($this->tags)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use ApiPlatform\Metadata\UrlGeneratorInterface;
use ApiPlatform\Symfony\Doctrine\EventListener\PurgeHttpCacheListener;
use ApiPlatform\Symfony\Tests\Fixtures\MappedEntity;
use ApiPlatform\Symfony\Tests\Fixtures\MappedResource;
use ApiPlatform\Symfony\Tests\Fixtures\NotAResource;
use ApiPlatform\Symfony\Tests\Fixtures\TestBundle\Entity\ContainNonResource;
use ApiPlatform\Symfony\Tests\Fixtures\TestBundle\Entity\Dummy;
Expand All @@ -35,6 +36,7 @@
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\ObjectMapper\ObjectMapperInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;

/**
Expand Down Expand Up @@ -215,7 +217,8 @@ public function testNotAResourceClass(): void
$propertyAccessorProphecy->getValue(Argument::type(ContainNonResource::class), 'notAResource')->shouldBeCalled()->willReturn($nonResource1);
$propertyAccessorProphecy->getValue(Argument::type(ContainNonResource::class), 'collectionOfNotAResource')->shouldBeCalled()->willReturn($collectionOfNotAResource);

$listener = new PurgeHttpCacheListener($purgerProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal());
$listener = new PurgeHttpCacheListener($purgerProphecy->reveal(), $iriConverterProphecy->reveal(),
$resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal());
$listener->onFlush($eventArgs);
$listener->postFlush();
}
Expand All @@ -229,7 +232,7 @@ public function testAddTagsForCollection(): void
$collection = [$dummy1, $dummy2];

$purgerProphecy = $this->prophesize(PurgerInterface::class);
$purgerProphecy->purge(['/dummies', '/dummies/1', '/dummies/2'])->shouldBeCalled();
$purgerProphecy->purge(['/dummies/1', '/dummies/2', '/dummies'])->shouldBeCalled();

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromResource(Argument::type(Dummy::class), UrlGeneratorInterface::ABS_PATH, new GetCollection())->willReturn('/dummies')->shouldBeCalled();
Expand Down Expand Up @@ -270,15 +273,30 @@ public function testAddTagsForCollection(): void
public function testMappedResources(): void
{
$mappedEntity = new MappedEntity();
$mappedEntity->setFirstName('first');
$mappedEntity->setlastName('last');

$mappedResource = new MappedResource();
$mappedResource->username = $mappedEntity->getFirstName().' '.$mappedEntity->getLastName();

$purgerProphecy = $this->prophesize(PurgerInterface::class);
$purgerProphecy->purge(['/mapped_ressources'])->shouldBeCalled();

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromResource(Argument::type(MappedEntity::class), UrlGeneratorInterface::ABS_PATH, new GetCollection())->willReturn('/mapped_ressources')->shouldBeCalled();
// the entity is not a resource, shouldn't be called
$iriConverterProphecy->getIriFromResource(
Argument::type(MappedEntity::class), UrlGeneratorInterface::ABS_PATH, new GetCollection()
)->shouldNotBeCalled();
// this should be called instead
$iriConverterProphecy->getIriFromResource(
Argument::type(MappedResource::class), UrlGeneratorInterface::ABS_PATH, new GetCollection()
)->willReturn('/mapped_ressources')->shouldBeCalled();

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->isResourceClass(MappedEntity::class)->willReturn(true)->shouldBeCalled();
$resourceClassResolverProphecy->isResourceClass(MappedEntity::class)->willReturn(false)->shouldBeCalled();

$objectMapperProphecy = $this->prophesize(ObjectMapperInterface::class);
$objectMapperProphecy->map($mappedEntity, MappedResource::class)->shouldBeCalled()->willReturn($mappedResource);

$uowProphecy = $this->prophesize(UnitOfWork::class);
$uowProphecy->getScheduledEntityInsertions()->willReturn([$mappedEntity])->shouldBeCalled();
Expand All @@ -294,7 +312,10 @@ public function testMappedResources(): void

$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);

$listener = new PurgeHttpCacheListener($purgerProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal());
$listener = new PurgeHttpCacheListener($purgerProphecy->reveal(), $iriConverterProphecy->reveal(),
$resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(),
$objectMapperProphecy->reveal()
);
$listener->onFlush($eventArgs);
$listener->postFlush();
}
Expand Down
1 change: 0 additions & 1 deletion src/Symfony/Tests/Fixtures/MappedEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace ApiPlatform\Symfony\Tests\Fixtures;

use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\ObjectMapper\Attribute\Map;

Expand Down
44 changes: 44 additions & 0 deletions src/Symfony/Tests/Fixtures/MappedResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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 ApiPlatform\Symfony\Tests\Fixtures;

use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\JsonLd\ContextBuilder;
use ApiPlatform\Metadata\ApiResource;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[ApiResource(
stateOptions: new Options(entityClass: MappedEntity::class),
normalizationContext: [ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX => false],
)]
#[Map(target: MappedEntity::class)]
final class MappedResource
{
#[Map(if: false)]
public ?string $id = null;

#[Map(target: 'firstName', transform: [self::class, 'toFirstName'])]
#[Map(target: 'lastName', transform: [self::class, 'toLastName'])]
public string $username;

public static function toFirstName(string $v): string
{
return explode(' ', $v)[0];
}

public static function toLastName(string $v): string
{
return explode(' ', $v)[1];
}
}
1 change: 1 addition & 0 deletions src/Symfony/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"symfony/expression-language": "^6.4 || ^7.0",
"symfony/intl": "^6.4 || ^7.0",
"symfony/mercure-bundle": "*",
"symfony/object-mapper": "^7.0",
"symfony/routing": "^6.4 || ^7.0",
"symfony/type-info": "^7.3",
"symfony/validator": "^6.4 || ^7.0",
Expand Down
9 changes: 8 additions & 1 deletion tests/Behat/HttpCacheContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ public function irisShouldBePurged(string $iris): void
{
$purger = $this->driverContainer->get('test.api_platform.http_cache.purger');

$purgedIris = implode(',', $purger->getIris());
$iris = explode(',', $iris);
sort($iris);
$iris = implode(',', $iris);

$purgedIris = $purger->getIris();
sort($purgedIris);
$purgedIris = implode(',', $purgedIris);

$purger->clear();

if ($iris !== $purgedIris) {
Expand Down
Loading