Data.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Config;
  7. use Magento\Framework\Serialize\SerializerInterface;
  8. use Magento\Framework\App\ObjectManager;
  9. /**
  10. * Represents loaded and cached configuration data, should be used to gain access to different types
  11. *
  12. * @SuppressWarnings(PHPMD.NumberOfChildren)
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Data implements \Magento\Framework\Config\DataInterface
  17. {
  18. /**
  19. * Configuration reader
  20. *
  21. * @var ReaderInterface
  22. */
  23. protected $_reader;
  24. /**
  25. * Configuration cache
  26. *
  27. * @var CacheInterface
  28. */
  29. protected $_cache;
  30. /**
  31. * Cache tag
  32. *
  33. * @var string
  34. */
  35. protected $_cacheId;
  36. /**
  37. * Cache tags
  38. *
  39. * @var array
  40. */
  41. protected $cacheTags = [];
  42. /**
  43. * Config data
  44. *
  45. * @var array
  46. */
  47. protected $_data = [];
  48. /**
  49. * @var ReaderInterface
  50. */
  51. private $reader;
  52. /**
  53. * @var CacheInterface
  54. */
  55. private $cache;
  56. /**
  57. * @var string
  58. */
  59. private $cacheId;
  60. /**
  61. * @var SerializerInterface
  62. */
  63. private $serializer;
  64. /**
  65. * Constructor
  66. *
  67. * @param ReaderInterface $reader
  68. * @param CacheInterface $cache
  69. * @param string $cacheId
  70. * @param SerializerInterface|null $serializer
  71. */
  72. public function __construct(
  73. ReaderInterface $reader,
  74. CacheInterface $cache,
  75. $cacheId,
  76. SerializerInterface $serializer = null
  77. ) {
  78. $this->reader = $reader;
  79. $this->cache = $cache;
  80. $this->cacheId = $cacheId;
  81. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
  82. $this->initData();
  83. }
  84. /**
  85. * Initialise data for configuration
  86. *
  87. * @return void
  88. */
  89. protected function initData()
  90. {
  91. $data = $this->cache->load($this->cacheId);
  92. if (false === $data) {
  93. $data = $this->reader->read();
  94. $this->cache->save($this->serializer->serialize($data), $this->cacheId, $this->cacheTags);
  95. } else {
  96. $data = $this->serializer->unserialize($data);
  97. }
  98. $this->merge($data);
  99. }
  100. /**
  101. * Merge config data to the object
  102. *
  103. * @param array $config
  104. * @return void
  105. */
  106. public function merge(array $config)
  107. {
  108. $this->_data = array_replace_recursive($this->_data, $config);
  109. }
  110. /**
  111. * Get config value by key
  112. *
  113. * @param string $path
  114. * @param mixed $default
  115. * @return array|mixed|null
  116. */
  117. public function get($path = null, $default = null)
  118. {
  119. if ($path === null) {
  120. return $this->_data;
  121. }
  122. $keys = explode('/', $path);
  123. $data = $this->_data;
  124. foreach ($keys as $key) {
  125. if (is_array($data) && array_key_exists($key, $data)) {
  126. $data = $data[$key];
  127. } else {
  128. return $default;
  129. }
  130. }
  131. return $data;
  132. }
  133. /**
  134. * Clear cache data
  135. *
  136. * @return void
  137. */
  138. public function reset()
  139. {
  140. $this->cache->remove($this->cacheId);
  141. }
  142. }