forked from algolia/algoliasearch-magento-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenderingCacheContextPlugin.php
executable file
·61 lines (52 loc) · 2.11 KB
/
RenderingCacheContextPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace Algolia\AlgoliaSearch\Plugin;
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\App\Request\Http;
use Magento\Store\Model\StoreManagerInterface;
/**
* The purpose of this class is to render different cached versions of the pages according to the user agent.
* If the "prevent backend rendering" configuration is turned on, we don't want to render the results on the backend
* side only for humans but we want to do it for the robots (configured in the "advanced" section of the extension).
* So with this plugin, two versions of the pages are cached : one for humans, and one for robots.
*/
class RenderingCacheContextPlugin
{
const RENDERING_CONTEXT = 'rendering_context';
const RENDERING_WITH_BACKEND = 'with_backend';
const RENDERING_WITHOUT_BACKEND = 'without_backend';
private $configHelper;
private $storeManager;
private $request;
public function __construct(
ConfigHelper $configHelper,
StoreManagerInterface $storeManager,
Http $request
) {
$this->configHelper = $configHelper;
$this->storeManager = $storeManager;
$this->request = $request;
}
/**
* Add Rendering context for caching purposes
* (If the prevent rendering configuration is enabled and the user agent has no white card to display it,
* we set a different page variation, and the FPC stores a different cached page)
*
* @param HttpContext $subject
*
* @return array
*/
public function beforeGetVaryString(HttpContext $subject)
{
$storeId = $this->storeManager->getStore()->getId();
if (! ($this->request->getControllerName() === 'category'
&& $this->configHelper->replaceCategories($storeId) === true)) {
return [];
}
$context = $this->configHelper->preventBackendRendering() ?
self::RENDERING_WITHOUT_BACKEND :
self::RENDERING_WITH_BACKEND;
$subject->setValue(self::RENDERING_CONTEXT, $context, self::RENDERING_WITH_BACKEND);
return [];
}
}