LayoutFactory.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View;
  7. /**
  8. * Factory class for Layout
  9. */
  10. class LayoutFactory
  11. {
  12. /**
  13. * Object Manager instance
  14. *
  15. * @var \Magento\Framework\ObjectManagerInterface
  16. */
  17. protected $_objectManager;
  18. /**
  19. * Instance name to create
  20. *
  21. * @var string
  22. */
  23. protected $_instanceName;
  24. /**
  25. * Constructor
  26. *
  27. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  28. * @param string $instanceName
  29. */
  30. public function __construct(
  31. \Magento\Framework\ObjectManagerInterface $objectManager,
  32. $instanceName = \Magento\Framework\View\LayoutInterface::class
  33. ) {
  34. $this->_objectManager = $objectManager;
  35. $this->_instanceName = $instanceName;
  36. }
  37. /**
  38. * Create class instance with specified parameters
  39. *
  40. * @param array $data
  41. * @return LayoutInterface
  42. * @throws \InvalidArgumentException
  43. */
  44. public function create(array $data = [])
  45. {
  46. $layout = $this->_objectManager->create($this->_instanceName, $data);
  47. if (!$layout instanceof LayoutInterface) {
  48. throw new \InvalidArgumentException(get_class($layout) . ' must be an instance of LayoutInterface.');
  49. }
  50. return $layout;
  51. }
  52. }