Skip to content
Open
Changes from all 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
27 changes: 13 additions & 14 deletions Cms/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class CacheManager
{
private $cacheItemPool;

private $requestStack;

private $isCacheActive;
Expand All @@ -26,7 +26,7 @@ public function __construct(TagAwareAdapterInterface $cacheItemPool, RequestStac
$this->isCacheActive = $isCacheActive;
}

public function isCacheable(BlockTypeInterface $blockType) : bool
public function isCacheable(BlockTypeInterface $blockType): bool
{
if (!$blockType instanceof CacheableBlockInterface) {
return false;
Expand All @@ -35,15 +35,15 @@ public function isCacheable(BlockTypeInterface $blockType) : bool
return $this->isCacheActive && $this->requestStack->getCurrentRequest()->isMethod('GET');
}

private function getCacheConfig(BlockTypeInterface $blockType, Block $block) : array
private function getCacheConfig(BlockTypeInterface $blockType, Block $block): array
{
$resolver = new OptionsResolver();
$resolver->setRequired('cache_key');
$resolver->setRequired('expires_after');
$resolver->setRequired('vary');

$resolver->setDefaults([
'cache_key' => 'block_'.$blockType->getType().'_'.$block->getId(),
'cache_key' => 'block_' . $blockType->getType() . '_' . $block->getId(),
'expires_after' => '5 minutes',
'vary' => '',
'vary_path_info' => true,
Expand All @@ -55,22 +55,22 @@ private function getCacheConfig(BlockTypeInterface $blockType, Block $block) : a
return $resolver->resolve($block->getConfiguration()['cache'] ?? []);
}

private function getCacheKeyWithVariation(array $config) : string
private function getCacheKeyWithVariation(array $config): string
{
$request = $this->requestStack->getCurrentRequest();
$key = $config['cache_key'];

if ($config['vary_path_info']) {
$key .= Urlizer::urlize($request->getPathInfo()).'__';
$key .= Urlizer::urlize($request->getPathInfo()) . '__';
}

if ($config['vary_query_string'] && $request->getQueryString()) {
$key .= 'qs_'.Urlizer::urlize($request->getQueryString()).'__';
$key .= 'qs_' . Urlizer::urlize($request->getQueryString()) . '__';
}

foreach (explode(',',$config['vary']) as $vary) {
foreach (explode(',', $config['vary']) as $vary) {
$vary = trim($vary);
$key .= $vary.'_'.Urlizer::urlize($request->headers->get($vary)).'__';
$key .= $vary . '_' . Urlizer::urlize($request->headers->get($vary)) . '__';
}

return $key;
Expand All @@ -94,10 +94,10 @@ public function write(BlockTypeInterface $blockType, Block $block, $content)
$config = $this->getCacheConfig($blockType, $block);

$cache = $this->cacheItemPool->getItem($this->getCacheKeyWithVariation($config));
$cache->expiresAfter(\DateInterval::createFromDateString($config['expires_after']));
$cache->expiresAfter(\DateInterval::createFromDateString($config['expires_after'] ?? '1 second'));
$cache->tag($config['cache_key']);

if ($content instanceof Response) {
if ($content instanceof Response) {
if (!$content->isCacheable()) {
return;
}
Expand All @@ -118,7 +118,6 @@ public function invalidateByTag(BlockTypeInterface $blockType, Block $block)

$config = $this->getCacheConfig($blockType, $block);

$this->cacheItemPool->invalidateTags([ $config['cache_key'] ]);
$this->cacheItemPool->invalidateTags([$config['cache_key']]);
}

}
}