-
Notifications
You must be signed in to change notification settings - Fork 1
Conveniently disable auto tagging proposal #43
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
base: conveniently-disable-auto-tagging
Are you sure you want to change the base?
Changes from all commits
5565e8e
4632690
0be11bb
13b75f9
e1a7cd1
823ab45
1f53096
e4ef552
505f4c9
a3fad86
20d7073
4d64128
d4c23f8
bf6b3a9
95ef6f3
f1c9e9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -14,8 +14,43 @@ To achieve this, you can use the `CacheActivator` to disable tagging and invalid | |||||
| self::getContainer()->get(CacheActivator::class)->deactivateCaching(); | ||||||
|
|
||||||
| // Your test code here | ||||||
|
|
||||||
| self::assertSame('this is amazing!', $result); | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| ## Suppress automatic tagging for a specific code block | ||||||
|
|
||||||
| Sometimes you need to load Pimcore elements for business logic without tagging the current response with those elements. For example, loading related products to calculate a price should not cause the response to be tagged with all those products. | ||||||
|
|
||||||
| Use `CacheActivator::withoutAutomaticTagging()` to run a block of code with automatic tagging suppressed: | ||||||
|
|
||||||
| ```php | ||||||
| $result = $cacheActivator->withoutAutomaticTagging(function () use ($id) { | ||||||
| // Automatic tagging is disabled here — loading this element | ||||||
| // will not tag the current response. | ||||||
| return $this->repository->find($id); | ||||||
| }); | ||||||
| ``` | ||||||
|
|
||||||
| ### Selectively tagging within the block | ||||||
|
|
||||||
| If you still want to tag the response with specific tags inside the block, use a generator and `yield` the tags you need: | ||||||
|
|
||||||
| ```php | ||||||
| $result = $cacheActivator->withoutAutomaticTagging(function () use ($id) { | ||||||
| $element = $this->repository->find($id); | ||||||
|
|
||||||
| // Yield only the tags you explicitly want applied to the response. | ||||||
| yield CacheTag::fromElement($element); | ||||||
|
|
||||||
| return $element; | ||||||
| }); | ||||||
| ``` | ||||||
|
|
||||||
| You can yield both `CacheTag` and `CacheTags` objects. All yielded tags are collected and applied to the response after the block completes. The return value of the generator is returned by `withoutAutomaticTagging()`. | ||||||
|
|
||||||
| ### State restoration | ||||||
|
|
||||||
| The previous caching state is always restored after the block completes, even if an exception is thrown. If caching was already disabled before calling `withoutAutomaticTagging()`, it remains disabled afterwards. | ||||||
|
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. Use “afterward” for US English consistency. Replace “afterwards” with “afterward”. ✏️ Proposed wording fix-...it remains disabled afterwards.
+...it remains disabled afterward.📝 Committable suggestion
Suggested change
🧰 Tools🪛 LanguageTool[locale-violation] ~55-~55: In American English, ‘afterward’ is the preferred variant. ‘Afterwards’ is more commonly used in British English and other dialects. (AFTERWARDS_US) 🤖 Prompt for AI Agents |
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| use App\Controller\WithoutAutomaticTaggingController; | ||
| use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; | ||
|
|
||
| return static function (RoutingConfigurator $routes) { | ||
| $routes->add('without_automatic_tagging', '/without-automatic-tagging') | ||
| ->controller(WithoutAutomaticTaggingController::class); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Neusta\Pimcore\HttpCacheBundle\Tests\Integration\Tagging; | ||
|
|
||
| use Neusta\Pimcore\HttpCacheBundle\Tests\Integration\Helpers\ArrangeCacheTest; | ||
| use Neusta\Pimcore\HttpCacheBundle\Tests\Integration\Helpers\TestAssetFactory; | ||
| use Neusta\Pimcore\TestingFramework\Database\ResetDatabase; | ||
| use Neusta\Pimcore\TestingFramework\Test\Attribute\ConfigureExtension; | ||
| use Neusta\Pimcore\TestingFramework\Test\Attribute\ConfigureRoute; | ||
| use Neusta\Pimcore\TestingFramework\Test\ConfigurableWebTestcase; | ||
| use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
|
|
||
| #[ConfigureRoute(__DIR__ . '/../Fixtures/without_automatic_tagging_route.php')] | ||
| final class WithoutAutomaticTaggingTest extends ConfigurableWebTestcase | ||
| { | ||
| use ArrangeCacheTest; | ||
| use ResetDatabase; | ||
|
|
||
| private KernelBrowser $client; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->client = self::createClient(); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| #[ConfigureExtension('neusta_pimcore_http_cache', [ | ||
| 'elements' => [ | ||
| 'assets' => true, | ||
| ], | ||
| ])] | ||
| public function response_is_not_tagged_when_asset_is_loaded_without_automatic_tagging(): void | ||
| { | ||
| self::arrange(static fn () => TestAssetFactory::simpleAsset()->save()); | ||
|
|
||
| $this->client->request('GET', '/without-automatic-tagging?id=42'); | ||
|
|
||
| $response = $this->client->getResponse(); | ||
| self::assertSame(200, $response->getStatusCode()); | ||
| self::assertNull($response->headers->get('X-Cache-Tags')); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| #[ConfigureExtension('neusta_pimcore_http_cache', [ | ||
| 'elements' => [ | ||
| 'assets' => true, | ||
| ], | ||
| ])] | ||
| public function response_is_tagged_with_manually_yielded_tag_when_loaded_without_automatic_tagging(): void | ||
| { | ||
| self::arrange(static fn () => TestAssetFactory::simpleAsset()->save()); | ||
|
|
||
| $this->client->request('GET', '/without-automatic-tagging?id=42&manual_tag=true'); | ||
|
|
||
| $response = $this->client->getResponse(); | ||
| self::assertSame(200, $response->getStatusCode()); | ||
| self::assertSame('a42', $response->headers->get('X-Cache-Tags')); | ||
| } | ||
| } |
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.
I guess this isn’t the best example..