Skip to content

Commit e26f7c5

Browse files
committed
Refactor loading of layout
1 parent 4c8f934 commit e26f7c5

1 file changed

Lines changed: 39 additions & 11 deletions

File tree

Util/Controller/LayoutLoader.php

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,57 @@
33
namespace Loki\Components\Util\Controller;
44

55
use Loki\Components\Layout\LayoutHandlerComposite;
6+
use Magento\Framework\App\RequestInterface;
7+
use Magento\Framework\View\Layout\BuilderFactory;
8+
use Magento\Framework\View\Layout\GeneratorPool;
69
use Magento\Framework\View\LayoutInterface;
7-
use Magento\Framework\View\Result\PageFactory as ResultPageFactory;
10+
use Magento\Framework\View\Page\Config as PageConfig;
11+
use Magento\Framework\View\Page\Layout\Reader as PageLayoutReader;
812

913
class LayoutLoader
1014
{
1115
public function __construct(
12-
private readonly ResultPageFactory $resultPageFactory,
13-
private readonly LayoutHandlerComposite $layoutHandlerComposite
16+
private readonly LayoutInterface $layout,
17+
private readonly LayoutHandlerComposite $layoutHandlerComposite,
18+
private readonly BuilderFactory $layoutBuilderFactory,
19+
private readonly GeneratorPool $generatorPool,
20+
private readonly RequestInterface $request,
21+
private readonly PageConfig $pageConfig,
22+
private readonly PageLayoutReader $pageLayoutReader,
1423
) {
1524
}
1625

17-
public function load(array $handles = []): LayoutInterface
26+
public function load(array $handles = [], bool $initializeElements = true): LayoutInterface
1827
{
1928
$handles = $this->layoutHandlerComposite->getHandles($handles);
20-
$resultPage = $this->resultPageFactory->create(true);
2129

22-
if ($handles !== []) {
23-
foreach ($handles as $handle) {
24-
$handle = preg_replace('/([^a-z0-9\-\_]+)/', '', $handle);
25-
$resultPage->addHandle($handle);
26-
}
30+
$layout = $this->layout;
31+
$layout->setGeneratorPool($this->generatorPool);
32+
33+
$update = $layout->getUpdate();
34+
$update->addHandle('default');
35+
$update->addHandle(strtolower($this->request->getFullActionName()));
36+
37+
foreach ($handles as $handle) {
38+
$update->addHandle($this->sanitizeHandle($handle));
2739
}
2840

29-
return $resultPage->getLayout();
41+
$builder = $this->layoutBuilderFactory->create(
42+
BuilderFactory::TYPE_PAGE,
43+
[
44+
'layout' => $layout,
45+
'pageConfig' => $this->pageConfig,
46+
'pageLayoutReader' => $this->pageLayoutReader,
47+
]
48+
);
49+
50+
$builder->build();
51+
52+
return $layout;
53+
}
54+
55+
private function sanitizeHandle(string $handle): string
56+
{
57+
return preg_replace('/([^a-z0-9\-\_]+)/', '', $handle);
3058
}
3159
}

0 commit comments

Comments
 (0)