Skip to content

Commit 7047337

Browse files
author
Joan He
authored
Merge pull request #743 from magento-okapis/MAGETWO-57795-Varnish
[Okapis] Varnish Updates
2 parents ac07ef1 + 0515f4e commit 7047337

File tree

31 files changed

+724
-274
lines changed

31 files changed

+724
-274
lines changed

app/code/Magento/Backend/Model/View/Result/Page.php

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,6 @@
1010

1111
class Page extends View\Result\Page
1212
{
13-
/**
14-
* Constructor
15-
*
16-
* @param View\Element\Template\Context $context
17-
* @param View\LayoutFactory $layoutFactory
18-
* @param View\Layout\ReaderPool $layoutReaderPool
19-
* @param Translate\InlineInterface $translateInline
20-
* @param View\Layout\BuilderFactory $layoutBuilderFactory
21-
* @param View\Layout\GeneratorPool $generatorPool
22-
* @param View\Page\Config\RendererFactory $pageConfigRendererFactory
23-
* @param View\Page\Layout\Reader $pageLayoutReader
24-
* @param string $template
25-
*/
26-
public function __construct(
27-
View\Element\Template\Context $context,
28-
View\LayoutFactory $layoutFactory,
29-
View\Layout\ReaderPool $layoutReaderPool,
30-
Translate\InlineInterface $translateInline,
31-
View\Layout\BuilderFactory $layoutBuilderFactory,
32-
View\Layout\GeneratorPool $generatorPool,
33-
View\Page\Config\RendererFactory $pageConfigRendererFactory,
34-
View\Page\Layout\Reader $pageLayoutReader,
35-
$template
36-
) {
37-
parent::__construct(
38-
$context,
39-
$layoutFactory,
40-
$layoutReaderPool,
41-
$translateInline,
42-
$layoutBuilderFactory,
43-
$generatorPool,
44-
$pageConfigRendererFactory,
45-
$pageLayoutReader,
46-
$template
47-
);
48-
}
49-
5013
/**
5114
* Define active menu item in menu block
5215
*

app/code/Magento/Catalog/Controller/Category/View.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,17 @@ public function execute()
183183
if (!$hasChildren) {
184184
// Two levels removed from parent. Need to add default page type.
185185
$parentType = strtok($type, '_');
186-
$page->addPageLayoutHandles(['type' => $parentType]);
186+
$page->addPageLayoutHandles(['type' => $parentType], null, false);
187187
}
188-
$page->addPageLayoutHandles(['type' => $type, 'id' => $category->getId()]);
188+
$page->addPageLayoutHandles(['type' => $type], null, false);
189+
$page->addPageLayoutHandles(['id' => $category->getId()]);
189190

190191
// apply custom layout update once layout is loaded
191192
$layoutUpdates = $settings->getLayoutUpdates();
192193
if ($layoutUpdates && is_array($layoutUpdates)) {
193194
foreach ($layoutUpdates as $layoutUpdate) {
194195
$page->addUpdate($layoutUpdate);
195-
$page->addPageLayoutHandles(['layout_update' => md5($layoutUpdate)]);
196+
$page->addPageLayoutHandles(['layout_update' => md5($layoutUpdate)], null, false);
196197
}
197198
}
198199

app/code/Magento/Catalog/Helper/Product/View.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,18 @@ public function initProductLayout(ResultPage $resultPage, $product, $params = nu
122122
// Load default page handles and page configurations
123123
if ($params && $params->getBeforeHandles()) {
124124
foreach ($params->getBeforeHandles() as $handle) {
125-
$resultPage->addPageLayoutHandles(
126-
['id' => $product->getId(), 'sku' => $urlSafeSku, 'type' => $product->getTypeId()],
127-
$handle
128-
);
125+
$resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku], $handle);
126+
$resultPage->addPageLayoutHandles(['type' => $product->getTypeId()], $handle, false);
129127
}
130128
}
131129

132-
$resultPage->addPageLayoutHandles(
133-
['id' => $product->getId(), 'sku' => $urlSafeSku, 'type' => $product->getTypeId()]
134-
);
130+
$resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku]);
131+
$resultPage->addPageLayoutHandles(['type' => $product->getTypeId()], null, false);
135132

136133
if ($params && $params->getAfterHandles()) {
137134
foreach ($params->getAfterHandles() as $handle) {
138-
$resultPage->addPageLayoutHandles(
139-
['id' => $product->getId(), 'sku' => $urlSafeSku, 'type' => $product->getTypeId()],
140-
$handle
141-
);
135+
$resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku], $handle);
136+
$resultPage->addPageLayoutHandles(['type' => $product->getTypeId()], $handle, false);
142137
}
143138
}
144139

app/code/Magento/ConfigurableImportExport/Model/Import/Product/Type/Configurable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,13 @@ protected function _loadSkuSuperDataForBunch(array $bunch)
388388
)->joinLeft(
389389
['o' => $optionTable],
390390
$this->connection->quoteIdentifier(
391-
'o.attribute_id'
391+
'm.attribute_id'
392392
) . ' = ' . $this->connection->quoteIdentifier(
393393
'o.attribute_id'
394394
),
395395
['option_id']
396396
)->where(
397-
'product_id IN ( ? )',
397+
'm.product_id IN ( ? )',
398398
$productIds
399399
);
400400

app/code/Magento/ConfigurableImportExport/Test/Unit/Model/Import/Product/Type/ConfigurableTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ protected function setUp()
183183
'fetchPairs',
184184
'joinLeft',
185185
'insertOnDuplicate',
186+
'quoteIdentifier',
186187
'delete',
187188
'quoteInto'
188189
],
@@ -519,6 +520,10 @@ public function testSaveData()
519520
[$this->productEntityLinkField => 9, 'type_id' => 'configurable', 'attr_set_code' => 'Default'],
520521
]));
521522

523+
// at(0) is select() call, quoteIdentifier() is invoked at(1) and at(2)
524+
$this->_connection->expects($this->at(1))->method('quoteIdentifier')->with('m.attribute_id')->willReturn('a');
525+
$this->_connection->expects($this->at(2))->method('quoteIdentifier')->with('o.attribute_id')->willReturn('b');
526+
522527
$this->_connection->expects($this->any())->method('select')->will($this->returnValue($this->select));
523528
$this->_connection->expects($this->any())->method('fetchAll')->with($this->select)->will($this->returnValue([
524529
['attribute_id' => 131, 'product_id' => 1, 'option_id' => 1, 'product_super_attribute_id' => 131],

app/code/Magento/PageCache/Block/System/Config/Form/Field/Export/Varnish3.php renamed to app/code/Magento/PageCache/Block/System/Config/Form/Field/Export/Varnish5.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* Class Export
1010
*/
11-
class Varnish3 extends \Magento\PageCache\Block\System\Config\Form\Field\Export
11+
class Varnish5 extends \Magento\PageCache\Block\System\Config\Form\Field\Export
1212
{
1313
/**
1414
* Return Varnish version to this class
@@ -17,6 +17,6 @@ class Varnish3 extends \Magento\PageCache\Block\System\Config\Form\Field\Export
1717
*/
1818
public function getVarnishVersion()
1919
{
20-
return 3;
20+
return 5;
2121
}
2222
}

app/code/Magento/PageCache/Controller/Adminhtml/PageCache/ExportVarnishConfig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public function execute()
4545
$fileName = 'varnish.vcl';
4646
$varnishVersion = $this->getRequest()->getParam('varnish');
4747
switch ($varnishVersion) {
48-
case 3:
49-
$content = $this->config->getVclFile(\Magento\PageCache\Model\Config::VARNISH_3_CONFIGURATION_PATH);
48+
case 5:
49+
$content = $this->config->getVclFile(\Magento\PageCache\Model\Config::VARNISH_5_CONFIGURATION_PATH);
5050
break;
5151
default:
5252
$content = $this->config->getVclFile(\Magento\PageCache\Model\Config::VARNISH_4_CONFIGURATION_PATH);

app/code/Magento/PageCache/Model/Config.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class Config
4141

4242
const XML_VARNISH_PAGECACHE_BACKEND_HOST = 'system/full_page_cache/varnish/backend_host';
4343

44+
const XML_VARNISH_PAGECACHE_GRACE_PERIOD = 'system/full_page_cache/varnish/grace_period';
45+
4446
const XML_VARNISH_PAGECACHE_DESIGN_THEME_REGEX = 'design/theme/ua_regexp';
4547

4648
/**
@@ -49,9 +51,9 @@ class Config
4951
protected $_scopeConfig;
5052

5153
/**
52-
* XML path to Varnish 3 config template path
54+
* XML path to Varnish 5 config template path
5355
*/
54-
const VARNISH_3_CONFIGURATION_PATH = 'system/full_page_cache/varnish3/path';
56+
const VARNISH_5_CONFIGURATION_PATH = 'system/full_page_cache/varnish5/path';
5557

5658
/**
5759
* XML path to Varnish 4 config template path
@@ -138,14 +140,8 @@ public function getVclFile($vclTemplatePath)
138140
protected function _getReplacements()
139141
{
140142
return [
141-
'/* {{ host }} */' => $this->_scopeConfig->getValue(
142-
self::XML_VARNISH_PAGECACHE_BACKEND_HOST,
143-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
144-
),
145-
'/* {{ port }} */' => $this->_scopeConfig->getValue(
146-
self::XML_VARNISH_PAGECACHE_BACKEND_PORT,
147-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
148-
),
143+
'/* {{ host }} */' => $this->_scopeConfig->getValue(self::XML_VARNISH_PAGECACHE_BACKEND_HOST),
144+
'/* {{ port }} */' => $this->_scopeConfig->getValue(self::XML_VARNISH_PAGECACHE_BACKEND_PORT),
149145
'/* {{ ips }} */' => $this->_getAccessList(),
150146
'/* {{ design_exceptions_code }} */' => $this->_getDesignExceptions(),
151147
// http headers get transformed by php `X-Forwarded-Proto: https`
@@ -154,11 +150,9 @@ protected function _getReplacements()
154150
'/* {{ ssl_offloaded_header }} */' => str_replace(
155151
'_',
156152
'-',
157-
$this->_scopeConfig->getValue(
158-
\Magento\Framework\HTTP\PhpEnvironment\Request::XML_PATH_OFFLOADER_HEADER,
159-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
160-
)
161-
)
153+
$this->_scopeConfig->getValue(\Magento\Framework\HTTP\PhpEnvironment\Request::XML_PATH_OFFLOADER_HEADER)
154+
),
155+
'/* {{ grace_period }} */' => $this->_scopeConfig->getValue(self::XML_VARNISH_PAGECACHE_GRACE_PERIOD)
162156
];
163157
}
164158

