|
3 | 3 | namespace Loki\Components\Util\Controller; |
4 | 4 |
|
5 | 5 | 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; |
6 | 9 | 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; |
8 | 12 |
|
9 | 13 | class LayoutLoader |
10 | 14 | { |
11 | 15 | 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, |
14 | 23 | ) { |
15 | 24 | } |
16 | 25 |
|
17 | | - public function load(array $handles = []): LayoutInterface |
| 26 | + public function load(array $handles = [], bool $initializeElements = true): LayoutInterface |
18 | 27 | { |
19 | 28 | $handles = $this->layoutHandlerComposite->getHandles($handles); |
20 | | - $resultPage = $this->resultPageFactory->create(true); |
21 | 29 |
|
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)); |
27 | 39 | } |
28 | 40 |
|
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); |
30 | 58 | } |
31 | 59 | } |
0 commit comments