PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/Ebozavrik/test-application
PHP | 183 lines | 87 code | 16 blank | 80 comment | 12 complexity | efe8db03cd60213327524f6b6221eb5c 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-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 24593 2012-01-05 20:35:02Z 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. *
  58. * @return void
  59. */
  60. public function __construct (array $options = array())
  61. {
  62. while (list( $name, $value ) = each($options)) {
  63. $this->setOption($name, $value);
  64. }
  65. $this->setOption('automatic_serialization', true);
  66. }
  67. /**
  68. * Main method : call the specified function or get the result from cache
  69. *
  70. * @param callback $callback A valid callback
  71. * @param array $parameters Function parameters
  72. * @param array $tags Cache tags
  73. * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
  74. * @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
  75. *
  76. * @return mixed Result
  77. */
  78. public function call ($callback, array $parameters = array(), $tags = array(), $specificLifetime = false, $priority = 8)
  79. {
  80. if (!is_callable($callback, true, $name)) {
  81. Zend_Cache::throwException('Invalid callback');
  82. }
  83. $cacheBool1 = $this->_specificOptions['cache_by_default'];
  84. $cacheBool2 = in_array($name, $this->_specificOptions['cached_functions']);
  85. $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_functions']);
  86. $cache = ( ( $cacheBool1 || $cacheBool2 ) && ( !$cacheBool3 ) );
  87. if (!$cache) {
  88. // Caching of this callback is disabled
  89. return call_user_func_array($callback, $parameters);
  90. }
  91. $id = $this->_makeId($callback, $parameters);
  92. if (( $rs = $this->load($id) ) && isset( $rs[0], $rs[1] )) {
  93. // A cache is available
  94. $output = $rs[0];
  95. $return = $rs[1];
  96. } else {
  97. // A cache is not available (or not valid for this frontend)
  98. ob_start();
  99. ob_implicit_flush(false);
  100. $return = call_user_func_array($callback, $parameters);
  101. $output = ob_get_clean();
  102. $data = array( $output, $return );
  103. $this->save($data, $id, $tags, $specificLifetime, $priority);
  104. }
  105. echo $output;
  106. return $return;
  107. }
  108. /**
  109. * ZF-9970
  110. *
  111. * @deprecated
  112. */
  113. private function _makeId ($callback, array $args)
  114. {
  115. return $this->makeId($callback, $args);
  116. }
  117. /**
  118. * Make a cache id from the function name and parameters
  119. *
  120. * @param callback $callback A valid callback
  121. * @param array $args Function parameters
  122. *
  123. * @throws Zend_Cache_Exception
  124. * @return string Cache id
  125. */
  126. public function makeId ($callback, array $args = array())
  127. {
  128. if (!is_callable($callback, true, $name)) {
  129. Zend_Cache::throwException('Invalid callback');
  130. }
  131. // functions, methods and classnames are case-insensitive
  132. $name = strtolower($name);
  133. // generate a unique id for object callbacks
  134. if (is_object($callback)) { // Closures & __invoke
  135. $object = $callback;
  136. } elseif (isset( $callback[0] )) { // array($object, 'method')
  137. $object = $callback[0];
  138. }
  139. if (isset( $object )) {
  140. try {
  141. $tmp = @serialize($callback);
  142. } catch (Exception $e) {
  143. Zend_Cache::throwException($e->getMessage());
  144. }
  145. if (!$tmp) {
  146. $lastErr = error_get_last();
  147. Zend_Cache::throwException("Can't serialize callback object to generate id: {$lastErr['message']}");
  148. }
  149. $name .= '__' . $tmp;
  150. }
  151. // generate a unique id for arguments
  152. $argsStr = '';
  153. if ($args) {
  154. try {
  155. $argsStr = @serialize(array_values($args));
  156. } catch (Exception $e) {
  157. Zend_Cache::throwException($e->getMessage());
  158. }
  159. if (!$argsStr) {
  160. $lastErr = error_get_last();
  161. throw Zend_Cache::throwException("Can't serialize arguments to generate id: {$lastErr['message']}");
  162. }
  163. }
  164. return md5($name . $argsStr);
  165. }
  166. }