PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/zendframework1/library/Zend/Cache/Frontend/Class.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 275 lines | 108 code | 26 blank | 141 comment | 16 complexity | e0fba9da74e040fc5fcbfeede03147d2 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Cache
  17. * @subpackage Zend_Cache_Frontend
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Cache_Core
  24. */
  25. #require_once 'Zend/Cache/Core.php';
  26. /**
  27. * @package Zend_Cache
  28. * @subpackage Zend_Cache_Frontend
  29. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Cache_Frontend_Class extends Zend_Cache_Core
  33. {
  34. /**
  35. * Available options
  36. *
  37. * ====> (mixed) cached_entity :
  38. * - if set to a class name, we will cache an abstract class and will use only static calls
  39. * - if set to an object, we will cache this object methods
  40. *
  41. * ====> (boolean) cache_by_default :
  42. * - if true, method calls will be cached by default
  43. *
  44. * ====> (array) cached_methods :
  45. * - an array of method names which will be cached (even if cache_by_default = false)
  46. *
  47. * ====> (array) non_cached_methods :
  48. * - an array of method names which won't be cached (even if cache_by_default = true)
  49. *
  50. * @var array available options
  51. */
  52. protected $_specificOptions = array(
  53. 'cached_entity' => null,
  54. 'cache_by_default' => true,
  55. 'cached_methods' => array(),
  56. 'non_cached_methods' => array()
  57. );
  58. /**
  59. * Tags array
  60. *
  61. * @var array
  62. */
  63. protected $_tags = array();
  64. /**
  65. * SpecificLifetime value
  66. *
  67. * false => no specific life time
  68. *
  69. * @var bool|int
  70. */
  71. protected $_specificLifetime = false;
  72. /**
  73. * The cached object or the name of the cached abstract class
  74. *
  75. * @var mixed
  76. */
  77. protected $_cachedEntity = null;
  78. /**
  79. * The class name of the cached object or cached abstract class
  80. *
  81. * Used to differentiate between different classes with the same method calls.
  82. *
  83. * @var string
  84. */
  85. protected $_cachedEntityLabel = '';
  86. /**
  87. * Priority (used by some particular backends)
  88. *
  89. * @var int
  90. */
  91. protected $_priority = 8;
  92. /**
  93. * Constructor
  94. *
  95. * @param array $options Associative array of options
  96. * @throws Zend_Cache_Exception
  97. */
  98. public function __construct(array $options = array())
  99. {
  100. foreach ($options as $name => $value) {
  101. $this->setOption($name, $value);
  102. }
  103. if ($this->_specificOptions['cached_entity'] === null) {
  104. Zend_Cache::throwException('cached_entity must be set !');
  105. }
  106. $this->setCachedEntity($this->_specificOptions['cached_entity']);
  107. $this->setOption('automatic_serialization', true);
  108. }
  109. /**
  110. * Set a specific life time
  111. *
  112. * @param bool|int $specificLifetime
  113. * @return void
  114. */
  115. public function setSpecificLifetime($specificLifetime = false)
  116. {
  117. $this->_specificLifetime = $specificLifetime;
  118. }
  119. /**
  120. * Set the priority (used by some particular backends)
  121. *
  122. * @param int $priority integer between 0 (very low priority) and 10 (maximum priority)
  123. */
  124. public function setPriority($priority)
  125. {
  126. $this->_priority = $priority;
  127. }
  128. /**
  129. * Public frontend to set an option
  130. *
  131. * Just a wrapper to get a specific behaviour for cached_entity
  132. *
  133. * @param string $name Name of the option
  134. * @param mixed $value Value of the option
  135. * @throws Zend_Cache_Exception
  136. * @return void
  137. */
  138. public function setOption($name, $value)
  139. {
  140. if ($name == 'cached_entity') {
  141. $this->setCachedEntity($value);
  142. } else {
  143. parent::setOption($name, $value);
  144. }
  145. }
  146. /**
  147. * Specific method to set the cachedEntity
  148. *
  149. * if set to a class name, we will cache an abstract class and will use only static calls
  150. * if set to an object, we will cache this object methods
  151. *
  152. * @param mixed $cachedEntity
  153. */
  154. public function setCachedEntity($cachedEntity)
  155. {
  156. if (!is_string($cachedEntity) && !is_object($cachedEntity)) {
  157. Zend_Cache::throwException(
  158. 'cached_entity must be an object or a class name'
  159. );
  160. }
  161. $this->_cachedEntity = $cachedEntity;
  162. $this->_specificOptions['cached_entity'] = $cachedEntity;
  163. if (is_string($this->_cachedEntity)) {
  164. $this->_cachedEntityLabel = $this->_cachedEntity;
  165. } else {
  166. $ro = new ReflectionObject($this->_cachedEntity);
  167. $this->_cachedEntityLabel = $ro->getName();
  168. }
  169. }
  170. /**
  171. * Set the cache array
  172. *
  173. * @param array $tags
  174. * @return void
  175. */
  176. public function setTagsArray($tags = array())
  177. {
  178. $this->_tags = $tags;
  179. }
  180. /**
  181. * Main method : call the specified method or get the result from cache
  182. *
  183. * @param string $name Method name
  184. * @param array $parameters Method parameters
  185. * @return mixed Result
  186. * @throws Exception
  187. */
  188. public function __call($name, $parameters)
  189. {
  190. $callback = array($this->_cachedEntity, $name);
  191. if (!is_callable($callback, false)) {
  192. Zend_Cache::throwException('Invalid callback');
  193. }
  194. $cacheBool1 = $this->_specificOptions['cache_by_default'];
  195. $cacheBool2 = in_array($name, $this->_specificOptions['cached_methods']);
  196. $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_methods']);
  197. $cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
  198. if (!$cache) {
  199. // We do not have not cache
  200. return call_user_func_array($callback, $parameters);
  201. }
  202. $id = $this->makeId($name, $parameters);
  203. if (($rs = $this->load($id)) && (array_key_exists(0, $rs))
  204. && (array_key_exists(1, $rs))
  205. ) {
  206. // A cache is available
  207. $output = $rs[0];
  208. $return = $rs[1];
  209. } else {
  210. // A cache is not available (or not valid for this frontend)
  211. ob_start();
  212. ob_implicit_flush(false);
  213. try {
  214. $return = call_user_func_array($callback, $parameters);
  215. $output = ob_get_clean();
  216. $data = array($output, $return);
  217. $this->save(
  218. $data, $id, $this->_tags, $this->_specificLifetime,
  219. $this->_priority
  220. );
  221. } catch (Exception $e) {
  222. ob_end_clean();
  223. throw $e;
  224. }
  225. }
  226. echo $output;
  227. return $return;
  228. }
  229. /**
  230. * ZF-9970
  231. *
  232. * @deprecated
  233. */
  234. private function _makeId($name, $args)
  235. {
  236. return $this->makeId($name, $args);
  237. }
  238. /**
  239. * Make a cache id from the method name and parameters
  240. *
  241. * @param string $name Method name
  242. * @param array $args Method parameters
  243. * @return string Cache id
  244. */
  245. public function makeId($name, array $args = array())
  246. {
  247. return md5($this->_cachedEntityLabel . '__' . $name . '__' . serialize($args));
  248. }
  249. }