-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathLayoutPlugin.php
110 lines (103 loc) · 3.49 KB
/
LayoutPlugin.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* Fastly CDN for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Fastly CDN for Magento End User License Agreement
* that is bundled with this package in the file LICENSE_FASTLY_CDN.txt.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Fastly CDN to newer
* versions in the future. If you wish to customize this module for your
* needs please refer to http://www.magento.com for more information.
*
* @category Fastly
* @package Fastly_Cdn
* @copyright Copyright (c) 2016 Fastly, Inc. (http://www.fastly.com)
* @license BSD, see LICENSE_FASTLY_CDN.txt
*/
namespace Fastly\Cdn\Model\Layout;
use \Fastly\Cdn\Model\Config;
/**
* Class LayoutPlugin
*/
class LayoutPlugin
{
/**
* @var \Magento\PageCache\Model\Config
*/
protected $config;
/**
* @var \Magento\Framework\App\ResponseInterface
*/
protected $response;
/**
* @var \Fastly\Cdn\Helper\CacheTags
*/
protected $cacheTags;
/**
* Constructor
*
* @param \Magento\Framework\App\ResponseInterface $response
* @param \Fastly\Cdn\Model\Config $config
* @param \Fastly\Cdn\Helper\CacheTags $cacheTags
*/
public function __construct(
\Magento\Framework\App\ResponseInterface $response,
Config $config,
\Fastly\Cdn\Helper\CacheTags $cacheTags
) {
$this->response = $response;
$this->config = $config;
$this->cacheTags = $cacheTags;
}
/**
* Set appropriate Cache-Control headers
* Set Fastly stale headers if configured
*
* @param \Magento\Framework\View\Layout $subject
* @param mixed $result
* @return mixed
*/
public function afterGenerateXml(\Magento\Framework\View\Layout $subject, $result)
{
// if subject is cacheable, FPC cache is enabled, Fastly module is chosen and general TTL is > 0
if ($subject->isCacheable() && $this->config->isEnabled()
&& $this->config->getType() == Config::FASTLY && $this->config->getTtl()) {
// get cache control header
$header = $this->response->getHeader('cache-control');
if (($header instanceof \Zend\Http\Header\HeaderInterface) && ($value = $header->getFieldValue())) {
// append stale values
if ($ttl = $this->config->getStaleTtl()) {
$value .= ', stale-while-revalidate=' . $ttl;
}
if ($ttl = $this->config->getStaleErrorTtl()) {
$value .= ', stale-if-error=' . $ttl;
}
// update cache control header
$this->response->setHeader($header->getFieldName(), $value, true);
}
}
return $result;
}
/**
* Adjust X-Magento-Tags for Fastly
*
* @param \Magento\Framework\View\Layout $subject
* @param mixed $result
* @return mixed
*/
public function afterGetOutput(\Magento\Framework\View\Layout $subject, $result)
{
if ($this->config->getType() == Config::FASTLY) {
// Fastly expects surrogate keys separated by space. replace existing header.
$header = $this->response->getHeader('X-Magento-Tags');
if ($header instanceof \Zend\Http\Header\HeaderInterface) {
$this->response->setHeader($header->getFieldName(), $this->cacheTags->convertCacheTags(str_replace(',', ' ', $header->getFieldValue())), true);
}
}
return $result;
}
}