Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Cache.php
Go to the documentation of this file.
1 <?php
7 
15 class Cache
16 {
22  protected static $_instance;
23 
29  protected $_idx = 0;
30 
36  protected $_objects = [];
37 
43  protected $_hashes = [];
44 
50  protected $_objectHashes = [];
51 
57  protected $_tags = [];
58 
64  protected $_objectTags = [];
65 
71  protected $_references = [];
72 
78  protected $_objectReferences = [];
79 
85  protected $_debug = [];
86 
92  public static function singleton()
93  {
94  if (!self::$_instance) {
95  self::$_instance = new self();
96  }
97  return self::$_instance;
98  }
99 
107  public function load($idx, $default = null)
108  {
109  if (isset($this->_references[$idx])) {
110  $idx = $this->_references[$idx];
111  }
112  if (isset($this->_objects[$idx])) {
113  return $this->_objects[$idx];
114  }
115  return $default;
116  }
117 
129  public function save($object, $idx = null, $tags = null)
130  {
131  //\Magento\Framework\Profiler::start('OBJECT_SAVE');
132  if (!is_object($object)) {
133  return false;
134  }
135 
136  $hash = spl_object_hash($object);
137  if ($idx !== null && strpos($idx, '{') !== false) {
138  $idx = str_replace('{hash}', $hash, $idx);
139  }
140  if (isset($this->_hashes[$hash])) {
141  //throw new \Exception('test');
142  if ($idx !== null) {
143  $this->_references[$idx] = $this->_hashes[$hash];
144  }
145  return $this->_hashes[$hash];
146  }
147 
148  if ($idx === null) {
149  $idx = '#' . ++$this->_idx;
150  }
151 
152  if (isset($this->_objects[$idx])) {
153  throw new \Magento\Framework\Exception\LocalizedException(
154  new \Magento\Framework\Phrase(
155  'Object already exists in registry (%1). Old object class: %2, new object class: %3',
156  [$idx, get_class($this->_objects[$idx]), get_class($object)]
157  )
158  );
159  }
160 
161  $this->_objects[$idx] = $object;
162 
163  $this->_hashes[$hash] = $idx;
164  $this->_objectHashes[$idx] = $hash;
165 
166  if (is_string($tags)) {
167  $this->_tags[$tags][$idx] = true;
168  $this->_objectTags[$idx][$tags] = true;
169  } elseif (is_array($tags)) {
170  foreach ($tags as $t) {
171  $this->_tags[$t][$idx] = true;
172  $this->_objectTags[$idx][$t] = true;
173  }
174  }
175  //\Magento\Framework\Profiler::stop('OBJECT_SAVE');
176 
177  return $idx;
178  }
179 
188  public function reference($refName, $idx)
189  {
190  if (is_array($refName)) {
191  foreach ($refName as $ref) {
192  $this->reference($ref, $idx);
193  }
194  return;
195  }
196 
197  if (isset($this->_references[$refName])) {
198  throw new \Magento\Framework\Exception\LocalizedException(
199  new \Magento\Framework\Phrase(
200  'The reference already exists: %1. New index: %2, old index: %3',
201  [$refName, $idx, $this->_references[$refName]]
202  )
203  );
204  }
205  $this->_references[$refName] = $idx;
206  $this->_objectReferences[$idx][$refName] = true;
207 
208  return true;
209  }
210 
217  public function delete($idx)
218  {
219  //\Magento\Framework\Profiler::start("OBJECT_DELETE");
220  if (is_object($idx)) {
221  $idx = $this->find($idx);
222  if (false === $idx) {
223  //\Magento\Framework\Profiler::stop("OBJECT_DELETE");
224  return false;
225  }
226  unset($this->_objects[$idx]);
227  //\Magento\Framework\Profiler::stop("OBJECT_DELETE");
228  return false;
229  } elseif (!isset($this->_objects[$idx])) {
230  //\Magento\Framework\Profiler::stop("OBJECT_DELETE");
231  return false;
232  }
233 
234  unset($this->_objects[$idx]);
235 
236  unset($this->_hashes[$this->_objectHashes[$idx]], $this->_objectHashes[$idx]);
237 
238  if (isset($this->_objectTags[$idx])) {
239  foreach ($this->_objectTags[$idx] as $t => $dummy) {
240  unset($this->_tags[$t][$idx]);
241  }
242  unset($this->_objectTags[$idx]);
243  }
244 
245  if (isset($this->_objectReferences[$idx])) {
246  foreach ($this->_references as $r => $dummy) {
247  unset($this->_references[$r]);
248  }
249  unset($this->_objectReferences[$idx]);
250  }
251  //\Magento\Framework\Profiler::stop("OBJECT_DELETE");
252 
253  return true;
254  }
255 
262  public function deleteByClass($class)
263  {
264  foreach ($this->_objects as $idx => $object) {
265  if ($object instanceof $class) {
266  $this->delete($idx);
267  }
268  }
269  }
270 
278  public function deleteByTags($tags)
279  {
280  if (is_string($tags)) {
281  $tags = [$tags];
282  }
283  foreach ($tags as $t) {
284  foreach ($this->_tags[$t] as $idx => $dummy) {
285  $this->delete($idx);
286  }
287  }
288  return true;
289  }
290 
297  public function has($idx)
298  {
299  return isset($this->_objects[$idx]) || isset($this->_references[$idx]);
300  }
301 
308  public function find($object)
309  {
310  foreach ($this->_objects as $idx => $obj) {
311  if ($object === $obj) {
312  return $idx;
313  }
314  }
315  return false;
316  }
317 
324  public function findByIds($ids)
325  {
326  $objects = [];
327  foreach ($this->_objects as $idx => $obj) {
328  if (in_array($idx, $ids)) {
329  $objects[$idx] = $obj;
330  }
331  }
332  return $objects;
333  }
334 
341  public function findByHash($hash)
342  {
343  return isset($this->_hashes[$hash]) ? $this->_objects[$this->_hashes[$hash]] : null;
344  }
345 
353  public function findByTags($tags)
354  {
355  if (is_string($tags)) {
356  $tags = [$tags];
357  }
358  $objects = [];
359  foreach ($tags as $t) {
360  foreach ($this->_tags[$t] as $idx => $dummy) {
361  if (isset($objects[$idx])) {
362  continue;
363  }
364  $objects[$idx] = $this->load($idx);
365  }
366  }
367  return $objects;
368  }
369 
376  public function findByClass($class)
377  {
378  $objects = [];
379  foreach ($this->_objects as $idx => $object) {
380  if ($object instanceof $class) {
381  $objects[$idx] = $object;
382  }
383  }
384  return $objects;
385  }
386 
395  public function debug($idx, $object = null)
396  {
397  $bt = debug_backtrace();
398  $debug = [];
399  foreach ($bt as $i => $step) {
400  $debug[$i] = [
401  'file' => isset($step['file']) ? $step['file'] : null,
402  'line' => isset($step['line']) ? $step['line'] : null,
403  'function' => isset($step['function']) ? $step['function'] : null,
404  ];
405  }
406  $this->_debug[$idx] = $debug;
407  }
408 
415  public function debugByIds($ids)
416  {
417  if (is_string($ids)) {
418  $ids = [$ids];
419  }
420  $debug = [];
421  foreach ($ids as $idx) {
422  $debug[$idx] = $this->_debug[$idx];
423  }
424  return $debug;
425  }
426 
432  public function getAllObjects()
433  {
434  return $this->_objects;
435  }
436 
442  public function getAllTags()
443  {
444  return $this->_tags;
445  }
446 
452  public function getAllTagsByObject()
453  {
454  return $this->_objectTags;
455  }
456 
462  public function getAllReferences()
463  {
464  return $this->_references;
465  }
466 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
load($idx, $default=null)
Definition: Cache.php:107
save($object, $idx=null, $tags=null)
Definition: Cache.php:129
debug($idx, $object=null)
Definition: Cache.php:395
$_option $_optionId $class
Definition: date.phtml:13
$debug
Definition: router.php:22
$i
Definition: gallery.phtml:31