Skip to content

Commit 8e442f9

Browse files
committed
Move childRenderer class from Loki_Components to Loki_Base
1 parent 51ee8ca commit 8e442f9

5 files changed

Lines changed: 201 additions & 2 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Base\ViewModel\Block;
4+
5+
use Magento\Framework\App\State as AppState;
6+
use Magento\Framework\View\Element\AbstractBlock;
7+
use Magento\Framework\View\Element\Block\ArgumentInterface;
8+
use Magento\Framework\View\LayoutInterface;
9+
use Loki\Components\Component\ComponentViewModelInterface;
10+
use RuntimeException;
11+
12+
abstract class AbstractRenderer implements ArgumentInterface
13+
{
14+
protected ?AbstractBlock $ancestorBlock = null;
15+
16+
public function __construct(
17+
protected LayoutInterface $layout,
18+
protected AppState $appState,
19+
protected ChildCounter $childCounter,
20+
) {
21+
}
22+
23+
protected function populateBlock(
24+
AbstractBlock $block,
25+
array $data = []
26+
): void {
27+
$block->addData($data);
28+
$block->setAncestorBlock($this->ancestorBlock);
29+
$block->setUniqId($this->getUniqId($block, $data));
30+
31+
$viewModel = $this->ancestorBlock->getViewModel();
32+
if ($viewModel instanceof ComponentViewModelInterface) {
33+
$block->setViewModel($viewModel);
34+
}
35+
}
36+
37+
protected function getCounter(AbstractBlock $block): int
38+
{
39+
return $this->childCounter->getCounter($block->getNameInLayout());
40+
}
41+
42+
protected function getBlockAlias(
43+
?AbstractBlock $block = null,
44+
array $data = []
45+
): string {
46+
if ($block instanceof AbstractBlock) {
47+
$alias = $block->getAlias();
48+
if (!empty($alias)) {
49+
return $alias;
50+
}
51+
}
52+
53+
if (isset($data['alias'])) {
54+
return $data['alias'];
55+
}
56+
57+
if ($this->ancestorBlock instanceof AbstractBlock) {
58+
return 'block' . $this->getCounter($this->ancestorBlock);
59+
}
60+
61+
return '';
62+
}
63+
64+
protected function setNameInLayout(AbstractBlock $block): void
65+
{
66+
$alias = $this->getBlockAlias($block);
67+
$block->setNameInLayout(
68+
$this->ancestorBlock->getNameInLayout() . '.' . $alias
69+
);
70+
}
71+
72+
protected function getUniqId(AbstractBlock $block, array $data = []): string
73+
{
74+
$ancestorId = preg_replace('/([^a-zA-Z0-9\-]+)/', '-', $this->ancestorBlock->getNameInLayout());
75+
76+
if (isset($data['uniq'])) {
77+
$uniq = preg_replace('/([^a-zA-Z0-9\-]+)/', '-', $data['uniq']);
78+
return $ancestorId . '-' . $uniq;
79+
}
80+
81+
$blockParts = explode('.', $block->getNameInLayout());
82+
return $ancestorId . '-' . array_pop($blockParts);
83+
}
84+
85+
protected function isDeveloperMode(): bool
86+
{
87+
return $this->appState->getMode() === AppState::MODE_DEVELOPER;
88+
}
89+
}

ViewModel/Block/ChildCounter.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Base\ViewModel\Block;
4+
5+
class ChildCounter
6+
{
7+
private array $counters = [];
8+
9+
public function getCounter(string $childName): int
10+
{
11+
if (isset($this->counters[$childName])) {
12+
$this->counters[$childName]++;
13+
} else {
14+
$this->counters[$childName] = 0;
15+
}
16+
17+
return $this->counters[$childName];
18+
}
19+
}

ViewModel/Block/ChildRenderer.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Base\ViewModel\Block;
4+
5+
use InvalidArgumentException;
6+
use Magento\Framework\View\Element\AbstractBlock;
7+
use RuntimeException;
8+
9+
class ChildRenderer extends AbstractRenderer
10+
{
11+
public function all(
12+
AbstractBlock $parentBlock
13+
): string {
14+
$html = '';
15+
$childNames = $parentBlock->getChildNames();
16+
$children = [];
17+
18+
foreach ($childNames as $childName) {
19+
$childBlock = $parentBlock->getLayout()->getBlock($childName);
20+
if (false === $childBlock instanceof AbstractBlock) {
21+
if ($this->isDeveloperMode()) {
22+
$html .= '<!-- WARNING: No child found "' . $childName
23+
. '" -->';
24+
}
25+
26+
continue;
27+
}
28+
29+
$children[] = $childBlock;
30+
}
31+
32+
$sortedChildren = $this->sortBlocks($children);
33+
34+
foreach ($sortedChildren as $sortedChild) {
35+
$html .= $sortedChild->toHtml();
36+
}
37+
38+
return $html;
39+
}
40+
41+
public function get(
42+
AbstractBlock $ancestorBlock,
43+
string $blockAlias,
44+
array $data = [],
45+
): AbstractBlock {
46+
$this->ancestorBlock = $ancestorBlock;
47+
$block = $this->ancestorBlock->getChildBlock($blockAlias);
48+
49+
if (false === $block instanceof AbstractBlock) {
50+
throw new RuntimeException(
51+
(string)__(
52+
'No child alias "%1" for parent "%2"',
53+
$blockAlias,
54+
$this->ancestorBlock->getNameInLayout()
55+
)
56+
);
57+
}
58+
59+
$block->setAlias($blockAlias);
60+
$this->setNameInLayout($block);
61+
$this->populateBlock($block, $data);
62+
63+
return $block;
64+
}
65+
66+
public function html(
67+
AbstractBlock $ancestorBlock,
68+
string $blockAlias,
69+
array $data = []
70+
) {
71+
try {
72+
return (string)$this->get($ancestorBlock, $blockAlias, $data)
73+
->toHtml();
74+
} catch (RuntimeException|InvalidArgumentException $e) {
75+
if ($this->isDeveloperMode()) {
76+
return '<!-- WARNING: ' . $e->getMessage() . ' -->';
77+
}
78+
79+
return '';
80+
}
81+
}
82+
83+
private function sortBlocks(array $blocks): array
84+
{
85+
usort($blocks, function (AbstractBlock $blockA, AbstractBlock $blockB) {
86+
return (int)$blockA->getSortOrder() <=> (int)$blockB->getSortOrder();
87+
});
88+
89+
return $blocks;
90+
}
91+
}

view/base/templates/container.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
use Loki\Components\Util\Block\ChildRenderer;
4+
use Loki\Base\ViewModel\Block\ChildRenderer;
55
use Magento\Framework\Escaper;
66
use Loki\CssUtils\Util\CssClass;
77
use Magento\Framework\View\Element\Template;

view/base/templates/script/script-container.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
use Loki\Components\Util\Block\ChildRenderer;
4+
use Loki\Base\ViewModel\Block\ChildRenderer;
55
use Magento\Framework\View\Element\Template;
66

77
/** @version 1.1.12 */

0 commit comments

Comments
 (0)