123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Config;
- use Magento\Framework\Serialize\SerializerInterface;
- use Magento\Framework\App\ObjectManager;
- /**
- * Represents loaded and cached configuration data, should be used to gain access to different types
- *
- * @SuppressWarnings(PHPMD.NumberOfChildren)
- * @api
- * @since 100.0.2
- */
- class Data implements \Magento\Framework\Config\DataInterface
- {
- /**
- * Configuration reader
- *
- * @var ReaderInterface
- */
- protected $_reader;
- /**
- * Configuration cache
- *
- * @var CacheInterface
- */
- protected $_cache;
- /**
- * Cache tag
- *
- * @var string
- */
- protected $_cacheId;
- /**
- * Cache tags
- *
- * @var array
- */
- protected $cacheTags = [];
- /**
- * Config data
- *
- * @var array
- */
- protected $_data = [];
- /**
- * @var ReaderInterface
- */
- private $reader;
- /**
- * @var CacheInterface
- */
- private $cache;
- /**
- * @var string
- */
- private $cacheId;
- /**
- * @var SerializerInterface
- */
- private $serializer;
- /**
- * Constructor
- *
- * @param ReaderInterface $reader
- * @param CacheInterface $cache
- * @param string $cacheId
- * @param SerializerInterface|null $serializer
- */
- public function __construct(
- ReaderInterface $reader,
- CacheInterface $cache,
- $cacheId,
- SerializerInterface $serializer = null
- ) {
- $this->reader = $reader;
- $this->cache = $cache;
- $this->cacheId = $cacheId;
- $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
- $this->initData();
- }
- /**
- * Initialise data for configuration
- *
- * @return void
- */
- protected function initData()
- {
- $data = $this->cache->load($this->cacheId);
- if (false === $data) {
- $data = $this->reader->read();
- $this->cache->save($this->serializer->serialize($data), $this->cacheId, $this->cacheTags);
- } else {
- $data = $this->serializer->unserialize($data);
- }
- $this->merge($data);
- }
- /**
- * Merge config data to the object
- *
- * @param array $config
- * @return void
- */
- public function merge(array $config)
- {
- $this->_data = array_replace_recursive($this->_data, $config);
- }
- /**
- * Get config value by key
- *
- * @param string $path
- * @param mixed $default
- * @return array|mixed|null
- */
- public function get($path = null, $default = null)
- {
- if ($path === null) {
- return $this->_data;
- }
- $keys = explode('/', $path);
- $data = $this->_data;
- foreach ($keys as $key) {
- if (is_array($data) && array_key_exists($key, $data)) {
- $data = $data[$key];
- } else {
- return $default;
- }
- }
- return $data;
- }
- /**
- * Clear cache data
- *
- * @return void
- */
- public function reset()
- {
- $this->cache->remove($this->cacheId);
- }
- }
|