Data.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Config;
  7. use Magento\Framework\Config\CacheInterface;
  8. use Magento\Framework\Serialize\SerializerInterface;
  9. use Magento\Ui\Config\Converter;
  10. use Magento\Framework\Data\Argument\InterpreterInterface;
  11. /**
  12. * UI Component configuration data
  13. */
  14. class Data implements \Magento\Framework\Config\DataInterface
  15. {
  16. /**
  17. * ID in the storage cache
  18. */
  19. const CACHE_ID = 'ui_component_configuration_data';
  20. /**
  21. * Search pattern
  22. */
  23. const SEARCH_PATTERN = '%s.xml';
  24. /**
  25. * Config data
  26. *
  27. * @var array
  28. */
  29. private $data = [];
  30. /**
  31. * @var ReaderFactory
  32. */
  33. private $readerFactory;
  34. /**
  35. * @var CacheInterface
  36. */
  37. private $cache;
  38. /**
  39. * @var string
  40. */
  41. private $cacheId;
  42. /**
  43. * @var SerializerInterface
  44. */
  45. private $serializer;
  46. /**
  47. * @var string
  48. */
  49. private $componentName;
  50. /**
  51. * Argument interpreter.
  52. *
  53. * @var InterpreterInterface
  54. */
  55. private $argumentInterpreter;
  56. /**
  57. * @param string $componentName
  58. * @param ReaderFactory $readerFactory
  59. * @param CacheInterface $cache
  60. * @param SerializerInterface $serializer
  61. * @param InterpreterInterface $argumentInterpreter,
  62. */
  63. public function __construct(
  64. $componentName,
  65. ReaderFactory $readerFactory,
  66. CacheInterface $cache,
  67. SerializerInterface $serializer,
  68. InterpreterInterface $argumentInterpreter
  69. ) {
  70. $this->readerFactory = $readerFactory;
  71. $this->cache = $cache;
  72. $this->serializer = $serializer;
  73. $this->componentName = $componentName;
  74. $this->argumentInterpreter = $argumentInterpreter;
  75. $this->cacheId = static::CACHE_ID . '_' . $componentName;
  76. }
  77. /**
  78. * Initialise data for configuration
  79. *
  80. * @return void
  81. */
  82. private function initData()
  83. {
  84. $data = $this->cache->load($this->cacheId);
  85. if (false === $data) {
  86. /** @var Reader $reader */
  87. $reader = $this->readerFactory->create(
  88. ['fileName' => sprintf(self::SEARCH_PATTERN, $this->componentName)]
  89. );
  90. $data = $reader->read();
  91. $this->cache->save($this->serializer->serialize($data), $this->cacheId);
  92. } else {
  93. $data = $this->serializer->unserialize($data);
  94. }
  95. if (!empty($data)) {
  96. $this->data[$this->componentName] = [Converter::DATA_ATTRIBUTES_KEY => ['name' => $this->componentName]];
  97. $this->merge([$this->componentName => $data]);
  98. $this->data = $this->evaluateComponentArguments($this->data);
  99. }
  100. }
  101. /**
  102. * Merge config data to the object
  103. *
  104. * @param array $config
  105. * @return void
  106. */
  107. public function merge(array $config)
  108. {
  109. $this->data = array_replace_recursive($this->get(), $config);
  110. }
  111. /**
  112. * Get config value by key
  113. *
  114. * @param string $path
  115. * @param mixed $default
  116. * @return mixed
  117. */
  118. public function get($path = null, $default = null)
  119. {
  120. if (empty($this->data)) {
  121. $this->initData();
  122. }
  123. if ($path === null) {
  124. return $this->data;
  125. }
  126. $keys = explode('/', $path);
  127. $data = $this->data;
  128. foreach ($keys as $key) {
  129. if (is_array($data) && array_key_exists($key, $data)) {
  130. $data = $data[$key];
  131. } else {
  132. return $default;
  133. }
  134. }
  135. return $data;
  136. }
  137. /**
  138. * Evaluated components arguments recursively
  139. *
  140. * @param array $components
  141. * @return array
  142. */
  143. private function evaluateComponentArguments($components)
  144. {
  145. foreach ($components as &$component) {
  146. foreach ($component[Converter::DATA_ARGUMENTS_KEY] as $argumentName => $argument) {
  147. $component[Converter::DATA_ARGUMENTS_KEY][$argumentName] =
  148. $this->argumentInterpreter->evaluate($argument);
  149. }
  150. $component[Converter::DATA_COMPONENTS_KEY] = $this->evaluateComponentArguments(
  151. $component[Converter::DATA_COMPONENTS_KEY]
  152. );
  153. }
  154. return $components;
  155. }
  156. }