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
6 namespace Magento\Ui\Config;
7 
12 
17 {
21  const CACHE_ID = 'ui_component_configuration_data';
22 
26  const SEARCH_PATTERN = '%s.xml';
27 
33  private $data = [];
34 
38  private $readerFactory;
39 
43  private $cache;
44 
48  private $cacheId;
49 
53  private $serializer;
54 
58  private $componentName;
59 
65  private $argumentInterpreter;
66 
74  public function __construct(
75  $componentName,
76  ReaderFactory $readerFactory,
77  CacheInterface $cache,
78  SerializerInterface $serializer,
79  InterpreterInterface $argumentInterpreter
80  ) {
81  $this->readerFactory = $readerFactory;
82  $this->cache = $cache;
83  $this->serializer = $serializer;
84  $this->componentName = $componentName;
85  $this->argumentInterpreter = $argumentInterpreter;
86  $this->cacheId = static::CACHE_ID . '_' . $componentName;
87  }
88 
94  private function initData()
95  {
96  $data = $this->cache->load($this->cacheId);
97  if (false === $data) {
99  $reader = $this->readerFactory->create(
100  ['fileName' => sprintf(self::SEARCH_PATTERN, $this->componentName)]
101  );
102  $data = $reader->read();
103  $this->cache->save($this->serializer->serialize($data), $this->cacheId);
104  } else {
105  $data = $this->serializer->unserialize($data);
106  }
107 
108  if (!empty($data)) {
109  $this->data[$this->componentName] = [Converter::DATA_ATTRIBUTES_KEY => ['name' => $this->componentName]];
110  $this->merge([$this->componentName => $data]);
111  $this->data = $this->evaluateComponentArguments($this->data);
112  }
113  }
114 
121  public function merge(array $config)
122  {
123  $this->data = array_replace_recursive($this->get(), $config);
124  }
125 
133  public function get($path = null, $default = null)
134  {
135  if (empty($this->data)) {
136  $this->initData();
137  }
138  if ($path === null) {
139  return $this->data;
140  }
141  $keys = explode('/', $path);
142  $data = $this->data;
143  foreach ($keys as $key) {
144  if (is_array($data) && array_key_exists($key, $data)) {
145  $data = $data[$key];
146  } else {
147  return $default;
148  }
149  }
150  return $data;
151  }
152 
159  private function evaluateComponentArguments($components)
160  {
161  foreach ($components as &$component) {
162  foreach ($component[Converter::DATA_ARGUMENTS_KEY] as $argumentName => $argument) {
163  $component[Converter::DATA_ARGUMENTS_KEY][$argumentName] =
164  $this->argumentInterpreter->evaluate($argument);
165  }
166  $component[Converter::DATA_COMPONENTS_KEY] = $this->evaluateComponentArguments(
167  $component[Converter::DATA_COMPONENTS_KEY]
168  );
169  }
170 
171  return $components;
172  }
173 }
__construct( $componentName, ReaderFactory $readerFactory, CacheInterface $cache, SerializerInterface $serializer, InterpreterInterface $argumentInterpreter)
Definition: Data.php:74
$config
Definition: fraud_order.php:17
const SEARCH_PATTERN
Definition: Data.php:26
merge(array $config)
Definition: Data.php:121