/library/Zend/Cache/Backend/AbstractBackend.php

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