Data.php 3.8 KB

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