Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Class.php
Go to the documentation of this file.
1 <?php
26 #require_once 'Zend/Cache/Core.php';
27 
28 
36 {
55  protected $_specificOptions = array(
56  'cached_entity' => null,
57  'cache_by_default' => true,
58  'cached_methods' => array(),
59  'non_cached_methods' => array()
60  );
61 
67  protected $_tags = array();
68 
76  protected $_specificLifetime = false;
77 
83  protected $_cachedEntity = null;
84 
92  protected $_cachedEntityLabel = '';
93 
99  protected $_priority = 8;
100 
107  public function __construct(array $options = array())
108  {
109  foreach ($options as $name => $value) {
110  $this->setOption($name, $value);
111  }
112  if ($this->_specificOptions['cached_entity'] === null) {
113  Zend_Cache::throwException('cached_entity must be set !');
114  }
115  $this->setCachedEntity($this->_specificOptions['cached_entity']);
116  $this->setOption('automatic_serialization', true);
117  }
118 
125  public function setSpecificLifetime($specificLifetime = false)
126  {
127  $this->_specificLifetime = $specificLifetime;
128  }
129 
135  public function setPriority($priority)
136  {
137  $this->_priority = $priority;
138  }
139 
150  public function setOption($name, $value)
151  {
152  if ($name == 'cached_entity') {
153  $this->setCachedEntity($value);
154  } else {
155  parent::setOption($name, $value);
156  }
157  }
158 
167  public function setCachedEntity($cachedEntity)
168  {
169  if (!is_string($cachedEntity) && !is_object($cachedEntity)) {
171  'cached_entity must be an object or a class name'
172  );
173  }
174 
175  $this->_cachedEntity = $cachedEntity;
176  $this->_specificOptions['cached_entity'] = $cachedEntity;
177 
178  if (is_string($this->_cachedEntity)) {
179  $this->_cachedEntityLabel = $this->_cachedEntity;
180  } else {
181  $ro = new ReflectionObject($this->_cachedEntity);
182  $this->_cachedEntityLabel = $ro->getName();
183  }
184  }
185 
192  public function setTagsArray($tags = array())
193  {
194  $this->_tags = $tags;
195  }
196 
205  public function __call($name, $parameters)
206  {
207  $callback = array($this->_cachedEntity, $name);
208 
209  if (!is_callable($callback, false)) {
210  Zend_Cache::throwException('Invalid callback');
211  }
212 
213  $cacheBool1 = $this->_specificOptions['cache_by_default'];
214  $cacheBool2 = in_array($name, $this->_specificOptions['cached_methods']);
215  $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_methods']);
216  $cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
217 
218  if (!$cache) {
219  // We do not have not cache
220  return call_user_func_array($callback, $parameters);
221  }
222 
223  $id = $this->makeId($name, $parameters);
224  if (($rs = $this->load($id)) && (array_key_exists(0, $rs))
225  && (array_key_exists(1, $rs))
226  ) {
227  // A cache is available
228  $output = $rs[0];
229  $return = $rs[1];
230  } else {
231  // A cache is not available (or not valid for this frontend)
232  ob_start();
233  ob_implicit_flush(false);
234 
235  try {
236  $return = call_user_func_array($callback, $parameters);
237  $output = ob_get_clean();
238  $data = array($output, $return);
239 
240  $this->save(
241  $data, $id, $this->_tags, $this->_specificLifetime,
242  $this->_priority
243  );
244  } catch (Exception $e) {
245  ob_end_clean();
246  throw $e;
247  }
248  }
249 
250  echo $output;
251  return $return;
252  }
253 
259  private function _makeId($name, $args)
260  {
261  return $this->makeId($name, $args);
262  }
263 
271  public function makeId($name, array $args = array())
272  {
273  return md5($this->_cachedEntityLabel . '__' . $name . '__' . serialize($args));
274  }
275 }
$id
Definition: fieldset.phtml:14
save($data, $id=null, $tags=array(), $specificLifetime=false, $priority=8)
Definition: Core.php:348
setSpecificLifetime($specificLifetime=false)
Definition: Class.php:125
__construct(array $options=array())
Definition: Class.php:107
setCachedEntity($cachedEntity)
Definition: Class.php:167
$value
Definition: gender.phtml:16
setPriority($priority)
Definition: Class.php:135
setOption($name, $value)
Definition: Class.php:150
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
makeId($name, array $args=array())
Definition: Class.php:271
__call($name, $parameters)
Definition: Class.php:205
setTagsArray($tags=array())
Definition: Class.php:192
load($id, $doNotTestCacheValidity=false, $doNotUnserialize=false)
Definition: Core.php:296
if(!isset($_GET['name'])) $name
Definition: log.php:14