UiComponent.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Layout\Reader;
  7. use Magento\Framework\View\Layout\ScheduledStructure\Helper;
  8. use Magento\Framework\View\Layout\ReaderInterface;
  9. use Magento\Framework\View\Layout\Element;
  10. use Magento\Framework\View\Layout\Reader\Visibility\Condition;
  11. use Magento\Framework\View\Layout\ReaderPool;
  12. use Magento\Framework\Config\DataInterfaceFactory;
  13. /**
  14. * Class UiComponent
  15. */
  16. class UiComponent implements ReaderInterface
  17. {
  18. /**
  19. * Supported types.
  20. */
  21. const TYPE_UI_COMPONENT = 'uiComponent';
  22. /**
  23. * List of supported attributes
  24. *
  25. * @var array
  26. */
  27. protected $attributes = ['group', 'component', 'aclResource'];
  28. /**
  29. * @var Helper
  30. */
  31. protected $layoutHelper;
  32. /**
  33. * @var Condition
  34. */
  35. private $conditionReader;
  36. /**
  37. * @var DataInterfaceFactory
  38. */
  39. private $uiConfigFactory;
  40. /**
  41. * @var ReaderPool
  42. */
  43. private $readerPool;
  44. /**
  45. * Constructor
  46. *
  47. * @param Helper $helper
  48. * @param Condition $conditionReader
  49. * @param DataInterfaceFactory $uiConfigFactory
  50. * @param ReaderPool $readerPool
  51. */
  52. public function __construct(
  53. Helper $helper,
  54. Condition $conditionReader,
  55. DataInterfaceFactory $uiConfigFactory,
  56. ReaderPool $readerPool
  57. ) {
  58. $this->layoutHelper = $helper;
  59. $this->conditionReader = $conditionReader;
  60. $this->uiConfigFactory = $uiConfigFactory;
  61. $this->readerPool = $readerPool;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getSupportedNodes()
  67. {
  68. return [self::TYPE_UI_COMPONENT];
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function interpret(Context $readerContext, Element $currentElement)
  74. {
  75. $attributes = $this->getAttributes($currentElement);
  76. $scheduledStructure = $readerContext->getScheduledStructure();
  77. $referenceName = $this->layoutHelper->scheduleStructure(
  78. $scheduledStructure,
  79. $currentElement,
  80. $currentElement->getParent(),
  81. ['attributes' => $attributes]
  82. );
  83. $attributes = array_merge(
  84. $attributes,
  85. ['visibilityConditions' => $this->conditionReader->parseConditions($currentElement)]
  86. );
  87. $scheduledStructure->setStructureElementData($referenceName, ['attributes' => $attributes]);
  88. $elements = [];
  89. $config = $this->uiConfigFactory->create(['componentName' => $referenceName])->get($referenceName);
  90. $this->getLayoutElementsFromUiConfiguration([$referenceName => $config], $elements);
  91. foreach ($elements as $layoutElement) {
  92. $layoutElement = simplexml_load_string(
  93. $layoutElement,
  94. Element::class
  95. );
  96. $this->readerPool->interpret($readerContext, $layoutElement);
  97. }
  98. return $this;
  99. }
  100. /**
  101. * Find layout elements in UI configuration for correct layout generation
  102. *
  103. * @param array $config
  104. * @param array $elements
  105. * @return void
  106. */
  107. private function getLayoutElementsFromUiConfiguration(array $config, array &$elements = [])
  108. {
  109. foreach ($config as $data) {
  110. if (isset($data['arguments']['block']['layout'])) {
  111. $elements[] = $data['arguments']['block']['layout'];
  112. }
  113. if (isset($data['children']) && !empty($data['children'])) {
  114. $this->getLayoutElementsFromUiConfiguration($data['children'], $elements);
  115. }
  116. }
  117. }
  118. /**
  119. * Get ui component attributes
  120. *
  121. * @param Element $element
  122. * @return array
  123. */
  124. protected function getAttributes(Element $element)
  125. {
  126. $attributes = [];
  127. foreach ($this->attributes as $attributeName) {
  128. $attributes[$attributeName] = (string)$element->getAttribute($attributeName);
  129. }
  130. return $attributes;
  131. }
  132. }