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
7 
10 
19 {
25  protected $_reader;
26 
32  protected $_cache;
33 
39  protected $_cacheId;
40 
46  protected $cacheTags = [];
47 
53  protected $_data = [];
54 
58  private $reader;
59 
63  private $cache;
64 
68  private $cacheId;
69 
73  private $serializer;
74 
83  public function __construct(
84  ReaderInterface $reader,
85  CacheInterface $cache,
86  $cacheId,
87  SerializerInterface $serializer = null
88  ) {
89  $this->reader = $reader;
90  $this->cache = $cache;
91  $this->cacheId = $cacheId;
92  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
93  $this->initData();
94  }
95 
101  protected function initData()
102  {
103  $data = $this->cache->load($this->cacheId);
104  if (false === $data) {
105  $data = $this->reader->read();
106  $this->cache->save($this->serializer->serialize($data), $this->cacheId, $this->cacheTags);
107  } else {
108  $data = $this->serializer->unserialize($data);
109  }
110 
111  $this->merge($data);
112  }
113 
120  public function merge(array $config)
121  {
122  $this->_data = array_replace_recursive($this->_data, $config);
123  }
124 
132  public function get($path = null, $default = null)
133  {
134  if ($path === null) {
135  return $this->_data;
136  }
137  $keys = explode('/', $path);
139  foreach ($keys as $key) {
140  if (is_array($data) && array_key_exists($key, $data)) {
141  $data = $data[$key];
142  } else {
143  return $default;
144  }
145  }
146  return $data;
147  }
148 
154  public function reset()
155  {
156  $this->cache->remove($this->cacheId);
157  }
158 }
$config
Definition: fraud_order.php:17
merge(array $config)
Definition: Data.php:120
__construct(ReaderInterface $reader, CacheInterface $cache, $cacheId, SerializerInterface $serializer=null)
Definition: Data.php:83