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 */ diff --git a/Controller/SnippetAreaController.php b/Controller/SnippetAreaController.php index 79ee7da..a1f3127 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 (null !== $this->cacheLifetimeRequestStore) { + $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% + 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')); + } } }