Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Function.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Cache/Core.php';
28 
29 
37 {
52  protected $_specificOptions = array(
53  'cache_by_default' => true,
54  'cached_functions' => array(),
55  'non_cached_functions' => array()
56  );
57 
64  public function __construct(array $options = array())
65  {
66  foreach ($options as $name => $value) {
67  $this->setOption($name, $value);
68  }
69  $this->setOption('automatic_serialization', true);
70  }
71 
82  public function call($callback, array $parameters = array(), $tags = array(), $specificLifetime = false, $priority = 8)
83  {
84  if (!is_callable($callback, true, $name)) {
85  Zend_Cache::throwException('Invalid callback');
86  }
87 
88  $cacheBool1 = $this->_specificOptions['cache_by_default'];
89  $cacheBool2 = in_array($name, $this->_specificOptions['cached_functions']);
90  $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_functions']);
91  $cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
92  if (!$cache) {
93  // Caching of this callback is disabled
94  return call_user_func_array($callback, $parameters);
95  }
96 
97  $id = $this->_makeId($callback, $parameters);
98  if ( ($rs = $this->load($id)) && isset($rs[0], $rs[1])) {
99  // A cache is available
100  $output = $rs[0];
101  $return = $rs[1];
102  } else {
103  // A cache is not available (or not valid for this frontend)
104  ob_start();
105  ob_implicit_flush(false);
106  $return = call_user_func_array($callback, $parameters);
107  $output = ob_get_clean();
108  $data = array($output, $return);
109  $this->save($data, $id, $tags, $specificLifetime, $priority);
110  }
111 
112  echo $output;
113  return $return;
114  }
115 
121  private function _makeId($callback, array $args)
122  {
123  return $this->makeId($callback, $args);
124  }
125 
134  public function makeId($callback, array $args = array())
135  {
136  if (!is_callable($callback, true, $name)) {
137  Zend_Cache::throwException('Invalid callback');
138  }
139 
140  // functions, methods and classnames are case-insensitive
141  $name = strtolower($name);
142 
143  // generate a unique id for object callbacks
144  if (is_object($callback)) { // Closures & __invoke
145  $object = $callback;
146  } elseif (isset($callback[0])) { // array($object, 'method')
147  $object = $callback[0];
148  }
149  if (isset($object)) {
150  try {
151  $tmp = @serialize($callback);
152  } catch (Exception $e) {
153  Zend_Cache::throwException($e->getMessage());
154  }
155  if (!$tmp) {
156  $lastErr = error_get_last();
157  Zend_Cache::throwException("Can't serialize callback object to generate id: {$lastErr['message']}");
158  }
159  $name.= '__' . $tmp;
160  }
161 
162  // generate a unique id for arguments
163  $argsStr = '';
164  if ($args) {
165  try {
166  $argsStr = @serialize(array_values($args));
167  } catch (Exception $e) {
168  Zend_Cache::throwException($e->getMessage());
169  }
170  if (!$argsStr) {
171  $lastErr = error_get_last();
172  throw Zend_Cache::throwException("Can't serialize arguments to generate id: {$lastErr['message']}");
173  }
174  }
175 
176  return md5($name . $argsStr);
177  }
178 
179 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
save($data, $id=null, $tags=array(), $specificLifetime=false, $priority=8)
Definition: Core.php:348
makeId($callback, array $args=array())
Definition: Function.php:134
setOption($name, $value)
Definition: Core.php:211
$value
Definition: gender.phtml:16
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
call($callback, array $parameters=array(), $tags=array(), $specificLifetime=false, $priority=8)
Definition: Function.php:82
load($id, $doNotTestCacheValidity=false, $doNotUnserialize=false)
Definition: Core.php:296
__construct(array $options=array())
Definition: Function.php:64
if(!isset($_GET['name'])) $name
Definition: log.php:14