Skip to content
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

Add support for CacheLifetime for scheduled blocks in SnippetAreaController #145

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions Controller/SnippetAreaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,6 +73,11 @@ class SnippetAreaController
*/
private $cacheLifetime;

/**
* @var CacheLifetimeRequestStore|null
*/
private $cacheLifetimeRequestStore;

public function __construct(
DefaultSnippetManagerInterface $defaultSnippetManager,
ContentMapperInterface $contentMapper,
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions Resources/config/controllers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<argument>%sulu_http_cache.cache.max_age%</argument>
<argument>%sulu_http_cache.cache.shared_max_age%</argument>
<argument>%sulu_headless.snippet_area.cache_lifetime%</argument>
<argument type="service" id="sulu_http_cache.cache_lifetime.request_store" on-invalid="null"/>
</service>
<service id="Sulu\Bundle\HeadlessBundle\Controller\SnippetAreaController"
alias="sulu_headless.controller.snippet_area" public="true"/>
Expand Down
47 changes: 47 additions & 0 deletions Tests/Application/config/templates/snippets/default-blocks.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" ?>
<template xmlns="http://schemas.sulu.io/template/template"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.sulu.io/template/template http://schemas.sulu.io/template/template-1.0.xsd">

<key>default-blocks</key>

<meta>
<title>Default Blocks</title>
</meta>

<properties>
<property name="title" type="text_line" mandatory="true">
<meta>
<title>Title</title>
</meta>

<tag name="sulu.node.name"/>
</property>

<property name="description" type="text_editor">
<meta>
<title>Description</title>
</meta>
</property>

<block name="blocks">
<types>
<type name="editor_image">
<meta>
<title lang="de">Editor</title>
<title lang="en">Editor</title>
</meta>

<properties>
<property name="article" type="text_editor">
<meta>
<title lang="de">Artikel</title>
<title lang="en">Article</title>
</meta>
</property>
</properties>
</type>
</types>
</block>
</properties>
</template>
64 changes: 63 additions & 1 deletion Tests/Functional/Controller/SnippetAreaControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<p>Article text</p>',
'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();
}

Expand All @@ -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 [
Expand All @@ -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,
];
}

/**
Expand All @@ -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);

Expand Down Expand Up @@ -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'));
}
}
}
Loading