Skip to content

Commit 3df6df9

Browse files
committed
added fallbacks to avoid errors
1 parent 791be0d commit 3df6df9

File tree

3 files changed

+70
-24
lines changed

3 files changed

+70
-24
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "infrangible/m2-cache-usage",
33
"description": "Infrangible Magento 2 Cache Usage",
44
"type": "magento2-module",
5-
"version": "2.4.0",
5+
"version": "2.4.1",
66
"license": "MIT",
77
"keywords": [
88
"Magento"

src/Model/Cache.php

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,27 +138,45 @@ public function setBlocksData(array $blocks): void
138138
foreach ($blocks as $blockName => $blockData) {
139139
$block = new Block();
140140

141-
if (array_key_exists('layout_name', $blockData)) {
141+
if (array_key_exists(
142+
'layout_name',
143+
$blockData
144+
)) {
142145
$block->setLayoutName($blockData[ 'layout_name' ]);
143146
}
144147

145-
if (array_key_exists('class_name', $blockData)) {
148+
if (array_key_exists(
149+
'class_name',
150+
$blockData
151+
)) {
146152
$block->setClassName($blockData[ 'class_name' ]);
147153
}
148154

149-
if (array_key_exists('template_name', $blockData)) {
155+
if (array_key_exists(
156+
'template_name',
157+
$blockData
158+
)) {
150159
$block->setTemplateName($blockData[ 'template_name' ]);
151160
}
152161

153-
if (array_key_exists('uncacheable', $blockData)) {
162+
if (array_key_exists(
163+
'uncacheable',
164+
$blockData
165+
)) {
154166
$block->setUncacheable($blockData[ 'uncacheable' ]);
155167
}
156168

157-
if (array_key_exists('cached', $blockData)) {
169+
if (array_key_exists(
170+
'cached',
171+
$blockData
172+
)) {
158173
$block->setCached($blockData[ 'cached' ]);
159174
}
160175

161-
if (array_key_exists('uncached', $blockData)) {
176+
if (array_key_exists(
177+
'uncached',
178+
$blockData
179+
)) {
162180
$block->setUncached($blockData[ 'uncached' ]);
163181
}
164182

@@ -174,7 +192,7 @@ public function getDefaultBlocks(): array
174192
$blocks = [];
175193

176194
foreach ($this->getBlocks() as $block) {
177-
if ( ! $block->isUncacheable() && ! $block->isCached() && ! $block->isUncached()) {
195+
if (! $block->isUncacheable() && ! $block->isCached() && ! $block->isUncached()) {
178196
$blocks[ $block->getLayoutName() ] = $block;
179197
}
180198
}
@@ -234,11 +252,16 @@ public function addBlockData(
234252
LayoutInterface $layout,
235253
string $name,
236254
string $className,
237-
string $templateName): Block
238-
{
255+
string $templateName
256+
): Block {
239257
$cacheBlock = $this->getBlock($name);
240258

241-
$cacheBlock->setLayoutName($this->getLayoutName($layout, $name));
259+
$cacheBlock->setLayoutName(
260+
$this->getLayoutName(
261+
$layout,
262+
$name
263+
)
264+
);
242265
$cacheBlock->setClassName($className);
243266
$cacheBlock->setTemplateName($templateName);
244267

@@ -263,12 +286,21 @@ public function addBlock(AbstractBlock $block): Block
263286
if ($block instanceof Template) {
264287
$template = $block->getTemplate();
265288

266-
if ( ! empty($template)) {
289+
if (! empty($template)) {
267290
$templateName = $block->getTemplateFile();
268291
}
269292
}
270293

271-
return $this->addBlockData($block->getLayout(), $blockName, get_class($block), $templateName);
294+
if ($templateName === false) {
295+
$templateName = '-';
296+
}
297+
298+
return $this->addBlockData(
299+
$block->getLayout(),
300+
$blockName,
301+
get_class($block),
302+
$templateName
303+
);
272304
}
273305

274306
public function generateBlockName(AbstractBlock $block): string
@@ -322,7 +354,10 @@ public function addUncachedBlock(AbstractBlock $block): Block
322354

323355
public function getBlock(string $blockName): Block
324356
{
325-
if (array_key_exists($blockName, $this->blocks)) {
357+
if (array_key_exists(
358+
$blockName,
359+
$this->blocks
360+
)) {
326361
return $this->blocks[ $blockName ];
327362
}
328363

@@ -337,7 +372,14 @@ protected function getLayoutName(LayoutInterface $layout, string $nameInLayout):
337372
return $nameInLayout;
338373
}
339374

340-
return sprintf('%s/%s', $this->getLayoutName($layout, $parentName), $nameInLayout);
375+
return sprintf(
376+
'%s/%s',
377+
$this->getLayoutName(
378+
$layout,
379+
$parentName
380+
),
381+
$nameInLayout
382+
);
341383
}
342384

343385
public function getStarted(): float

src/Plugin/Framework/View/Layout.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
namespace Infrangible\CacheUsage\Plugin\Framework\View;
66

7+
use Infrangible\CacheUsage\Model\Cache;
8+
use Magento\Framework\View\Element\AbstractBlock;
79
use Magento\Framework\View\Element\Template;
810
use Magento\Framework\View\Layout\Element;
9-
use Infrangible\CacheUsage\Model\Cache;
1011

1112
/**
1213
* @author Andreas Knollmann
@@ -18,17 +19,11 @@ class Layout
1819
/** @var Cache */
1920
protected $cache;
2021

21-
/**
22-
* @param Cache $cache
23-
*/
2422
public function __construct(Cache $cache)
2523
{
2624
$this->cache = $cache;
2725
}
2826

29-
/**
30-
* @param \Magento\Framework\View\Layout $subject
31-
*/
3227
public function afterGenerateElements(\Magento\Framework\View\Layout $subject)
3328
{
3429
if ($subject->isCacheable()) {
@@ -45,6 +40,10 @@ public function afterGenerateElements(\Magento\Framework\View\Layout $subject)
4540
$blockName = $element->getBlockName();
4641
$className = $element->getAttribute('class');
4742

43+
if ($className === null) {
44+
$className = AbstractBlock::class;
45+
}
46+
4847
$templateName = '-';
4948

5049
if ($subject->isBlock($blockName)) {
@@ -53,13 +52,18 @@ public function afterGenerateElements(\Magento\Framework\View\Layout $subject)
5352
if ($block instanceof Template) {
5453
$template = $block->getTemplate();
5554

56-
if ( ! empty($template)) {
55+
if (! empty($template)) {
5756
$templateName = $block->getTemplateFile();
5857
}
5958
}
6059
}
6160

62-
$cacheBlock = $this->cache->addBlockData($subject, $blockName, $className, $templateName);
61+
$cacheBlock = $this->cache->addBlockData(
62+
$subject,
63+
$blockName,
64+
$className,
65+
$templateName
66+
);
6367

6468
$cacheBlock->setUncacheable(true);
6569
}

0 commit comments

Comments
 (0)