response = $response; $this->config = $config; $this->maintenanceMode = $maintenanceMode; $this->pageCacheTagsPreprocessor = $pageCacheTagsPreprocessor ?? ObjectManager::getInstance()->get(PageCacheTagsPreprocessorInterface::class); } /** * Set appropriate Cache-Control headers. * * We have to set public headers in order to tell Varnish and Builtin app that page should be cached * * @param Layout $subject * @return void */ public function afterGenerateElements(Layout $subject) { if ($subject->isCacheable() && !$this->maintenanceMode->isOn() && $this->config->isEnabled()) { $this->response->setPublicHeaders($this->config->getTtl()); } } /** * Retrieve all identities from blocks for further cache invalidation. * * @param Layout $subject * @param mixed $result * @return mixed */ public function afterGetOutput(Layout $subject, $result) { if ($subject->isCacheable() && $this->config->isEnabled()) { $tags = []; $isVarnish = $this->config->getType() === Config::VARNISH; foreach ($subject->getAllBlocks() as $block) { if ($block instanceof IdentityInterface) { $isEsiBlock = $block->getTtl() > 0; if ($isVarnish && $isEsiBlock) { continue; } $tags[] = $block->getIdentities(); } } $tags = array_unique(array_merge([], ...$tags)); $tags = $this->pageCacheTagsPreprocessor->process($tags); $this->response->setHeader('X-Magento-Tags', implode(',', $tags)); } return $result; } }