Skip to content

Commit

Permalink
also purge collection tags for parent classes
Browse files Browse the repository at this point in the history
  • Loading branch information
usu committed May 28, 2024
1 parent 6bd7409 commit 221b3a3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
18 changes: 14 additions & 4 deletions api/src/HttpCache/PurgeHttpCacheListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,20 @@ public function onFlush(OnFlushEventArgs $eventArgs): void {
$uow = $em->getUnitOfWork();

foreach ($uow->getScheduledEntityInsertions() as $entity) {
$this->gatherResourceTags($entity);
$this->gatherResourceTags($em, $entity);
$this->gatherRelationTags($em, $entity);
}

foreach ($uow->getScheduledEntityUpdates() as $entity) {
$originalEntity = $this->getOriginalEntity($entity, $em);
$this->addTagForItem($entity);
$this->gatherResourceTags($entity, $originalEntity);
$this->gatherResourceTags($em, $entity, $originalEntity);
}

foreach ($uow->getScheduledEntityDeletions() as $entity) {
$originalEntity = $this->getOriginalEntity($entity, $em);
$this->addTagForItem($originalEntity);
$this->gatherResourceTags($originalEntity);
$this->gatherResourceTags($em, $originalEntity);
$this->gatherRelationTags($em, $originalEntity);
}
}
Expand Down Expand Up @@ -124,8 +124,18 @@ private function getOriginalEntity($entity, $em) {
* If oldEntity is provided, purge is only done if the IRI of the collection has changed
* (e.g. for updating period on a ScheduleEntry and the IRI changes from /periods/1/schedule_entries to /periods/2/schedule_entries)
*/
private function gatherResourceTags(object $entity, ?object $oldEntity = null): void {
private function gatherResourceTags(EntityManagerInterface $em, object $entity, ?object $oldEntity = null): void {
$resourceClass = $this->resourceClassResolver->getResourceClass($entity);
$this->gatherResourceTagsForClass($resourceClass, $entity, $oldEntity);

// also purge parent classes (e.g. /content_nodes)
$classMetadata = $em->getClassMetadata(ClassUtils::getClass($entity));
foreach ($classMetadata->parentClasses as $parentClass) {
$this->gatherResourceTagsForClass($parentClass, $entity, $oldEntity);
}
}

private function gatherResourceTagsForClass(string $resourceClass, object $entity, ?object $oldEntity = null): void {
$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
$resourceIterator = $resourceMetadataCollection->getIterator();
while ($resourceIterator->valid()) {
Expand Down
3 changes: 1 addition & 2 deletions api/tests/Api/Categories/CreateCategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,7 @@ public function testCreateCategoryPurgesCacheTags() {
self::assertEqualsCanonicalizing([
'/categories',
'/camps/'.$camp1->getId().'/categories',
// TODO: fix PurgeHttpCacheListener to include the following tags:
// '/content_nodes',
'/content_nodes',
'/content_node/column_layouts',
$camp1->getId().'#categories',
$contentType->getId().'#categories',
Expand Down
3 changes: 1 addition & 2 deletions api/tests/Api/Categories/DeleteCategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ public function testDeleteCategoryPurgesCacheTags() {
'/categories',
'/camps/'.$camp->getId().'/categories',
$camp->getId().'#categories',
// TODO: fix PurgeHttpCacheListener to include the following tags:
// '/content_nodes',
'/content_nodes',
'/content_node/column_layouts',
$rootContentNode->getId(),
$rootContentNode->getId().'#rootDescendants',
Expand Down
3 changes: 1 addition & 2 deletions api/tests/Api/ContentNodes/CreateContentNodeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ public function testCreatePurgesCacheTags() {

$this->assertResponseStatusCodeSame(201);
self::assertEqualsCanonicalizing([
// TODO: fix PurgeHttpCacheListener to include the following tags:
// '/content_nodes',
'/content_nodes',
$this->endpoint,
$this->defaultParent->getRoot()->getId().'#rootDescendants',
$this->defaultParent->getId().'#children',
Expand Down

0 comments on commit 221b3a3

Please sign in to comment.