-
Notifications
You must be signed in to change notification settings - Fork 1
Invalidate dependent elements #37
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
Draft
jan888adams
wants to merge
5
commits into
main
Choose a base branch
from
invalidate-dependent-elements
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.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e7c60f8
Invalidate dependent elements
jan888adams 507cb99
Refactor test factories
jan888adams 3edf2b2
Add testcases for asset & document invalidation
jan888adams c38800b
Only invalidate dependencies from objects
jan888adams af41501
Test invalidation for dependent documents
jan888adams 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
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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| use Neusta\Pimcore\HttpCacheBundle\Cache\CacheInvalidator; | ||
| use Pimcore\Event\Model\ElementEventInterface; | ||
| use Pimcore\Model\Dependency; | ||
| use Pimcore\Model\Element\ElementInterface; | ||
| use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
|
||
|
|
@@ -12,6 +13,7 @@ final class InvalidateElementListener | |
| public function __construct( | ||
| private readonly CacheInvalidator $cacheInvalidator, | ||
| private readonly EventDispatcherInterface $dispatcher, | ||
| private readonly ElementRepository $elementRepository, | ||
| ) { | ||
| } | ||
|
|
||
|
|
@@ -21,12 +23,24 @@ public function onUpdate(ElementEventInterface $event): void | |
| return; | ||
| } | ||
|
|
||
| $this->invalidateElement($event->getElement()); | ||
| $element = $event->getElement(); | ||
|
|
||
| $this->invalidateElement($element); | ||
|
|
||
| if (ElementType::Object === ElementType::tryFrom($element->getType())) { | ||
| $this->invalidateDependencies($element->getDependencies()); | ||
| } | ||
| } | ||
|
|
||
| public function onDelete(ElementEventInterface $event): void | ||
| { | ||
| $this->invalidateElement($event->getElement()); | ||
| $element = $event->getElement(); | ||
|
|
||
| $this->invalidateElement($element); | ||
|
|
||
| if (ElementType::Object === ElementType::tryFrom($element->getType())) { | ||
| $this->invalidateDependencies($element->getDependencies()); | ||
| } | ||
| } | ||
|
|
||
| private function invalidateElement(ElementInterface $element): void | ||
|
|
@@ -40,4 +54,24 @@ private function invalidateElement(ElementInterface $element): void | |
|
|
||
| $this->cacheInvalidator->invalidate($invalidationEvent->cacheTags()); | ||
| } | ||
|
|
||
| private function invalidateDependencies(Dependency $dependency): void | ||
| { | ||
| foreach ($dependency->getRequiredBy() as $required) { | ||
| if (!isset($required['id'], $required['type'])) { | ||
| continue; | ||
| } | ||
|
|
||
| $element = match (ElementType::tryFrom($required['type'])) { | ||
| ElementType::Object => $this->elementRepository->findObject($required['id']), | ||
| ElementType::Document => $this->elementRepository->findDocument($required['id']), | ||
| ElementType::Asset => $this->elementRepository->findAsset($required['id']), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to check if invalidating an asset makes sense. |
||
| default => null, | ||
| }; | ||
|
|
||
| if ($element) { | ||
| $this->invalidateElement($element); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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.
Why only for objects?
Uh oh!
There was an error while loading. Please reload this page.
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.
It wouldn’t make sense in every case.
If an object is required by a document, why should all sites that use this object be invalidated?
If a snippet is required by a page, why should all pages containing that snippet be invalidated?
I think we need to carefully consider in which cases this behavior actually makes sense.
Uh oh!
There was an error while loading. Please reload this page.
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.
You don't know that. Only the app will know, right?
Probably because it is somehow used in the document, so when it's not invalidated it displays outdated data?
Same reason: why should the page require the snippet if it doesn't use it? And if it does, and it isn't invalidated, it shows outdated data.
Here I'm with you. Maybe we need an (easy) mechanism that controls whether the dependencies should be invalidated or not (also: which ones?) 🤔
Uh oh!
There was an error while loading. Please reload this page.
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.
Yeah, you are right. The examples were not selected carefully.
Indeed, it should be: a Document is required by an Object. That may or may not make sense, but it’s a case the user of the bundle could have control over.
Also, a Page being required by a Snippet doesn’t sound like a standard case. If we want to do that, it should be considered more carefully.
So the idea was: cases where an Object is required by another Object or by a Document are clear. The other cases are not standard and should not be included in our default behavior.
The best way to handle them is, as you said, through configuration.