-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for Varnish ESI blocks to be interpreted separately
- Loading branch information
1 parent
8302864
commit b32a758
Showing
6 changed files
with
150 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Yireo\NextGenImages\Observer; | ||
|
||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\Framework\View\LayoutInterface; | ||
use Yireo\NextGenImages\Config\Config; | ||
use Yireo\NextGenImages\Util\HtmlReplacer; | ||
use Yireo\NextGenImages\Util\ShouldModifyOutput; | ||
|
||
/** | ||
* @todo Add integration test for URL /page_cache/block/esi/blocks/[%22catalog.topnav%22]/handles/WyJkZWZhdWx0Il0= | ||
*/ | ||
class ReplaceBlockHtml implements ObserverInterface | ||
{ | ||
/** | ||
* @var HtmlReplacer | ||
*/ | ||
private $htmlReplacer; | ||
|
||
/** | ||
* @var ShouldModifyOutput | ||
*/ | ||
private $shouldModifyOutput; | ||
|
||
/** | ||
* @var LayoutInterface | ||
*/ | ||
private $layout; | ||
|
||
/** | ||
* @var Config | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* ReplaceTags constructor. | ||
* | ||
* @param HtmlReplacer $htmlReplacer | ||
* @param ShouldModifyOutput $shouldModifyOutput | ||
* @param LayoutInterface $layout | ||
* @param Config $config | ||
*/ | ||
public function __construct( | ||
HtmlReplacer $htmlReplacer, | ||
ShouldModifyOutput $shouldModifyOutput, | ||
LayoutInterface $layout, | ||
Config $config | ||
) { | ||
$this->htmlReplacer = $htmlReplacer; | ||
$this->shouldModifyOutput = $shouldModifyOutput; | ||
$this->layout = $layout; | ||
$this->config = $config; | ||
} | ||
|
||
public function execute(Observer $observer) | ||
{ | ||
$block = $observer->getEvent()->getBlock(); | ||
if (!$block->getData('ttl')) { | ||
return; | ||
} | ||
|
||
if (!$this->config->hasFullPageCacheEnabled($this->layout)) { | ||
return; | ||
} | ||
|
||
if ($this->shouldModifyOutput->shouldModifyOutput($this->layout) === false) { | ||
return; | ||
} | ||
|
||
$transport = $observer->getEvent()->getTransport(); | ||
$html = $this->htmlReplacer->replace($transport->getHtml()); | ||
$transport->setHtml($html); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Yireo\NextGenImages\Util; | ||
|
||
use Magento\Framework\View\LayoutInterface; | ||
use Yireo\NextGenImages\Config\Config; | ||
|
||
class ShouldModifyOutput | ||
{ | ||
/** | ||
* @var Config | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @param Config $config | ||
*/ | ||
public function __construct( | ||
Config $config | ||
) { | ||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* @param LayoutInterface $layout | ||
* @return bool | ||
*/ | ||
public function shouldModifyOutput(LayoutInterface $layout): bool | ||
{ | ||
if (!$this->config->enabled()) { | ||
return false; | ||
} | ||
|
||
$handles = $layout->getUpdate()->getHandles(); | ||
if (empty($handles)) { | ||
return false; | ||
} | ||
|
||
foreach ($handles as $handle) { | ||
if (strstr($handle, '_email_')) { | ||
return false; | ||
} | ||
} | ||
|
||
$skippedHandles = [ | ||
'webp_skip', | ||
'nextgenimages_skip', | ||
]; | ||
|
||
if (array_intersect($skippedHandles, $handles)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> | ||
<event name="view_block_abstract_to_html_after"> | ||
<observer name="Yireo_NextGenImages::replaceBlockHtml" instance="Yireo\NextGenImages\Observer\ReplaceBlockHtml" /> | ||
</event> | ||
</config> |