123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Ui\Model;
- use ArrayObject;
- use Magento\Framework\Data\Argument\InterpreterInterface;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\Config\CacheInterface;
- use Magento\Framework\View\Element\UiComponent\ArrayObjectFactory;
- use Magento\Framework\View\Element\UiComponent\Config\Converter;
- use Magento\Framework\View\Element\UiComponent\Config\DomMergerInterface;
- use Magento\Framework\View\Element\UiComponent\Config\FileCollector\AggregatedFileCollectorFactory;
- use Magento\Framework\View\Element\UiComponent\Config\ManagerInterface;
- use Magento\Framework\View\Element\UiComponent\Config\Provider\Component\Definition as ComponentDefinition;
- use Magento\Framework\View\Element\UiComponent\Config\ReaderFactory;
- use Magento\Framework\View\Element\UiComponent\Config\UiReaderInterface;
- use Magento\Framework\Serialize\SerializerInterface;
- use Magento\Framework\App\ObjectManager;
- /**
- * Class Manager
- * @deprecated 101.0.0
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Manager implements ManagerInterface
- {
- /**
- * ID in the storage cache
- */
- const CACHE_ID = 'ui_component_configuration_data';
- /**
- * Configuration provider for UI component
- *
- * @var ComponentDefinition
- */
- protected $componentConfigProvider;
- /**
- * Argument interpreter
- *
- * @var InterpreterInterface
- */
- protected $argumentInterpreter;
- /**
- * DOM document merger
- *
- * @var DomMergerInterface
- */
- protected $domMerger;
- /**
- * Factory for UI config reader
- *
- * @var ReaderFactory
- */
- protected $readerFactory;
- /**
- * Component data
- *
- * @var ArrayObject
- */
- protected $componentsData;
- /**
- * Components pool
- *
- * @var ArrayObject
- */
- protected $componentsPool;
- /**
- * Factory for ArrayObject
- *
- * @var ArrayObjectFactory
- */
- protected $arrayObjectFactory;
- /**
- * @var AggregatedFileCollectorFactory
- */
- protected $aggregatedFileCollectorFactory;
- /**
- * @var CacheInterface
- */
- protected $cache;
- /**
- * @var UiReaderInterface[]
- */
- protected $uiReader;
- /**
- * @var SerializerInterface
- */
- private $serializer;
- /**
- * @param ComponentDefinition $componentConfigProvider
- * @param DomMergerInterface $domMerger
- * @param ReaderFactory $readerFactory
- * @param ArrayObjectFactory $arrayObjectFactory
- * @param AggregatedFileCollectorFactory $aggregatedFileCollectorFactory
- * @param CacheInterface $cache
- * @param InterpreterInterface $argumentInterpreter
- * @param SerializerInterface|null $serializer
- */
- public function __construct(
- ComponentDefinition $componentConfigProvider,
- DomMergerInterface $domMerger,
- ReaderFactory $readerFactory,
- ArrayObjectFactory $arrayObjectFactory,
- AggregatedFileCollectorFactory $aggregatedFileCollectorFactory,
- CacheInterface $cache,
- InterpreterInterface $argumentInterpreter,
- SerializerInterface $serializer = null
- ) {
- $this->componentConfigProvider = $componentConfigProvider;
- $this->domMerger = $domMerger;
- $this->readerFactory = $readerFactory;
- $this->arrayObjectFactory = $arrayObjectFactory;
- $this->componentsData = $this->arrayObjectFactory->create();
- $this->aggregatedFileCollectorFactory = $aggregatedFileCollectorFactory;
- $this->cache = $cache;
- $this->argumentInterpreter = $argumentInterpreter;
- $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
- }
- /**
- * Get component data
- *
- * @param string $name
- * @return array
- */
- public function getData($name)
- {
- return (array) $this->componentsData->offsetGet($name);
- }
- /**
- * Has component data
- *
- * @param string $name
- * @return bool
- */
- protected function hasData($name)
- {
- return $this->componentsData->offsetExists($name);
- }
- /**
- * Prepare the initialization data of UI components
- *
- * @param string $name
- * @return ManagerInterface
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function prepareData($name)
- {
- if ($name === null || $this->hasData($name)) {
- throw new LocalizedException(
- new \Magento\Framework\Phrase(
- 'The "%1" UI component element name is invalid. Verify the name and try again.',
- [$name]
- )
- );
- }
- $this->componentsPool = $this->arrayObjectFactory->create();
- $cacheID = static::CACHE_ID . '_' . $name;
- $cachedPool = $this->cache->load($cacheID);
- if ($cachedPool === false) {
- $this->prepare($name);
- $this->cache->save(
- $this->serializer->serialize($this->componentsPool->getArrayCopy()),
- $cacheID
- );
- } else {
- $this->componentsPool->exchangeArray(
- $this->serializer->unserialize($cachedPool)
- );
- }
- $this->componentsData->offsetSet($name, $this->componentsPool);
- $this->componentsData->offsetSet($name, $this->evaluateComponentArguments($this->getData($name)));
- return $this;
- }
- /**
- * Evaluated components data
- *
- * @param array $components
- * @return array
- */
- protected function evaluateComponentArguments($components)
- {
- foreach ($components as &$component) {
- foreach ($component[ManagerInterface::COMPONENT_ARGUMENTS_KEY] as $argumentName => $argument) {
- $component[ManagerInterface::COMPONENT_ARGUMENTS_KEY][$argumentName]
- = $this->argumentInterpreter->evaluate($argument);
- }
- $component[ManagerInterface::CHILDREN_KEY] = $this->evaluateComponentArguments(
- $component[ManagerInterface::CHILDREN_KEY]
- );
- }
- return $components;
- }
- /**
- * To create the raw data components
- *
- * @param string $component
- * @param bool $evaluated
- * @return array
- */
- public function createRawComponentData($component, $evaluated = true)
- {
- $componentData = $this->componentConfigProvider->getComponentData($component);
- $componentData[Converter::DATA_ATTRIBUTES_KEY] = isset($componentData[Converter::DATA_ATTRIBUTES_KEY])
- ? $componentData[Converter::DATA_ATTRIBUTES_KEY]
- : [];
- $componentData[Converter::DATA_ARGUMENTS_KEY] = isset($componentData[Converter::DATA_ARGUMENTS_KEY])
- ? $componentData[Converter::DATA_ARGUMENTS_KEY]
- : [];
- if ($evaluated) {
- foreach ($componentData[Converter::DATA_ARGUMENTS_KEY] as $argumentName => $argument) {
- $componentData[Converter::DATA_ARGUMENTS_KEY][$argumentName]
- = $this->argumentInterpreter->evaluate($argument);
- }
- }
- return [
- ManagerInterface::COMPONENT_ATTRIBUTES_KEY => $componentData[Converter::DATA_ATTRIBUTES_KEY],
- ManagerInterface::COMPONENT_ARGUMENTS_KEY => $componentData[Converter::DATA_ARGUMENTS_KEY],
- ];
- }
- /**
- * Get UIReader and collect base files configuration
- *
- * @param string $name
- * @return UiReaderInterface
- */
- public function getReader($name)
- {
- if (!isset($this->uiReader[$name])) {
- $this->domMerger->unsetDom();
- $this->uiReader[$name] = $this->readerFactory->create(
- [
- 'fileCollector' => $this->aggregatedFileCollectorFactory->create(
- ['searchPattern' => sprintf(ManagerInterface::SEARCH_PATTERN, $name)]
- ),
- 'domMerger' => $this->domMerger
- ]
- );
- }
- return $this->uiReader[$name];
- }
- /**
- * Initialize the new component data
- *
- * @param string $name
- * @return void
- */
- protected function prepare($name)
- {
- $componentData = $this->getReader($name)->read();
- $componentsPool = reset($componentData);
- $componentsPool = reset($componentsPool);
- $componentsPool[Converter::DATA_ATTRIBUTES_KEY] = array_merge(
- ['name' => $name],
- $componentsPool[Converter::DATA_ATTRIBUTES_KEY]
- );
- $components = $this->createDataForComponent(key($componentData), [$componentsPool]);
- $this->addComponentIntoPool($name, reset($components));
- }
- /**
- * Create data for component instance
- *
- * @param string $name
- * @param array $componentsPool
- * @return array
- */
- protected function createDataForComponent($name, array $componentsPool)
- {
- $createdComponents = [];
- $rootComponent = $this->createRawComponentData($name, false);
- foreach ($componentsPool as $key => $component) {
- $resultConfiguration = [ManagerInterface::CHILDREN_KEY => []];
- $instanceName = $this->createName($component, $key, $name);
- $resultConfiguration[ManagerInterface::COMPONENT_ARGUMENTS_KEY] = $this->mergeArguments(
- $component,
- $rootComponent
- );
- unset($component[Converter::DATA_ARGUMENTS_KEY]);
- $resultConfiguration[ManagerInterface::COMPONENT_ATTRIBUTES_KEY] = $this->mergeAttributes(
- $component,
- $rootComponent
- );
- unset($component[Converter::DATA_ATTRIBUTES_KEY]);
- // Create inner components
- foreach ($component as $subComponentName => $subComponent) {
- if (is_array($subComponent)) {
- $resultConfiguration[ManagerInterface::CHILDREN_KEY] = array_merge(
- $resultConfiguration[ManagerInterface::CHILDREN_KEY],
- $this->createDataForComponent($subComponentName, $subComponent)
- );
- }
- }
- $createdComponents[$instanceName] = $resultConfiguration;
- }
- return $createdComponents;
- }
- /**
- * Add a component into pool
- *
- * @param string $instanceName
- * @param array $configuration
- * @return void
- */
- protected function addComponentIntoPool($instanceName, array $configuration)
- {
- $this->componentsPool->offsetSet($instanceName, $configuration);
- }
- /**
- * Merge component arguments
- *
- * @param array $componentData
- * @param array $rootComponentData
- * @return array
- */
- protected function mergeArguments(array $componentData, array $rootComponentData)
- {
- $baseArguments = isset($rootComponentData[ManagerInterface::COMPONENT_ARGUMENTS_KEY])
- ? $rootComponentData[ManagerInterface::COMPONENT_ARGUMENTS_KEY]
- : [];
- $componentArguments = isset($componentData[Converter::DATA_ARGUMENTS_KEY])
- ? $componentData[Converter::DATA_ARGUMENTS_KEY]
- : [];
- return array_replace_recursive($baseArguments, $componentArguments);
- }
- /**
- * Merge component attributes
- *
- * @param array $componentData
- * @param array $rootComponentData
- * @return array
- */
- protected function mergeAttributes(array $componentData, array $rootComponentData)
- {
- $baseAttributes = isset($rootComponentData[ManagerInterface::COMPONENT_ATTRIBUTES_KEY])
- ? $rootComponentData[ManagerInterface::COMPONENT_ATTRIBUTES_KEY]
- : [];
- $componentAttributes = isset($componentData[Converter::DATA_ATTRIBUTES_KEY])
- ? $componentData[Converter::DATA_ATTRIBUTES_KEY]
- : [];
- unset($componentAttributes['noNamespaceSchemaLocation']);
- return array_replace_recursive($baseAttributes, $componentAttributes);
- }
- /**
- * Create name component instance
- *
- * @param array $componentData
- * @param string|int $key
- * @param string $componentName
- * @return string
- */
- protected function createName(array $componentData, $key, $componentName)
- {
- return isset($componentData[Converter::DATA_ATTRIBUTES_KEY][Converter::NAME_ATTRIBUTE_KEY])
- ? $componentData[Converter::DATA_ATTRIBUTES_KEY][Converter::NAME_ATTRIBUTE_KEY]
- : sprintf(ManagerInterface::ANONYMOUS_TEMPLATE, $componentName, $key);
- }
- }
|