UiComponent.php 3.97 KB
Newer Older
Ketan's avatar
Ketan committed
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\View\Layout\Reader;

use Magento\Framework\View\Layout\ScheduledStructure\Helper;
use Magento\Framework\View\Layout\ReaderInterface;
use Magento\Framework\View\Layout\Element;
use Magento\Framework\View\Layout\Reader\Visibility\Condition;
use Magento\Framework\View\Layout\ReaderPool;
use Magento\Framework\Config\DataInterfaceFactory;

/**
 * Class UiComponent
 */
class UiComponent implements ReaderInterface
{
    /**
     * Supported types.
     */
    const TYPE_UI_COMPONENT = 'uiComponent';

    /**
     * List of supported attributes
     *
     * @var array
     */
    protected $attributes = ['group', 'component', 'aclResource'];

    /**
     * @var Helper
     */
    protected $layoutHelper;

    /**
     * @var Condition
     */
    private $conditionReader;

    /**
     * @var DataInterfaceFactory
     */
    private $uiConfigFactory;

    /**
     * @var ReaderPool
     */
    private $readerPool;

    /**
     * Constructor
     *
     * @param Helper $helper
     * @param Condition $conditionReader
     * @param DataInterfaceFactory $uiConfigFactory
     * @param ReaderPool $readerPool
     */
    public function __construct(
        Helper $helper,
        Condition $conditionReader,
        DataInterfaceFactory $uiConfigFactory,
        ReaderPool $readerPool
    ) {
        $this->layoutHelper = $helper;
        $this->conditionReader = $conditionReader;
        $this->uiConfigFactory = $uiConfigFactory;
        $this->readerPool = $readerPool;
    }

    /**
     * {@inheritdoc}
     */
    public function getSupportedNodes()
    {
        return [self::TYPE_UI_COMPONENT];
    }

    /**
     * {@inheritdoc}
     */
    public function interpret(Context $readerContext, Element $currentElement)
    {
        $attributes = $this->getAttributes($currentElement);
        $scheduledStructure = $readerContext->getScheduledStructure();
        $referenceName = $this->layoutHelper->scheduleStructure(
            $scheduledStructure,
            $currentElement,
            $currentElement->getParent(),
            ['attributes' => $attributes]
        );
        $attributes = array_merge(
            $attributes,
            ['visibilityConditions' => $this->conditionReader->parseConditions($currentElement)]
        );
        $scheduledStructure->setStructureElementData($referenceName, ['attributes' => $attributes]);

        $elements = [];
        $config = $this->uiConfigFactory->create(['componentName' => $referenceName])->get($referenceName);
        $this->getLayoutElementsFromUiConfiguration([$referenceName => $config], $elements);
        foreach ($elements as $layoutElement) {
            $layoutElement = simplexml_load_string(
                $layoutElement,
                Element::class
            );
            $this->readerPool->interpret($readerContext, $layoutElement);
        }

        return $this;
    }

    /**
     * Find layout elements in UI configuration for correct layout generation
     *
     * @param array $config
     * @param array $elements
     * @return void
     */
    private function getLayoutElementsFromUiConfiguration(array $config, array &$elements = [])
    {
        foreach ($config as $data) {
            if (isset($data['arguments']['block']['layout'])) {
                $elements[] = $data['arguments']['block']['layout'];
            }
            if (isset($data['children']) && !empty($data['children'])) {
                $this->getLayoutElementsFromUiConfiguration($data['children'], $elements);
            }
        }
    }

    /**
     * Get ui component attributes
     *
     * @param Element $element
     * @return array
     */
    protected function getAttributes(Element $element)
    {
        $attributes = [];
        foreach ($this->attributes as $attributeName) {
            $attributes[$attributeName] = (string)$element->getAttribute($attributeName);
        }

        return $attributes;
    }
}