Skip to content

Commit ac4934f

Browse files
author
Roman Ganin
committed
Merge branch 'MAGETWO-34276' of github.corp.ebay.com:magento-troll/magento2ce into MAGETWO-34276
2 parents fa01e9d + a9cfe44 commit ac4934f

File tree

2 files changed

+74
-30
lines changed

2 files changed

+74
-30
lines changed

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

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ class ProcessLayoutRenderElement
1616
protected $_config;
1717

1818
/**
19+
* Is varnish enabled flag
20+
*
21+
* @var bool
22+
*/
23+
protected $isVarnishEnabled;
24+
25+
/**
26+
* Is full page cache enabled flag
27+
*
28+
* @var bool
29+
*/
30+
protected $isFullPageCacheEnabled;
31+
32+
/**
33+
* Class constructor
34+
*
1935
* @param \Magento\PageCache\Model\Config $config
2036
*/
2137
public function __construct(\Magento\PageCache\Model\Config $config)
@@ -44,6 +60,32 @@ protected function _wrapEsi(
4460
return sprintf('<esi:include src="%s" />', $url);
4561
}
4662

63+
/**
64+
* Is full page cache enabled
65+
*
66+
* @return bool
67+
*/
68+
protected function isFullPageCacheEnabled()
69+
{
70+
if ($this->isFullPageCacheEnabled === null) {
71+
$this->isFullPageCacheEnabled = $this->_config->isEnabled();
72+
}
73+
return $this->isFullPageCacheEnabled;
74+
}
75+
76+
/**
77+
* Is varnish cache engine enabled
78+
*
79+
* @return bool
80+
*/
81+
protected function isVarnishEnabled()
82+
{
83+
if ($this->isVarnishEnabled === null) {
84+
$this->isVarnishEnabled = ($this->_config->getType() == \Magento\PageCache\Model\Config::VARNISH);
85+
}
86+
return $this->isVarnishEnabled;
87+
}
88+
4789
/**
4890
* Add comment cache containers to private blocks
4991
* Blocks are wrapped only if page is cacheable
@@ -56,15 +98,15 @@ public function execute(\Magento\Framework\Event\Observer $observer)
5698
$event = $observer->getEvent();
5799
/** @var \Magento\Framework\View\Layout $layout */
58100
$layout = $event->getLayout();
59-
if ($layout->isCacheable() && $this->_config->isEnabled()) {
101+
if ($this->isFullPageCacheEnabled() && $layout->isCacheable()) {
60102
$name = $event->getElementName();
103+
/** @var \Magento\Framework\View\Element\AbstractBlock $block */
61104
$block = $layout->getBlock($name);
62105
$transport = $event->getTransport();
63106
if ($block instanceof \Magento\Framework\View\Element\AbstractBlock) {
64107
$blockTtl = $block->getTtl();
65-
$varnishIsEnabledFlag = ($this->_config->getType() == \Magento\PageCache\Model\Config::VARNISH);
66108
$output = $transport->getData('output');
67-
if ($varnishIsEnabledFlag && isset($blockTtl)) {
109+
if (isset($blockTtl) && $this->isVarnishEnabled()) {
68110
$output = $this->_wrapEsi($block, $layout);
69111
} elseif ($block->isScopePrivate()) {
70112
$output = sprintf(

app/code/Magento/PageCache/Test/Unit/Model/Observer/ProcessLayoutRenderElementTest.php

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,29 @@ public function testExecute(
9999
$this->_configMock->expects($this->any())->method('isEnabled')->will($this->returnValue($cacheState));
100100

101101
if ($cacheState) {
102-
$eventMock->expects($this->once())->method('getElementName')->will($this->returnValue('blockName'));
103-
$eventMock->expects($this->once())->method('getTransport')->will($this->returnValue($this->_transport));
104-
$this->_layoutMock->expects($this->once())->method('isCacheable')->will($this->returnValue(true));
105-
106-
$this->_layoutMock->expects($this->any())->method('getUpdate')->will($this->returnSelf());
107-
$this->_layoutMock->expects($this->any())->method('getHandles')->will($this->returnValue([]));
108-
$this->_layoutMock->expects(
109-
$this->once()
110-
)->method(
111-
'getBlock'
112-
)->will(
113-
$this->returnValue($this->_blockMock)
114-
);
102+
$eventMock->expects($this->once())
103+
->method('getElementName')
104+
->will($this->returnValue('blockName'));
105+
106+
$eventMock->expects($this->once())
107+
->method('getTransport')
108+
->will($this->returnValue($this->_transport));
109+
110+
$this->_layoutMock->expects($this->once())
111+
->method('isCacheable')
112+
->will($this->returnValue(true));
113+
114+
$this->_layoutMock->expects($this->any())
115+
->method('getUpdate')
116+
->will($this->returnSelf());
117+
118+
$this->_layoutMock->expects($this->any())
119+
->method('getHandles')
120+
->will($this->returnValue([]));
121+
122+
$this->_layoutMock->expects($this->once())
123+
->method('getBlock')
124+
->will($this->returnValue($this->_blockMock));
115125

116126
if ($varnishIsEnabled) {
117127
$this->_blockMock->expects($this->once())
@@ -123,20 +133,12 @@ public function testExecute(
123133
->will($this->returnValue('page_cache/block/wrapesi/with/handles/and/other/stuff'));
124134
}
125135
if ($scopeIsPrivate) {
126-
$this->_blockMock->expects(
127-
$this->once()
128-
)->method(
129-
'getNameInLayout'
130-
)->will(
131-
$this->returnValue('testBlockName')
132-
);
133-
$this->_blockMock->expects(
134-
$this->once()
135-
)->method(
136-
'isScopePrivate'
137-
)->will(
138-
$this->returnValue($scopeIsPrivate)
139-
);
136+
$this->_blockMock->expects($this->once())
137+
->method('getNameInLayout')
138+
->will($this->returnValue('testBlockName'));
139+
$this->_blockMock->expects($this->once())
140+
->method('isScopePrivate')
141+
->will($this->returnValue($scopeIsPrivate));
140142
}
141143
$this->_configMock->expects($this->any())->method('getType')->will($this->returnValue($varnishIsEnabled));
142144
}

0 commit comments

Comments
 (0)