BlockFactory.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Element;
  7. use Magento\Framework\ObjectManagerInterface;
  8. /**
  9. * Class BlockFactory
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class BlockFactory
  15. {
  16. /**
  17. * Object manager
  18. *
  19. * @var ObjectManagerInterface
  20. */
  21. protected $objectManager;
  22. /**
  23. * Constructor
  24. *
  25. * @param ObjectManagerInterface $objectManager
  26. */
  27. public function __construct(ObjectManagerInterface $objectManager)
  28. {
  29. $this->objectManager = $objectManager;
  30. }
  31. /**
  32. * Create block
  33. *
  34. * @param string $blockName
  35. * @param array $arguments
  36. * @return \Magento\Framework\View\Element\BlockInterface
  37. * @throws \LogicException
  38. */
  39. public function createBlock($blockName, array $arguments = [])
  40. {
  41. $blockName = ltrim($blockName, '\\');
  42. $block = $this->objectManager->create($blockName, $arguments);
  43. if (!$block instanceof BlockInterface) {
  44. throw new \LogicException($blockName . ' does not implement BlockInterface');
  45. }
  46. if ($block instanceof Template) {
  47. $block->setTemplateContext($block);
  48. }
  49. return $block;
  50. }
  51. }