Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Data.php
Go to the documentation of this file.
1 <?php
7 
13 use Magento\Ui\Config\Reader\DefinitionFactory;
14 
19 {
23  const CACHE_ID = 'ui_component_configuration_definition_data';
24 
28  const SEARCH_PATTERN = '%s.xml';
29 
35  private $data = [];
36 
40  private $readerFactory;
41 
45  private $cache;
46 
50  private $cacheId;
51 
55  private $serializer;
56 
62  private $argumentInterpreter;
63 
70  public function __construct(
71  DefinitionFactory $readerFactory,
72  CacheInterface $cache,
73  SerializerInterface $serializer,
74  InterpreterInterface $argumentInterpreter
75  ) {
76  $this->readerFactory = $readerFactory;
77  $this->cache = $cache;
78  $this->serializer = $serializer;
79  $this->argumentInterpreter = $argumentInterpreter;
80  $this->cacheId = static::CACHE_ID;
81  $this->initData();
82  }
83 
89  private function initData()
90  {
91  $data = $this->cache->load($this->cacheId);
92  if (false === $data) {
94  $reader = $this->readerFactory->create();
95  $data = $reader->read();
96  $this->cache->save($this->serializer->serialize($data), $this->cacheId);
97  } else {
98  $data = $this->serializer->unserialize($data);
99  }
100 
101  if (!empty($data)) {
102  $this->data = $this->evaluateComponentArguments($data);
103  }
104  }
105 
112  public function merge(array $config)
113  {
114  $this->data = array_replace_recursive($this->data, $config);
115  }
116 
124  public function get($key, $default = null)
125  {
126  return isset($this->data[$key]) ? $this->data[$key] : $default;
127  }
128 
135  private function evaluateComponentArguments($components)
136  {
137  foreach ($components as &$component) {
138  $component[Converter::DATA_ATTRIBUTES_KEY] = isset($component[Converter::DATA_ATTRIBUTES_KEY])
139  ? $component[Converter::DATA_ATTRIBUTES_KEY]
140  : [];
141  $component[Converter::DATA_ARGUMENTS_KEY] = isset($component[Converter::DATA_ARGUMENTS_KEY])
142  ? $component[Converter::DATA_ARGUMENTS_KEY]
143  : [];
144 
145  foreach ($component[Converter::DATA_ARGUMENTS_KEY] as $argumentName => $argument) {
146  $component[Converter::DATA_ARGUMENTS_KEY][$argumentName] =
147  $this->argumentInterpreter->evaluate($argument);
148  }
149  }
150 
151  return $components;
152  }
153 }
$config
Definition: fraud_order.php:17
__construct(DefinitionFactory $readerFactory, CacheInterface $cache, SerializerInterface $serializer, InterpreterInterface $argumentInterpreter)
Definition: Data.php:70