Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Manager.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Ui\Model;
8 
9 use ArrayObject;
23 
29 class Manager implements ManagerInterface
30 {
34  const CACHE_ID = 'ui_component_configuration_data';
35 
42 
49 
55  protected $domMerger;
56 
62  protected $readerFactory;
63 
69  protected $componentsData;
70 
76  protected $componentsPool;
77 
84 
89 
93  protected $cache;
94 
98  protected $uiReader;
99 
103  private $serializer;
104 
115  public function __construct(
116  ComponentDefinition $componentConfigProvider,
123  SerializerInterface $serializer = null
124  ) {
125  $this->componentConfigProvider = $componentConfigProvider;
126  $this->domMerger = $domMerger;
127  $this->readerFactory = $readerFactory;
128  $this->arrayObjectFactory = $arrayObjectFactory;
129  $this->componentsData = $this->arrayObjectFactory->create();
130  $this->aggregatedFileCollectorFactory = $aggregatedFileCollectorFactory;
131  $this->cache = $cache;
132  $this->argumentInterpreter = $argumentInterpreter;
133  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
134  }
135 
142  public function getData($name)
143  {
144  return (array) $this->componentsData->offsetGet($name);
145  }
146 
153  protected function hasData($name)
154  {
155  return $this->componentsData->offsetExists($name);
156  }
157 
165  public function prepareData($name)
166  {
167  if ($name === null || $this->hasData($name)) {
168  throw new LocalizedException(
169  new \Magento\Framework\Phrase(
170  'The "%1" UI component element name is invalid. Verify the name and try again.',
171  [$name]
172  )
173  );
174  }
175  $this->componentsPool = $this->arrayObjectFactory->create();
176 
177  $cacheID = static::CACHE_ID . '_' . $name;
178  $cachedPool = $this->cache->load($cacheID);
179  if ($cachedPool === false) {
180  $this->prepare($name);
181  $this->cache->save(
182  $this->serializer->serialize($this->componentsPool->getArrayCopy()),
183  $cacheID
184  );
185  } else {
186  $this->componentsPool->exchangeArray(
187  $this->serializer->unserialize($cachedPool)
188  );
189  }
190  $this->componentsData->offsetSet($name, $this->componentsPool);
191  $this->componentsData->offsetSet($name, $this->evaluateComponentArguments($this->getData($name)));
192 
193  return $this;
194  }
195 
202  protected function evaluateComponentArguments($components)
203  {
204  foreach ($components as &$component) {
205  foreach ($component[ManagerInterface::COMPONENT_ARGUMENTS_KEY] as $argumentName => $argument) {
206  $component[ManagerInterface::COMPONENT_ARGUMENTS_KEY][$argumentName]
207  = $this->argumentInterpreter->evaluate($argument);
208  }
211  );
212  }
213 
214  return $components;
215  }
216 
224  public function createRawComponentData($component, $evaluated = true)
225  {
226  $componentData = $this->componentConfigProvider->getComponentData($component);
227  $componentData[Converter::DATA_ATTRIBUTES_KEY] = isset($componentData[Converter::DATA_ATTRIBUTES_KEY])
228  ? $componentData[Converter::DATA_ATTRIBUTES_KEY]
229  : [];
230  $componentData[Converter::DATA_ARGUMENTS_KEY] = isset($componentData[Converter::DATA_ARGUMENTS_KEY])
231  ? $componentData[Converter::DATA_ARGUMENTS_KEY]
232  : [];
233  if ($evaluated) {
234  foreach ($componentData[Converter::DATA_ARGUMENTS_KEY] as $argumentName => $argument) {
235  $componentData[Converter::DATA_ARGUMENTS_KEY][$argumentName]
236  = $this->argumentInterpreter->evaluate($argument);
237  }
238  }
239 
240  return [
243  ];
244  }
245 
252  public function getReader($name)
253  {
254  if (!isset($this->uiReader[$name])) {
255  $this->domMerger->unsetDom();
256  $this->uiReader[$name] = $this->readerFactory->create(
257  [
258  'fileCollector' => $this->aggregatedFileCollectorFactory->create(
259  ['searchPattern' => sprintf(ManagerInterface::SEARCH_PATTERN, $name)]
260  ),
261  'domMerger' => $this->domMerger
262  ]
263  );
264  }
265 
266  return $this->uiReader[$name];
267  }
268 
275  protected function prepare($name)
276  {
277  $componentData = $this->getReader($name)->read();
278  $componentsPool = reset($componentData);
281  ['name' => $name],
283  );
284  $components = $this->createDataForComponent(key($componentData), [$componentsPool]);
285  $this->addComponentIntoPool($name, reset($components));
286  }
287 
295  protected function createDataForComponent($name, array $componentsPool)
296  {
297  $createdComponents = [];
298  $rootComponent = $this->createRawComponentData($name, false);
299  foreach ($componentsPool as $key => $component) {
300  $resultConfiguration = [ManagerInterface::CHILDREN_KEY => []];
301  $instanceName = $this->createName($component, $key, $name);
302  $resultConfiguration[ManagerInterface::COMPONENT_ARGUMENTS_KEY] = $this->mergeArguments(
303  $component,
304  $rootComponent
305  );
306  unset($component[Converter::DATA_ARGUMENTS_KEY]);
307  $resultConfiguration[ManagerInterface::COMPONENT_ATTRIBUTES_KEY] = $this->mergeAttributes(
308  $component,
309  $rootComponent
310  );
311  unset($component[Converter::DATA_ATTRIBUTES_KEY]);
312 
313  // Create inner components
314  foreach ($component as $subComponentName => $subComponent) {
315  if (is_array($subComponent)) {
316  $resultConfiguration[ManagerInterface::CHILDREN_KEY] = array_merge(
317  $resultConfiguration[ManagerInterface::CHILDREN_KEY],
318  $this->createDataForComponent($subComponentName, $subComponent)
319  );
320  }
321  }
322  $createdComponents[$instanceName] = $resultConfiguration;
323  }
324 
325  return $createdComponents;
326  }
327 
335  protected function addComponentIntoPool($instanceName, array $configuration)
336  {
337  $this->componentsPool->offsetSet($instanceName, $configuration);
338  }
339 
347  protected function mergeArguments(array $componentData, array $rootComponentData)
348  {
349  $baseArguments = isset($rootComponentData[ManagerInterface::COMPONENT_ARGUMENTS_KEY])
350  ? $rootComponentData[ManagerInterface::COMPONENT_ARGUMENTS_KEY]
351  : [];
352  $componentArguments = isset($componentData[Converter::DATA_ARGUMENTS_KEY])
353  ? $componentData[Converter::DATA_ARGUMENTS_KEY]
354  : [];
355 
356  return array_replace_recursive($baseArguments, $componentArguments);
357  }
358 
366  protected function mergeAttributes(array $componentData, array $rootComponentData)
367  {
368  $baseAttributes = isset($rootComponentData[ManagerInterface::COMPONENT_ATTRIBUTES_KEY])
369  ? $rootComponentData[ManagerInterface::COMPONENT_ATTRIBUTES_KEY]
370  : [];
371  $componentAttributes = isset($componentData[Converter::DATA_ATTRIBUTES_KEY])
372  ? $componentData[Converter::DATA_ATTRIBUTES_KEY]
373  : [];
374  unset($componentAttributes['noNamespaceSchemaLocation']);
375 
376  return array_replace_recursive($baseAttributes, $componentAttributes);
377  }
378 
387  protected function createName(array $componentData, $key, $componentName)
388  {
391  : sprintf(ManagerInterface::ANONYMOUS_TEMPLATE, $componentName, $key);
392  }
393 }
$configuration
Definition: index.php:33
__construct(ComponentDefinition $componentConfigProvider, DomMergerInterface $domMerger, ReaderFactory $readerFactory, ArrayObjectFactory $arrayObjectFactory, AggregatedFileCollectorFactory $aggregatedFileCollectorFactory, CacheInterface $cache, InterpreterInterface $argumentInterpreter, SerializerInterface $serializer=null)
Definition: Manager.php:115
createName(array $componentData, $key, $componentName)
Definition: Manager.php:387
createDataForComponent($name, array $componentsPool)
Definition: Manager.php:295
mergeAttributes(array $componentData, array $rootComponentData)
Definition: Manager.php:366
evaluateComponentArguments($components)
Definition: Manager.php:202
mergeArguments(array $componentData, array $rootComponentData)
Definition: Manager.php:347
addComponentIntoPool($instanceName, array $configuration)
Definition: Manager.php:335
createRawComponentData($component, $evaluated=true)
Definition: Manager.php:224
if(!isset($_GET['name'])) $name
Definition: log.php:14