Backend.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_Backend
  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. * @package Zend_Cache
  23. * @subpackage Zend_Cache_Backend
  24. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. class Zend_Cache_Backend
  28. {
  29. /**
  30. * Frontend or Core directives
  31. *
  32. * =====> (int) lifetime :
  33. * - Cache lifetime (in seconds)
  34. * - If null, the cache is valid forever
  35. *
  36. * =====> (int) logging :
  37. * - if set to true, a logging is activated throw Zend_Log
  38. *
  39. * @var array directives
  40. */
  41. protected $_directives = array(
  42. 'lifetime' => 3600,
  43. 'logging' => false,
  44. 'logger' => null
  45. );
  46. /**
  47. * Available options
  48. *
  49. * @var array available options
  50. */
  51. protected $_options = array();
  52. /**
  53. * Constructor
  54. *
  55. * @param array $options Associative array of options
  56. * @throws Zend_Cache_Exception
  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. }
  65. /**
  66. * Set the frontend directives
  67. *
  68. * @param array $directives Assoc of directives
  69. * @throws Zend_Cache_Exception
  70. * @return void
  71. */
  72. public function setDirectives($directives)
  73. {
  74. if (!is_array($directives)) Zend_Cache::throwException('Directives parameter must be an array');
  75. while (list($name, $value) = each($directives)) {
  76. if (!is_string($name)) {
  77. Zend_Cache::throwException("Incorrect option name : $name");
  78. }
  79. $name = strtolower($name);
  80. if (array_key_exists($name, $this->_directives)) {
  81. $this->_directives[$name] = $value;
  82. }
  83. }
  84. $this->_loggerSanity();
  85. }
  86. /**
  87. * Set an option
  88. *
  89. * @param string $name
  90. * @param mixed $value
  91. * @throws Zend_Cache_Exception
  92. * @return void
  93. */
  94. public function setOption($name, $value)
  95. {
  96. if (!is_string($name)) {
  97. Zend_Cache::throwException("Incorrect option name : $name");
  98. }
  99. $name = strtolower($name);
  100. if (array_key_exists($name, $this->_options)) {
  101. $this->_options[$name] = $value;
  102. }
  103. }
  104. /**
  105. * Get the life time
  106. *
  107. * if $specificLifetime is not false, the given specific life time is used
  108. * else, the global lifetime is used
  109. *
  110. * @param int $specificLifetime
  111. * @return int Cache life time
  112. */
  113. public function getLifetime($specificLifetime)
  114. {
  115. if ($specificLifetime === false) {
  116. return $this->_directives['lifetime'];
  117. }
  118. return $specificLifetime;
  119. }
  120. /**
  121. * Return true if the automatic cleaning is available for the backend
  122. *
  123. * DEPRECATED : use getCapabilities() instead
  124. *
  125. * @deprecated
  126. * @return boolean
  127. */
  128. public function isAutomaticCleaningAvailable()
  129. {
  130. return true;
  131. }
  132. /**
  133. * Return a system-wide tmp directory
  134. *
  135. * @return string System-wide tmp directory
  136. */
  137. static function getTmpDir()
  138. {
  139. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  140. // windows...
  141. foreach (array($_ENV, $_SERVER) as $tab) {
  142. foreach (array('TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
  143. if (isset($tab[$key])) {
  144. $result = $tab[$key];
  145. if (($key == 'windir') or ($key == 'SystemRoot')) {
  146. $result = $result . '\\temp';
  147. }
  148. return $result;
  149. }
  150. }
  151. }
  152. return '\\temp';
  153. } else {
  154. // unix...
  155. if (isset($_ENV['TMPDIR'])) return $_ENV['TMPDIR'];
  156. if (isset($_SERVER['TMPDIR'])) return $_SERVER['TMPDIR'];
  157. return '/tmp';
  158. }
  159. }
  160. /**
  161. * Make sure if we enable logging that the Zend_Log class
  162. * is available.
  163. * Create a default log object if none is set.
  164. *
  165. * @throws Zend_Cache_Exception
  166. * @return void
  167. */
  168. protected function _loggerSanity()
  169. {
  170. if (!isset($this->_directives['logging']) || !$this->_directives['logging']) {
  171. return;
  172. }
  173. try {
  174. /**
  175. * @see Zend_Log
  176. */
  177. require_once 'Zend/Log.php';
  178. } catch (Zend_Exception $e) {
  179. Zend_Cache::throwException('Logging feature is enabled but the Zend_Log class is not available');
  180. }
  181. if (isset($this->_directives['logger'])) {
  182. if ($this->_directives['logger'] instanceof Zend_Log) {
  183. return;
  184. } else {
  185. Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
  186. }
  187. }
  188. // Create a default logger to the standard output stream
  189. require_once 'Zend/Log/Writer/Stream.php';
  190. $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
  191. $this->_directives['logger'] = $logger;
  192. }
  193. /**
  194. * Log a message at the WARN (4) priority.
  195. *
  196. * @param string $message
  197. * @throws Zend_Cache_Exception
  198. * @return void
  199. */
  200. protected function _log($message, $priority = 4)
  201. {
  202. if (!$this->_directives['logging']) {
  203. return;
  204. }
  205. if (!isset($this->_directives['logger'])) {
  206. Zend_Cache::throwException('Logging is enabled but logger is not set.');
  207. }
  208. $logger = $this->_directives['logger'];
  209. if (!$logger instanceof Zend_Log) {
  210. Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
  211. }
  212. $logger->log($message, $priority);
  213. }
  214. }