PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Cache/Frontend/Function.php

https://bitbucket.org/ksekar/campus
PHP | 179 lines | 87 code | 15 blank | 77 comment | 12 complexity | 82cde971206fba9e320bfbcfc6484bf3 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Function.php 24594 2012-01-05 21:27:01Z matthew $
  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-2012 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_Function extends Zend_Cache_Core
  33. {
  34. /**
  35. * This frontend specific options
  36. *
  37. * ====> (boolean) cache_by_default :
  38. * - if true, function calls will be cached by default
  39. *
  40. * ====> (array) cached_functions :
  41. * - an array of function names which will be cached (even if cache_by_default = false)
  42. *
  43. * ====> (array) non_cached_functions :
  44. * - an array of function names which won't be cached (even if cache_by_default = true)
  45. *
  46. * @var array options
  47. */
  48. protected $_specificOptions = array(
  49. 'cache_by_default' => true,
  50. 'cached_functions' => array(),
  51. 'non_cached_functions' => array()
  52. );
  53. /**
  54. * Constructor
  55. *
  56. * @param array $options Associative array of options
  57. * @return void
  58. */
  59. public function __construct(array $options = array())
  60. {
  61. while (list($name, $value) = each($options)) {
  62. $this->setOption($name, $value);
  63. }
  64. $this->setOption('automatic_serialization', true);
  65. }
  66. /**
  67. * Main method : call the specified function or get the result from cache
  68. *
  69. * @param callback $callback A valid callback
  70. * @param array $parameters Function parameters
  71. * @param array $tags Cache tags
  72. * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
  73. * @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
  74. * @return mixed Result
  75. */
  76. public function call($callback, array $parameters = array(), $tags = array(), $specificLifetime = false, $priority = 8)
  77. {
  78. if (!is_callable($callback, true, $name)) {
  79. Zend_Cache::throwException('Invalid callback');
  80. }
  81. $cacheBool1 = $this->_specificOptions['cache_by_default'];
  82. $cacheBool2 = in_array($name, $this->_specificOptions['cached_functions']);
  83. $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_functions']);
  84. $cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
  85. if (!$cache) {
  86. // Caching of this callback is disabled
  87. return call_user_func_array($callback, $parameters);
  88. }
  89. $id = $this->_makeId($callback, $parameters);
  90. if ( ($rs = $this->load($id)) && isset($rs[0], $rs[1])) {
  91. // A cache is available
  92. $output = $rs[0];
  93. $return = $rs[1];
  94. } else {
  95. // A cache is not available (or not valid for this frontend)
  96. ob_start();
  97. ob_implicit_flush(false);
  98. $return = call_user_func_array($callback, $parameters);
  99. $output = ob_get_clean();
  100. $data = array($output, $return);
  101. $this->save($data, $id, $tags, $specificLifetime, $priority);
  102. }
  103. echo $output;
  104. return $return;
  105. }
  106. /**
  107. * ZF-9970
  108. *
  109. * @deprecated
  110. */
  111. private function _makeId($callback, array $args)
  112. {
  113. return $this->makeId($callback, $args);
  114. }
  115. /**
  116. * Make a cache id from the function name and parameters
  117. *
  118. * @param callback $callback A valid callback
  119. * @param array $args Function parameters
  120. * @throws Zend_Cache_Exception
  121. * @return string Cache id
  122. */
  123. public function makeId($callback, array $args = array())
  124. {
  125. if (!is_callable($callback, true, $name)) {
  126. Zend_Cache::throwException('Invalid callback');
  127. }
  128. // functions, methods and classnames are case-insensitive
  129. $name = strtolower($name);
  130. // generate a unique id for object callbacks
  131. if (is_object($callback)) { // Closures & __invoke
  132. $object = $callback;
  133. } elseif (isset($callback[0])) { // array($object, 'method')
  134. $object = $callback[0];
  135. }
  136. if (isset($object)) {
  137. try {
  138. $tmp = @serialize($callback);
  139. } catch (Exception $e) {
  140. Zend_Cache::throwException($e->getMessage());
  141. }
  142. if (!$tmp) {
  143. $lastErr = error_get_last();
  144. Zend_Cache::throwException("Can't serialize callback object to generate id: {$lastErr['message']}");
  145. }
  146. $name.= '__' . $tmp;
  147. }
  148. // generate a unique id for arguments
  149. $argsStr = '';
  150. if ($args) {
  151. try {
  152. $argsStr = @serialize(array_values($args));
  153. } catch (Exception $e) {
  154. Zend_Cache::throwException($e->getMessage());
  155. }
  156. if (!$argsStr) {
  157. $lastErr = error_get_last();
  158. throw Zend_Cache::throwException("Can't serialize arguments to generate id: {$lastErr['message']}");
  159. }
  160. }
  161. return md5($name . $argsStr);
  162. }
  163. }