From 6104dc1cae3f02e64aa92f5b603a6ee86b71e6ea Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Mon, 21 Oct 2024 11:22:49 +0200 Subject: [PATCH 1/3] Add cache life time to SnippetArea controller --- Controller/SnippetAreaController.php | 29 ++++++++++++++++++++++++++-- Resources/config/controllers.xml | 1 + 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Controller/SnippetAreaController.php b/Controller/SnippetAreaController.php index 79ee7da..19e6360 100644 --- a/Controller/SnippetAreaController.php +++ b/Controller/SnippetAreaController.php @@ -17,6 +17,7 @@ use JMS\Serializer\SerializerInterface; use Sulu\Bundle\HeadlessBundle\Content\StructureResolverInterface; use Sulu\Bundle\HttpCacheBundle\Cache\SuluHttpCache; +use Sulu\Bundle\HttpCacheBundle\CacheLifetime\CacheLifetimeRequestStore; use Sulu\Bundle\SnippetBundle\Snippet\DefaultSnippetManagerInterface; use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreInterface; use Sulu\Component\Content\Mapper\ContentMapperInterface; @@ -72,6 +73,11 @@ class SnippetAreaController */ private $cacheLifetime; + /** + * @var CacheLifetimeRequestStore|null + */ + private $cacheLifetimeRequestStore; + public function __construct( DefaultSnippetManagerInterface $defaultSnippetManager, ContentMapperInterface $contentMapper, @@ -80,7 +86,8 @@ public function __construct( ?ReferenceStoreInterface $snippetReferenceStore, int $maxAge, int $sharedMaxAge, - int $cacheLifetime + int $cacheLifetime, + ?CacheLifetimeRequestStore $cacheLifetimeRequestStore = null ) { $this->defaultSnippetManager = $defaultSnippetManager; $this->contentMapper = $contentMapper; @@ -90,6 +97,14 @@ public function __construct( $this->maxAge = $maxAge; $this->sharedMaxAge = $sharedMaxAge; $this->cacheLifetime = $cacheLifetime; + $this->cacheLifetimeRequestStore = $cacheLifetimeRequestStore; + + if (null === $cacheLifetimeRequestStore) { + @\trigger_error( + 'Instantiating the SnippetAreaController without the $cacheLifetimeRequestStore argument is deprecated!', + \E_USER_DEPRECATED + ); + } } public function getAction(Request $request, string $area): Response @@ -151,7 +166,17 @@ public function getAction(Request $request, string $area): Response $response->setPublic(); $response->setMaxAge($this->maxAge); $response->setSharedMaxAge($this->sharedMaxAge); - $response->headers->set(SuluHttpCache::HEADER_REVERSE_PROXY_TTL, (string) $this->cacheLifetime); + + $cacheLifetime = $this->cacheLifetime; + if ($this->cacheLifetimeRequestStore !== null) { + $this->cacheLifetimeRequestStore->setCacheLifetime($this->cacheLifetime); + $cacheLifetime = $this->cacheLifetimeRequestStore->getCacheLifetime(); + } + + $response->headers->set( + SuluHttpCache::HEADER_REVERSE_PROXY_TTL, + (string) $cacheLifetime + ); return $response; } diff --git a/Resources/config/controllers.xml b/Resources/config/controllers.xml index 404882d..03d078f 100644 --- a/Resources/config/controllers.xml +++ b/Resources/config/controllers.xml @@ -32,6 +32,7 @@ %sulu_http_cache.cache.max_age% %sulu_http_cache.cache.shared_max_age% %sulu_headless.snippet_area.cache_lifetime% + From f5f5df3101fe05ff85241564badcfef2d783b044 Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Mon, 21 Oct 2024 13:59:26 +0200 Subject: [PATCH 2/3] Add tests --- Controller/SnippetAreaController.php | 2 +- .../templates/snippets/default-blocks.xml | 47 ++++++++++++++ .../Controller/SnippetAreaControllerTest.php | 64 ++++++++++++++++++- 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 Tests/Application/config/templates/snippets/default-blocks.xml diff --git a/Controller/SnippetAreaController.php b/Controller/SnippetAreaController.php index 19e6360..a1f3127 100644 --- a/Controller/SnippetAreaController.php +++ b/Controller/SnippetAreaController.php @@ -168,7 +168,7 @@ public function getAction(Request $request, string $area): Response $response->setSharedMaxAge($this->sharedMaxAge); $cacheLifetime = $this->cacheLifetime; - if ($this->cacheLifetimeRequestStore !== null) { + if (null !== $this->cacheLifetimeRequestStore) { $this->cacheLifetimeRequestStore->setCacheLifetime($this->cacheLifetime); $cacheLifetime = $this->cacheLifetimeRequestStore->getCacheLifetime(); } diff --git a/Tests/Application/config/templates/snippets/default-blocks.xml b/Tests/Application/config/templates/snippets/default-blocks.xml new file mode 100644 index 0000000..8c66863 --- /dev/null +++ b/Tests/Application/config/templates/snippets/default-blocks.xml @@ -0,0 +1,47 @@ + + diff --git a/Tests/Functional/Controller/SnippetAreaControllerTest.php b/Tests/Functional/Controller/SnippetAreaControllerTest.php index c4ce70f..00291a9 100644 --- a/Tests/Functional/Controller/SnippetAreaControllerTest.php +++ b/Tests/Functional/Controller/SnippetAreaControllerTest.php @@ -63,6 +63,48 @@ public static function setUpBeforeClass(): void 'template' => 'other', ], 'de'); + $currentTime = (new \DateTime()); + $scheduledBlockEndtime = (new \DateTime())->add(new \DateInterval('PT30M')); + $snippetWithBlocks = self::createSnippet( + [ + 'title' => 'My Snippet with blocks', + 'description' => 'Description of my snippet with blocks', + 'template' => 'default-blocks', + 'blocks' => [ + [ + 'type' => 'editor_image', + 'article' => '

Article text

', + 'settings' => [ + 'schedules_enabled' => true, + 'schedules' => [ + [ + 'type' => 'weekly', + 'days' => [ + 'monday', + 'tuesday', + 'wednesday', + 'thursday', + 'friday', + 'saturday', + 'sunday', + ], + 'start' => $currentTime->format('H:i:s'), + 'end' => $scheduledBlockEndtime->format('H:i:s'), + ], + ], + ], + ], + ], + ], + 'de' + ); + $defaultSnippetManager->save( + 'sulu_io', + 'default-blocks', + $snippetWithBlocks->getUuid(), + 'de', + ); + static::ensureKernelShutdown(); } @@ -84,18 +126,24 @@ public function provideAttributes(): \Generator '/api/snippet-areas/default', Response::HTTP_OK, 'snippet-area__default.json', + null, + 86400, ]; yield [ '/api/snippet-areas/default?includeExtension=true', Response::HTTP_OK, 'snippet-area__default_include-extension.json', + null, + 86400, ]; yield [ '/api/snippet-areas/default?includeExtension=false', Response::HTTP_OK, 'snippet-area__default.json', + null, + 86400, ]; yield [ @@ -118,6 +166,14 @@ public function provideAttributes(): \Generator null, 'Snippet area "invalid" does not exist', ]; + + yield [ + '/api/snippet-areas/default-blocks', + Response::HTTP_OK, + null, + null, + 1800, + ]; } /** @@ -127,7 +183,8 @@ public function testGetAction( string $url, int $statusCode = Response::HTTP_OK, ?string $expectedPatternFile = null, - ?string $errorMessage = null + ?string $errorMessage = null, + ?int $reverseProxyTtl = null ): void { $this->websiteClient->request('GET', $url); @@ -163,5 +220,10 @@ public function testGetAction( self::assertTrue(\property_exists($responseObject, 'message')); self::assertSame($errorMessage, $responseObject->message); } + + if (null !== $reverseProxyTtl) { + // we need to use less than because the reverse proxy ttl is calculated during runtime + self::assertLessThanOrEqual($reverseProxyTtl, (int) $response->headers->get('X-Reverse-Proxy-TTL')); + } } } From 9e95db63aec579f772b8b429a960938ad4168325 Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Mon, 21 Oct 2024 14:11:06 +0200 Subject: [PATCH 3/3] fix linting --- Content/DataProviderResolver/PageDataProviderResolver.php | 2 +- Content/DataProviderResolver/SnippetDataProviderResolver.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Content/DataProviderResolver/PageDataProviderResolver.php b/Content/DataProviderResolver/PageDataProviderResolver.php index 12cbe3d..1dc22e7 100644 --- a/Content/DataProviderResolver/PageDataProviderResolver.php +++ b/Content/DataProviderResolver/PageDataProviderResolver.php @@ -102,7 +102,7 @@ public function resolve( $pageIds = []; foreach ($providerResult->getItems() as $resultItem) { - $pageIds[] = $resultItem->getId(); + $pageIds[] = (string) $resultItem->getId(); } /** @var PropertyParameter[] $propertiesParamValue */ diff --git a/Content/DataProviderResolver/SnippetDataProviderResolver.php b/Content/DataProviderResolver/SnippetDataProviderResolver.php index 9744247..b9d217c 100644 --- a/Content/DataProviderResolver/SnippetDataProviderResolver.php +++ b/Content/DataProviderResolver/SnippetDataProviderResolver.php @@ -95,7 +95,7 @@ public function resolve( $snippetIds = []; foreach ($providerResult->getItems() as $resultItem) { - $snippetIds[] = $resultItem->getId(); + $snippetIds[] = (string) $resultItem->getId(); } /** @var PropertyParameter[] $propertiesParamValue */