-
-
Notifications
You must be signed in to change notification settings - Fork 965
feat(httpcache): introduce PurgeTagProviderInterface extension point #7970
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
Open
guillaumedelre
wants to merge
1
commit into
api-platform:main
Choose a base branch
from
guillaumedelre:feature/http-cache-purge-tag-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?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\HttpCache; | ||
|
|
||
| /** | ||
| * Collects extra HTTP cache tags to invalidate for a given entity. | ||
| */ | ||
| interface PurgeTagProviderInterface | ||
| { | ||
| /** | ||
| * @return iterable<string> | ||
| */ | ||
| public function getTagsForResource(object $entity): iterable; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/Laravel/Tests/Unit/Listener/PurgeHttpCacheListenerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| <?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\Laravel\Tests\Unit\Listener; | ||
|
|
||
| use ApiPlatform\HttpCache\PurgerInterface; | ||
| use ApiPlatform\HttpCache\PurgeTagProviderInterface; | ||
| use ApiPlatform\Laravel\Eloquent\Listener\PurgeHttpCacheListener; | ||
| use ApiPlatform\Metadata\Exception\InvalidArgumentException; | ||
| use ApiPlatform\Metadata\GetCollection; | ||
| use ApiPlatform\Metadata\IriConverterInterface; | ||
| use ApiPlatform\Metadata\ResourceClassResolverInterface; | ||
| use Illuminate\Database\Eloquent\Model; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class PurgeHttpCacheListenerTest extends TestCase | ||
| { | ||
| public function testPurgeTagProviders(): void | ||
| { | ||
| $model = new class extends Model { | ||
| }; | ||
|
|
||
| $purger = $this->createMock(PurgerInterface::class); | ||
| $purger->expects($this->once()) | ||
| ->method('purge') | ||
| ->with(['/models/1', '/models', '/parents/42/children']); | ||
|
|
||
| $iriConverter = $this->createStub(IriConverterInterface::class); | ||
| $iriConverter->method('getIriFromResource') | ||
| ->willReturnCallback(static function (object|string $resource, int $referenceType = 0, ?object $operation = null): string { | ||
| if ($operation instanceof GetCollection) { | ||
| return '/models'; | ||
| } | ||
|
|
||
| return '/models/1'; | ||
| }); | ||
|
|
||
| $resourceClassResolver = $this->createStub(ResourceClassResolverInterface::class); | ||
| $resourceClassResolver->method('isResourceClass')->willReturn(true); | ||
|
|
||
| $provider = $this->createMock(PurgeTagProviderInterface::class); | ||
| $provider->expects($this->once()) | ||
| ->method('getTagsForResource') | ||
| ->with($model) | ||
| ->willReturn(['/parents/42/children']); | ||
|
|
||
| $listener = new PurgeHttpCacheListener($purger, $iriConverter, $resourceClassResolver, [$provider]); | ||
| $listener->handleModelSaved('eloquent.saved: '.$model::class, [$model]); | ||
| $listener->postFlush(); | ||
| } | ||
|
|
||
| public function testPurgeTagProvidersOnDelete(): void | ||
| { | ||
| $model = new class extends Model { | ||
| }; | ||
|
|
||
| $purger = $this->createMock(PurgerInterface::class); | ||
| $purger->expects($this->once()) | ||
| ->method('purge') | ||
| ->with(['/models/1', '/models', '/parents/42/children']); | ||
|
|
||
| $iriConverter = $this->createStub(IriConverterInterface::class); | ||
| $iriConverter->method('getIriFromResource') | ||
| ->willReturnCallback(static function (object|string $resource, int $referenceType = 0, ?object $operation = null): string { | ||
| if ($operation instanceof GetCollection) { | ||
| return '/models'; | ||
| } | ||
|
|
||
| return '/models/1'; | ||
| }); | ||
|
|
||
| $resourceClassResolver = $this->createStub(ResourceClassResolverInterface::class); | ||
| $resourceClassResolver->method('isResourceClass')->willReturn(true); | ||
|
|
||
| $provider = $this->createMock(PurgeTagProviderInterface::class); | ||
| $provider->expects($this->once()) | ||
| ->method('getTagsForResource') | ||
| ->with($model) | ||
| ->willReturn(['/parents/42/children']); | ||
|
|
||
| $listener = new PurgeHttpCacheListener($purger, $iriConverter, $resourceClassResolver, [$provider]); | ||
| $listener->handleModelDeleted('eloquent.deleted: '.$model::class, [$model]); | ||
| $listener->postFlush(); | ||
| } | ||
|
|
||
| public function testNoTagsWhenNoProviders(): void | ||
| { | ||
| $model = new class extends Model { | ||
| }; | ||
|
|
||
| $purger = $this->createMock(PurgerInterface::class); | ||
| $purger->expects($this->never())->method('purge'); | ||
|
|
||
| $iriConverter = $this->createStub(IriConverterInterface::class); | ||
| $iriConverter->method('getIriFromResource')->willThrowException(new InvalidArgumentException()); | ||
|
|
||
| $resourceClassResolver = $this->createStub(ResourceClassResolverInterface::class); | ||
| $resourceClassResolver->method('isResourceClass')->willReturn(true); | ||
|
|
||
| $listener = new PurgeHttpCacheListener($purger, $iriConverter, $resourceClassResolver); | ||
| $listener->handleModelSaved('eloquent.saved: '.$model::class, [$model]); | ||
| $listener->postFlush(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this method better receive both the old entity (before an update) and the new entity (after an update)? In the example provided in the initial issue, it will otherwise be difficult to detect when a child changes it's parent and hence 2 separate tags need to be purged?
Also providing old entity and new entity would provide an easy method for the provider to detect if the method was called from an insertion or deletion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback, you're right that the current signature is insufficient for the parent-change use case.
When a
Childchanges itsparent, two tags need to be purged:/parents/{old_id}/childrenand/parents/{new_id}/children. With the currentgetTagsForResource(object $entity), the implementor only receives the entity in its new state and has no way to compute the old parent's tag.The fix I have in mind is to change the signature to:
Where
previousEntityisnullon insertion and populated on update/deletion. This is BC-safe since the second parameter is nullable with a default value.The key architectural point is where this is called. The current implementation calls providers from inside
PurgeHttpCacheListener(a Doctrine ORM event listener). At that layer there is noprevious_data— Doctrine's changeset only exposes scalar/identifier diffs, not a fully hydrated snapshot of the previous entity.API Platform already solves this at a higher level:
ReadProviderclones the entity before deserialization and stores it in$context['previous_data']. This snapshot is exactly what we need. The right place to call the providers is therefore the processor layer (e.g.PersistProcessoror a dedicated purge processor), where$context['previous_data']is naturally available for PUT/PATCH operations andnullfor POST.I can rework the PR to move the provider calls to that layer and update the interface accordingly. @soyuka what do you think?