PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/Cache/Backend.php

https://bitbucket.org/bigstylee/zend-framework
PHP | 290 lines | 149 code | 21 blank | 120 comment | 34 complexity | 1c9ab961b80c36447d5020172e04057c 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_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 24989 2012-06-21 07:24:13Z 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. * Returns an option
  107. *
  108. * @param string $name Optional, the options name to return
  109. * @throws Zend_Cache_Exceptions
  110. * @return mixed
  111. */
  112. public function getOption($name)
  113. {
  114. $name = strtolower($name);
  115. if (array_key_exists($name, $this->_options)) {
  116. return $this->_options[$name];
  117. }
  118. if (array_key_exists($name, $this->_directives)) {
  119. return $this->_directives[$name];
  120. }
  121. Zend_Cache::throwException("Incorrect option name : {$name}");
  122. }
  123. /**
  124. * Get the life time
  125. *
  126. * if $specificLifetime is not false, the given specific life time is used
  127. * else, the global lifetime is used
  128. *
  129. * @param int $specificLifetime
  130. * @return int Cache life time
  131. */
  132. public function getLifetime($specificLifetime)
  133. {
  134. if ($specificLifetime === false) {
  135. return $this->_directives['lifetime'];
  136. }
  137. return $specificLifetime;
  138. }
  139. /**
  140. * Return true if the automatic cleaning is available for the backend
  141. *
  142. * DEPRECATED : use getCapabilities() instead
  143. *
  144. * @deprecated
  145. * @return boolean
  146. */
  147. public function isAutomaticCleaningAvailable()
  148. {
  149. return true;
  150. }
  151. /**
  152. * Determine system TMP directory and detect if we have read access
  153. *
  154. * inspired from Zend_File_Transfer_Adapter_Abstract
  155. *
  156. * @return string
  157. * @throws Zend_Cache_Exception if unable to determine directory
  158. */
  159. public function getTmpDir()
  160. {
  161. $tmpdir = array();
  162. foreach (array($_ENV, $_SERVER) as $tab) {
  163. foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
  164. if (isset($tab[$key]) && is_string($tab[$key])) {
  165. if (($key == 'windir') or ($key == 'SystemRoot')) {
  166. $dir = realpath($tab[$key] . '\\temp');
  167. } else {
  168. $dir = realpath($tab[$key]);
  169. }
  170. if ($this->_isGoodTmpDir($dir)) {
  171. return $dir;
  172. }
  173. }
  174. }
  175. }
  176. $upload = ini_get('upload_tmp_dir');
  177. if ($upload) {
  178. $dir = realpath($upload);
  179. if ($this->_isGoodTmpDir($dir)) {
  180. return $dir;
  181. }
  182. }
  183. if (function_exists('sys_get_temp_dir')) {
  184. $dir = sys_get_temp_dir();
  185. if ($this->_isGoodTmpDir($dir)) {
  186. return $dir;
  187. }
  188. }
  189. // Attemp to detect by creating a temporary file
  190. $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
  191. if ($tempFile) {
  192. $dir = realpath(dirname($tempFile));
  193. unlink($tempFile);
  194. if ($this->_isGoodTmpDir($dir)) {
  195. return $dir;
  196. }
  197. }
  198. if ($this->_isGoodTmpDir('/tmp')) {
  199. return '/tmp';
  200. }
  201. if ($this->_isGoodTmpDir('\\temp')) {
  202. return '\\temp';
  203. }
  204. Zend_Cache::throwException('Could not determine temp directory, please specify a cache_dir manually');
  205. }
  206. /**
  207. * Verify if the given temporary directory is readable and writable
  208. *
  209. * @param string $dir temporary directory
  210. * @return boolean true if the directory is ok
  211. */
  212. protected function _isGoodTmpDir($dir)
  213. {
  214. if (is_readable($dir)) {
  215. if (is_writable($dir)) {
  216. return true;
  217. }
  218. }
  219. return false;
  220. }
  221. /**
  222. * Make sure if we enable logging that the Zend_Log class
  223. * is available.
  224. * Create a default log object if none is set.
  225. *
  226. * @throws Zend_Cache_Exception
  227. * @return void
  228. */
  229. protected function _loggerSanity()
  230. {
  231. if (!isset($this->_directives['logging']) || !$this->_directives['logging']) {
  232. return;
  233. }
  234. if (isset($this->_directives['logger'])) {
  235. if ($this->_directives['logger'] instanceof Zend_Log) {
  236. return;
  237. }
  238. Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
  239. }
  240. // Create a default logger to the standard output stream
  241. require_once 'Zend/Log.php';
  242. require_once 'Zend/Log/Writer/Stream.php';
  243. require_once 'Zend/Log/Filter/Priority.php';
  244. $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
  245. $logger->addFilter(new Zend_Log_Filter_Priority(Zend_Log::WARN, '<='));
  246. $this->_directives['logger'] = $logger;
  247. }
  248. /**
  249. * Log a message at the WARN (4) priority.
  250. *
  251. * @param string $message
  252. * @throws Zend_Cache_Exception
  253. * @return void
  254. */
  255. protected function _log($message, $priority = 4)
  256. {
  257. if (!$this->_directives['logging']) {
  258. return;
  259. }
  260. if (!isset($this->_directives['logger'])) {
  261. Zend_Cache::throwException('Logging is enabled but logger is not set.');
  262. }
  263. $logger = $this->_directives['logger'];
  264. if (!$logger instanceof Zend_Log) {
  265. Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
  266. }
  267. $logger->log($message, $priority);
  268. }
  269. }