/
BlockFactory.php
52 lines (48 loc) · 1.22 KB
/
BlockFactory.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
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\Element;
use Magento\Framework\ObjectManagerInterface;
/**
* Class BlockFactory
*/
class BlockFactory
{
/**
* Object manager
*
* @var ObjectManagerInterface
*/
protected $objectManager;
/**
* Constructor
*
* @param ObjectManagerInterface $objectManager
*/
public function __construct(ObjectManagerInterface $objectManager)
{
$this->objectManager = $objectManager;
}
/**
* Create block
*
* @param string $blockName
* @param array $arguments
* @return \Magento\Framework\View\Element\BlockInterface
* @throws \LogicException
*/
public function createBlock($blockName, array $arguments = [])
{
$blockName = ltrim($blockName, '\\');
$block = $this->objectManager->create($blockName, $arguments);
if (!$block instanceof BlockInterface) {
throw new \LogicException($blockName . ' does not implement BlockInterface');
}
if ($block instanceof Template) {
$block->setTemplateContext($block);
}
return $block;
}
}