Pool.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Layout;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\Framework\View\Element\UiComponent\LayoutInterface;
  9. /**
  10. * Class Pool
  11. */
  12. class Pool
  13. {
  14. const DEFAULT_CLASS = \Magento\Framework\View\Layout\Generic::class;
  15. /**
  16. * Layouts pool
  17. *
  18. * @var array
  19. */
  20. protected $types;
  21. /**
  22. * @var ObjectManagerInterface
  23. */
  24. protected $objectManager;
  25. /**
  26. * Constructor
  27. *
  28. * @param ObjectManagerInterface $objectManager
  29. * @param array $types
  30. */
  31. public function __construct(
  32. ObjectManagerInterface $objectManager,
  33. array $types = []
  34. ) {
  35. $this->objectManager = $objectManager;
  36. $this->types = $types;
  37. }
  38. /**
  39. * Get layout by type
  40. *
  41. * @param string $layoutType
  42. * @param array $arguments
  43. * @return LayoutInterface
  44. */
  45. public function create($layoutType, array $arguments = [])
  46. {
  47. if (!isset($this->types[$layoutType])) {
  48. throw new \InvalidArgumentException(sprintf('Unknown layout type "%s"', $layoutType));
  49. }
  50. $defArgs = $this->types[$layoutType];
  51. $class = isset($defArgs['class']) ? $defArgs['class'] : self::DEFAULT_CLASS;
  52. unset($defArgs['class']);
  53. if ($defArgs) {
  54. $arguments = array_merge($defArgs, $arguments);
  55. }
  56. return $this->objectManager->create($class, $arguments);
  57. }
  58. }