@@ -176,10 +170,7 @@ protected function _getAccessList()
176170
{
177171
$result = '';
178172
$tpl = " \"%s\";";
179-
$accessList = $this->_scopeConfig->getValue(
180-
self::XML_VARNISH_PAGECACHE_ACCESS_LIST,
181-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
182-
);
173+
$accessList = $this->_scopeConfig->getValue(self::XML_VARNISH_PAGECACHE_ACCESS_LIST);
183174
if (!empty($accessList)) {
184175
$result = [];
185176
$ips = explode(',', $accessList);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\PageCache\Model\Layout;
7+
8+
use Magento\Framework\View\EntitySpecificHandlesList;
9+
10+
/**
11+
* Plugin for @see \Magento\Framework\View\Model\Layout\Merge
12+
*/
13+
class MergePlugin
14+
{
15+
/**
16+
* @var EntitySpecificHandlesList
17+
*/
18+
private $entitySpecificHandlesList;
19+
20+
/**
21+
* Constructor
22+
*
23+
* @param EntitySpecificHandlesList $entitySpecificHandlesList
24+
*/
25+
public function __construct(
26+
EntitySpecificHandlesList $entitySpecificHandlesList
27+
) {
28+
$this->entitySpecificHandlesList = $entitySpecificHandlesList;
29+
}
30+
31+
/**
32+
* Make sure that page specific handles (those which contain entity ID) do not have any declarations of ESI blocks
33+
*
34+
* ESI blocks cannot be declared for page specific handles, otherwise they will not be shared between pages
35+
*
36+
* @param \Magento\Framework\View\Model\Layout\Merge $subject
37+
* @param string $handle
38+
* @param string $updateXml
39+
* @return array|null
40+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
41+
*/
42+
public function beforeValidateUpdate(\Magento\Framework\View\Model\Layout\Merge $subject, $handle, $updateXml)
43+
{
44+
if (in_array($handle, $this->entitySpecificHandlesList->getHandles())
45+
&& (strpos($updateXml, 'ttl=') !== false)
46+
) {
47+
throw new \LogicException(
48+
"Handle '{$handle}' must not contain blocks with 'ttl' attribute specified. "
49+
. "Otherwise, these blocks will be treated as ESI by Varnish, however will not be shared between pages "
50+
. "because handle '{$handle}' is not generic. Such blocks will not be rendered on the page"
51+
);
52+
}
53+
return null;
54+
}
55+
}

app/code/Magento/PageCache/Observer/ProcessLayoutRenderElement.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\PageCache\Observer;
88

99
use Magento\Framework\Event\ObserverInterface;
10+
use Magento\Framework\View\EntitySpecificHandlesList;
1011

1112
class ProcessLayoutRenderElement implements ObserverInterface
1213
{
@@ -15,30 +16,40 @@ class ProcessLayoutRenderElement implements ObserverInterface
1516
*
1617
* @var \Magento\PageCache\Model\Config
1718
*/
18-
protected $_config;
19+
private $_config;
1920

2021
/**
2122
* Is varnish enabled flag
2223
*
2324
* @var bool
2425
*/
25-
protected $isVarnishEnabled;
26+
private $isVarnishEnabled;
2627

2728
/**
2829
* Is full page cache enabled flag
2930
*
3031
* @var bool
3132
*/
32-
protected $isFullPageCacheEnabled;
33+
private $isFullPageCacheEnabled;
34+
35+
/**
36+
* @var EntitySpecificHandlesList
37+
*/
38+
private $entitySpecificHandlesList;
3339

3440
/**
3541
* Class constructor
3642
*
3743
* @param \Magento\PageCache\Model\Config $config
44+
* @param EntitySpecificHandlesList $entitySpecificHandlesList
3845
*/
39-
public function __construct(\Magento\PageCache\Model\Config $config)
40-
{
46+
public function __construct(
47+
\Magento\PageCache\Model\Config $config,
48+
EntitySpecificHandlesList $entitySpecificHandlesList = null
49+
) {
4150
$this->_config = $config;
51+
$this->entitySpecificHandlesList = $entitySpecificHandlesList
52+
?: \Magento\Framework\App\ObjectManager::getInstance()->get(EntitySpecificHandlesList::class);
4253
}
4354

4455
/**
@@ -48,15 +59,17 @@ public function __construct(\Magento\PageCache\Model\Config $config)
4859
* @param \Magento\Framework\View\Layout $layout
4960
* @return string
5061
*/
51-
protected function _wrapEsi(
62+
private function _wrapEsi(
5263
\Magento\Framework\View\Element\AbstractBlock $block,
5364
\Magento\Framework\View\Layout $layout
5465
) {
66+
$handles = $layout->getUpdate()->getHandles();
67+
$pageSpecificHandles = $this->entitySpecificHandlesList->getHandles();
5568
$url = $block->getUrl(
5669
'page_cache/block/esi',
5770
[
5871
'blocks' => json_encode([$block->getNameInLayout()]),
59-
'handles' => json_encode($layout->getUpdate()->getHandles())
72+
'handles' => json_encode(array_values(array_diff($handles, $pageSpecificHandles)))
6073
]
6174
);
6275
// Varnish does not support ESI over HTTPS must change to HTTP
@@ -69,7 +82,7 @@ protected function _wrapEsi(
6982
*
7083
* @return bool
7184
*/
72-
protected function isFullPageCacheEnabled()
85+
private function isFullPageCacheEnabled()
7386
{
7487
if ($this->isFullPageCacheEnabled === null) {
7588
$this->isFullPageCacheEnabled = $this->_config->isEnabled();
@@ -82,7 +95,7 @@ protected function isFullPageCacheEnabled()
8295
*
8396
* @return bool
8497
*/
85-
protected function isVarnishEnabled()
98+
private function isVarnishEnabled()
8699
{
87100
if ($this->isVarnishEnabled === null) {
88101
$this->isVarnishEnabled = ($this->_config->getType() == \Magento\PageCache\Model\Config::VARNISH);

0 commit comments

Comments
 (0)