-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLayoutGenerateBlocksAfter.php
67 lines (60 loc) · 2.11 KB
/
LayoutGenerateBlocksAfter.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
<?php
/**
* LiteMage
* @package LiteSpeed_LiteMage
* @copyright Copyright (c) LiteSpeed Technologies, Inc. All rights reserved. (https://www.litespeedtech.com)
* @license https://opensource.org/licenses/GPL-3.0
*/
namespace Litespeed\Litemage\Observer;
class LayoutGenerateBlocksAfter
implements \Magento\Framework\Event\ObserverInterface
{
/**
* @var \Litespeed\Litemage\Model\CacheControl
*/
protected $litemageCache;
/**
* Class constructor
*
* @param \Litespeed\Litemage\Model\CacheControl $litemageCache
*/
public function __construct(\Litespeed\Litemage\Model\CacheControl $litemageCache)
{
$this->litemageCache = $litemageCache;
}
/**
* Check if still cacheable
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
if (!$this->litemageCache->maybeCacheable()) {
return;
}
$layout = $observer->getEvent()->getLayout();
if (!$layout->isCacheable()) {
// not cacheable by layout, find out which blocks caused that for trouble shooting
if ($this->litemageCache->debugEnabled()) {
$nocache = '//' . \Magento\Framework\View\Layout\Element::TYPE_BLOCK . '[@cacheable="false"]';
$blocks = $layout->getUpdate()->asSimplexml()->xpath($nocache);
$notcacheable = [];
foreach ($blocks as $block) {
if ($block->getAttribute('cacheable') === 'false') {
$notcacheable[] = $block->attributes();
}
}
$str = print_r($notcacheable, true);
$shortmsg = 'Layout has uncacheable blocks ';
if (preg_match_all('/\[name\] => ([^\s]+)/', $str, $m)) {
$shortmsg .= implode(', ', $m[1]);
}
$msg = 'Observer LayoutGenerateBlocksAfter Blocks not cacheable ' . $str;
} else {
$msg = $shortmsg = 'layout blocks';
}
$this->litemageCache->setNotCacheable($msg, $shortmsg);
}
}
}