Generic.php 5.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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\View\Layout;

use Magento\Framework\View\Element\UiComponent\DataSourceInterface;
use Magento\Framework\View\Element\UiComponent\BlockWrapperInterface;
use Magento\Framework\View\Element\UiComponent\LayoutInterface;
use Magento\Framework\View\Element\UiComponentInterface;
use Magento\Framework\View\Element\UiComponentFactory;

/**
 * Class Generic
 */
class Generic implements LayoutInterface
{
    const CONFIG_JS_COMPONENT = 'component';
    const CONFIG_COMPONENT_NAME = 'componentName';
    const CONFIG_PANEL_COMPONENT = 'panelComponentName';

    /**
     * @var UiComponentInterface
     */
    protected $component;

    /**
     * @var string
     */
    protected $namespace;

    /**
     * @var UiComponentFactory
     */
    protected $uiComponentFactory;

    /**
     * @var array
     */
    protected $data;

    /**
     * @param UiComponentFactory $uiComponentFactory
     * @param array $data
     */
    public function __construct(UiComponentFactory $uiComponentFactory, $data = [])
    {
        $this->uiComponentFactory = $uiComponentFactory;
        $this->data = $data;
    }

    /**
     * Generate Java Script configuration element
     *
     * @param UiComponentInterface $component
     * @return array
     */
    public function build(UiComponentInterface $component)
    {
        $this->component = $component;
        $this->namespace = $component->getContext()->getNamespace();

        $this->component->getContext()->addComponentDefinition(
            $this->getConfig(self::CONFIG_COMPONENT_NAME),
            [
                'component' => $this->getConfig(self::CONFIG_JS_COMPONENT),
                'extends' => $this->namespace
            ]
        );

        $children = [];
        $context = $component->getContext();
        $this->addChildren($children, $component, $component->getName());
        $dataSources = $component->getContext()->getDataSourceData($component);
        $configuration = [
            'types' => $context->getComponentsDefinitions(),
            'components' => [
                $context->getNamespace() => [
                    'children' => array_merge($children, $dataSources)
                ]
            ]
        ];
        return $configuration;
    }

    /**
     * Add children data
     *
     * @param array $topNode
     * @param UiComponentInterface $component
     * @param string $componentType
     * @return void
     */
    protected function addChildren(
        array &$topNode,
        UiComponentInterface $component,
        $componentType
    ) {
        $childrenNode = [];
        $childComponents = $component->getChildComponents();
        if (!empty($childComponents)) {
            /** @var UiComponentInterface $child */
            foreach ($childComponents as $child) {
                if ($child instanceof DataSourceInterface) {
                    continue;
                }
                if ($child->getData('wrapper')) {
                    $this->addWrappedBlock($child, $childrenNode);
                    continue;
                }
                self::addChildren($childrenNode, $child, $child->getComponentName());
            }
        }

        $config = $component->getConfiguration();
        if (is_string($config)) {
            $topNode[$config] = $config;
        } else {
            $nodeData = [
                'type' => $componentType,
                'name' => $component->getName(),
            ];
            if (!empty($childrenNode)) {
                $nodeData['children'] = $childrenNode;
            }
            if (isset($config['dataScope'])) {
                $nodeData['dataScope'] = $config['dataScope'];
                unset($config['dataScope']);
            }
            if (!empty($config)) {
                $nodeData['config'] = $config;
            }
            $topNode[$component->getName()] = $nodeData;
        }
    }

    /**
     * Add wrapped layout block
     *
     * @param BlockWrapperInterface $childComponent
     * @param array $childrenNode
     * @return $this
     */
    protected function addWrappedBlock(BlockWrapperInterface $childComponent, array &$childrenNode)
    {
        if (!($childComponent->getData('wrapper/canShow') && $childComponent->getData('wrapper/componentType'))) {
            return $this;
        }

        $name = $childComponent->getName();
        $panelComponent = $this->createChildFormComponent($childComponent, $name);
        $childrenNode[$name] = [
            'type' => $panelComponent->getComponentName(),
            'dataScope' => $name,
            'config' => $childComponent->getConfiguration(),
            'children' => [
                $name => [
                    'type' => $childComponent->getComponentName(),
                    'dataScope' => $name,
                    'config' => [
                        'content' => $childComponent->render()
                    ],
                ]
            ],
        ];

        return $this;
    }

    /**
     * Create child of form
     *
     * @param UiComponentInterface $childComponent
     * @param string $name
     * @return UiComponentInterface
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    protected function createChildFormComponent(UiComponentInterface $childComponent, $name)
    {
        $panelComponent = $this->uiComponentFactory->create(
            $name,
            $childComponent->getData('wrapper/componentType'),
            [
                'context' => $this->component->getContext(),
                'components' => [$childComponent->getName() => $childComponent]
            ]
        );
        $panelComponent->prepare();
        $this->component->addComponent($name, $panelComponent);

        return $panelComponent;
    }

    /**
     * Get config by name
     *
     * @param string $name
     * @return mixed
     */
    protected function getConfig($name)
    {
        return $this->data['config'][$name] ?? null;
    }
}