PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/baruffaldi/cms-php-bfcms
PHP | 129 lines | 53 code | 8 blank | 68 comment | 8 complexity | b50426a7bfa62dffb7c07798a051f369 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Cache_Core
  23. */
  24. require_once 'Zend/Cache/Core.php';
  25. /**
  26. * @package Zend_Cache
  27. * @subpackage Zend_Cache_Frontend
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Cache_Frontend_Function extends Zend_Cache_Core
  32. {
  33. /**
  34. * This frontend specific options
  35. *
  36. * ====> (boolean) cache_by_default :
  37. * - if true, function calls will be cached by default
  38. *
  39. * ====> (array) cached_functions :
  40. * - an array of function names which will be cached (even if cache_by_default = false)
  41. *
  42. * ====> (array) non_cached_functions :
  43. * - an array of function names which won't be cached (even if cache_by_default = true)
  44. *
  45. * @var array options
  46. */
  47. protected $_specificOptions = array(
  48. 'cache_by_default' => true,
  49. 'cached_functions' => array(),
  50. 'non_cached_functions' => array()
  51. );
  52. /**
  53. * Constructor
  54. *
  55. * @param array $options Associative array of options
  56. * @return void
  57. */
  58. public function __construct(array $options = array())
  59. {
  60. while (list($name, $value) = each($options)) {
  61. $this->setOption($name, $value);
  62. }
  63. $this->setOption('automatic_serialization', true);
  64. }
  65. /**
  66. * Main method : call the specified function or get the result from cache
  67. *
  68. * @param string $name Function name
  69. * @param array $parameters Function parameters
  70. * @param array $tags Cache tags
  71. * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
  72. * @return mixed Result
  73. */
  74. public function call($name, $parameters = array(), $tags = array(), $specificLifetime = false)
  75. {
  76. $cacheBool1 = $this->_specificOptions['cache_by_default'];
  77. $cacheBool2 = in_array($name, $this->_specificOptions['cached_functions']);
  78. $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_functions']);
  79. $cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
  80. if (!$cache) {
  81. // We do not have not cache
  82. return call_user_func_array($name, $parameters);
  83. }
  84. $id = $this->_makeId($name, $parameters);
  85. if ($this->test($id)) {
  86. // A cache is available
  87. $result = $this->load($id);
  88. $output = $result[0];
  89. $return = $result[1];
  90. } else {
  91. // A cache is not available
  92. ob_start();
  93. ob_implicit_flush(false);
  94. $return = call_user_func_array($name, $parameters);
  95. $output = ob_get_contents();
  96. ob_end_clean();
  97. $data = array($output, $return);
  98. $this->save($data, $id, $tags, $specificLifetime);
  99. }
  100. echo $output;
  101. return $return;
  102. }
  103. /**
  104. * Make a cache id from the function name and parameters
  105. *
  106. * @param string $name Function name
  107. * @param array $parameters Function parameters
  108. * @throws Zend_Cache_Exception
  109. * @return string Cache id
  110. */
  111. private function _makeId($name, $parameters)
  112. {
  113. if (!is_string($name)) {
  114. Zend_Cache::throwException('Incorrect function name');
  115. }
  116. if (!is_array($parameters)) {
  117. Zend_Cache::throwException('parameters argument must be an array');
  118. }
  119. return md5($name . serialize($parameters));
  120. }
  121. }