PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Cache/Backend.php

https://bitbucket.org/hamidrezas/melobit
PHP | 268 lines | 138 code | 17 blank | 113 comment | 32 complexity | c5726166b23c223d0abdac884c0f3590 MD5 | raw file
Possible License(s): AGPL-1.0
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Backend.php 24600 2012-01-08 15:58:57Z mabe $
  21. */
  22. /**
  23. * @package Zend_Cache
  24. * @subpackage Zend_Cache_Backend
  25. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. class Zend_Cache_Backend
  29. {
  30. /**
  31. * Frontend or Core directives
  32. *
  33. * =====> (int) lifetime :
  34. * - Cache lifetime (in seconds)
  35. * - If null, the cache is valid forever
  36. *
  37. * =====> (int) logging :
  38. * - if set to true, a logging is activated throw Zend_Log
  39. *
  40. * @var array directives
  41. */
  42. protected $_directives = array(
  43. 'lifetime' => 3600,
  44. 'logging' => false,
  45. 'logger' => null
  46. );
  47. /**
  48. * Available options
  49. *
  50. * @var array available options
  51. */
  52. protected $_options = array();
  53. /**
  54. * Constructor
  55. *
  56. * @param array $options Associative array of options
  57. * @throws Zend_Cache_Exception
  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. }
  66. /**
  67. * Set the frontend directives
  68. *
  69. * @param array $directives Assoc of directives
  70. * @throws Zend_Cache_Exception
  71. * @return void
  72. */
  73. public function setDirectives($directives)
  74. {
  75. if (!is_array($directives)) Zend_Cache::throwException('Directives parameter must be an array');
  76. while (list($name, $value) = each($directives)) {
  77. if (!is_string($name)) {
  78. Zend_Cache::throwException("Incorrect option name : $name");
  79. }
  80. $name = strtolower($name);
  81. if (array_key_exists($name, $this->_directives)) {
  82. $this->_directives[$name] = $value;
  83. }
  84. }
  85. $this->_loggerSanity();
  86. }
  87. /**
  88. * Set an option
  89. *
  90. * @param string $name
  91. * @param mixed $value
  92. * @throws Zend_Cache_Exception
  93. * @return void
  94. */
  95. public function setOption($name, $value)
  96. {
  97. if (!is_string($name)) {
  98. Zend_Cache::throwException("Incorrect option name : $name");
  99. }
  100. $name = strtolower($name);
  101. if (array_key_exists($name, $this->_options)) {
  102. $this->_options[$name] = $value;
  103. }
  104. }
  105. /**
  106. * Get the life time
  107. *
  108. * if $specificLifetime is not false, the given specific life time is used
  109. * else, the global lifetime is used
  110. *
  111. * @param int $specificLifetime
  112. * @return int Cache life time
  113. */
  114. public function getLifetime($specificLifetime)
  115. {
  116. if ($specificLifetime === false) {
  117. return $this->_directives['lifetime'];
  118. }
  119. return $specificLifetime;
  120. }
  121. /**
  122. * Return true if the automatic cleaning is available for the backend
  123. *
  124. * DEPRECATED : use getCapabilities() instead
  125. *
  126. * @deprecated
  127. * @return boolean
  128. */
  129. public function isAutomaticCleaningAvailable()
  130. {
  131. return true;
  132. }
  133. /**
  134. * Determine system TMP directory and detect if we have read access
  135. *
  136. * inspired from Zend_File_Transfer_Adapter_Abstract
  137. *
  138. * @return string
  139. * @throws Zend_Cache_Exception if unable to determine directory
  140. */
  141. public function getTmpDir()
  142. {
  143. $tmpdir = array();
  144. foreach (array($_ENV, $_SERVER) as $tab) {
  145. foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
  146. if (isset($tab[$key]) && is_string($tab[$key])) {
  147. if (($key == 'windir') or ($key == 'SystemRoot')) {
  148. $dir = realpath($tab[$key] . '\\temp');
  149. } else {
  150. $dir = realpath($tab[$key]);
  151. }
  152. if ($this->_isGoodTmpDir($dir)) {
  153. return $dir;
  154. }
  155. }
  156. }
  157. }
  158. $upload = ini_get('upload_tmp_dir');
  159. if ($upload) {
  160. $dir = realpath($upload);
  161. if ($this->_isGoodTmpDir($dir)) {
  162. return $dir;
  163. }
  164. }
  165. if (function_exists('sys_get_temp_dir')) {
  166. $dir = sys_get_temp_dir();
  167. if ($this->_isGoodTmpDir($dir)) {
  168. return $dir;
  169. }
  170. }
  171. // Attemp to detect by creating a temporary file
  172. $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
  173. if ($tempFile) {
  174. $dir = realpath(dirname($tempFile));
  175. unlink($tempFile);
  176. if ($this->_isGoodTmpDir($dir)) {
  177. return $dir;
  178. }
  179. }
  180. if ($this->_isGoodTmpDir('/tmp')) {
  181. return '/tmp';
  182. }
  183. if ($this->_isGoodTmpDir('\\temp')) {
  184. return '\\temp';
  185. }
  186. Zend_Cache::throwException('Could not determine temp directory, please specify a cache_dir manually');
  187. }
  188. /**
  189. * Verify if the given temporary directory is readable and writable
  190. *
  191. * @param string $dir temporary directory
  192. * @return boolean true if the directory is ok
  193. */
  194. protected function _isGoodTmpDir($dir)
  195. {
  196. if (is_readable($dir)) {
  197. if (is_writable($dir)) {
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. /**
  204. * Make sure if we enable logging that the Zend_Log class
  205. * is available.
  206. * Create a default log object if none is set.
  207. *
  208. * @throws Zend_Cache_Exception
  209. * @return void
  210. */
  211. protected function _loggerSanity()
  212. {
  213. if (!isset($this->_directives['logging']) || !$this->_directives['logging']) {
  214. return;
  215. }
  216. if (isset($this->_directives['logger'])) {
  217. if ($this->_directives['logger'] instanceof Zend_Log) {
  218. return;
  219. }
  220. Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
  221. }
  222. // Create a default logger to the standard output stream
  223. require_once 'Zend/Log.php';
  224. require_once 'Zend/Log/Writer/Stream.php';
  225. require_once 'Zend/Log/Filter/Priority.php';
  226. $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
  227. $logger->addFilter(new Zend_Log_Filter_Priority(Zend_Log::WARN, '<='));
  228. $this->_directives['logger'] = $logger;
  229. }
  230. /**
  231. * Log a message at the WARN (4) priority.
  232. *
  233. * @param string $message
  234. * @throws Zend_Cache_Exception
  235. * @return void
  236. */
  237. protected function _log($message, $priority = 4)
  238. {
  239. if (!$this->_directives['logging']) {
  240. return;
  241. }
  242. if (!isset($this->_directives['logger'])) {
  243. Zend_Cache::throwException('Logging is enabled but logger is not set.');
  244. }
  245. $logger = $this->_directives['logger'];
  246. if (!$logger instanceof Zend_Log) {
  247. Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
  248. }
  249. $logger->log($message, $priority);
  250. }
  251. }