PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Cache/Backend.php

https://bitbucket.org/Ebozavrik/test-application
PHP | 299 lines | 149 code | 23 blank | 127 comment | 34 complexity | a0d5aafe7fbf45f9d3f37c5667ec7d8e 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. *
  58. * @throws Zend_Cache_Exception
  59. * @return void
  60. */
  61. public function __construct (array $options = array())
  62. {
  63. while (list( $name, $value ) = each($options)) {
  64. $this->setOption($name, $value);
  65. }
  66. }
  67. /**
  68. * Set the frontend directives
  69. *
  70. * @param array $directives Assoc of directives
  71. *
  72. * @throws Zend_Cache_Exception
  73. * @return void
  74. */
  75. public function setDirectives ($directives)
  76. {
  77. if (!is_array($directives)) Zend_Cache::throwException('Directives parameter must be an array');
  78. while (list( $name, $value ) = each($directives)) {
  79. if (!is_string($name)) {
  80. Zend_Cache::throwException("Incorrect option name : $name");
  81. }
  82. $name = strtolower($name);
  83. if (array_key_exists($name, $this->_directives)) {
  84. $this->_directives[$name] = $value;
  85. }
  86. }
  87. $this->_loggerSanity();
  88. }
  89. /**
  90. * Set an option
  91. *
  92. * @param string $name
  93. * @param mixed $value
  94. *
  95. * @throws Zend_Cache_Exception
  96. * @return void
  97. */
  98. public function setOption ($name, $value)
  99. {
  100. if (!is_string($name)) {
  101. Zend_Cache::throwException("Incorrect option name : $name");
  102. }
  103. $name = strtolower($name);
  104. if (array_key_exists($name, $this->_options)) {
  105. $this->_options[$name] = $value;
  106. }
  107. }
  108. /**
  109. * Returns an option
  110. *
  111. * @param string $name Optional, the options name to return
  112. *
  113. * @throws Zend_Cache_Exceptions
  114. * @return mixed
  115. */
  116. public function getOption ($name)
  117. {
  118. $name = strtolower($name);
  119. if (array_key_exists($name, $this->_options)) {
  120. return $this->_options[$name];
  121. }
  122. if (array_key_exists($name, $this->_directives)) {
  123. return $this->_directives[$name];
  124. }
  125. Zend_Cache::throwException("Incorrect option name : {$name}");
  126. }
  127. /**
  128. * Get the life time
  129. *
  130. * if $specificLifetime is not false, the given specific life time is used
  131. * else, the global lifetime is used
  132. *
  133. * @param int $specificLifetime
  134. *
  135. * @return int Cache life time
  136. */
  137. public function getLifetime ($specificLifetime)
  138. {
  139. if ($specificLifetime === false) {
  140. return $this->_directives['lifetime'];
  141. }
  142. return $specificLifetime;
  143. }
  144. /**
  145. * Return true if the automatic cleaning is available for the backend
  146. *
  147. * DEPRECATED : use getCapabilities() instead
  148. *
  149. * @deprecated
  150. * @return boolean
  151. */
  152. public function isAutomaticCleaningAvailable ()
  153. {
  154. return true;
  155. }
  156. /**
  157. * Determine system TMP directory and detect if we have read access
  158. *
  159. * inspired from Zend_File_Transfer_Adapter_Abstract
  160. *
  161. * @return string
  162. * @throws Zend_Cache_Exception if unable to determine directory
  163. */
  164. public function getTmpDir ()
  165. {
  166. $tmpdir = array();
  167. foreach (array( $_ENV, $_SERVER ) as $tab) {
  168. foreach (array( 'TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot' ) as $key) {
  169. if (isset( $tab[$key] ) && is_string($tab[$key])) {
  170. if (( $key == 'windir' ) or ( $key == 'SystemRoot' )) {
  171. $dir = realpath($tab[$key] . '\\temp');
  172. } else {
  173. $dir = realpath($tab[$key]);
  174. }
  175. if ($this->_isGoodTmpDir($dir)) {
  176. return $dir;
  177. }
  178. }
  179. }
  180. }
  181. $upload = ini_get('upload_tmp_dir');
  182. if ($upload) {
  183. $dir = realpath($upload);
  184. if ($this->_isGoodTmpDir($dir)) {
  185. return $dir;
  186. }
  187. }
  188. if (function_exists('sys_get_temp_dir')) {
  189. $dir = sys_get_temp_dir();
  190. if ($this->_isGoodTmpDir($dir)) {
  191. return $dir;
  192. }
  193. }
  194. // Attemp to detect by creating a temporary file
  195. $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
  196. if ($tempFile) {
  197. $dir = realpath(dirname($tempFile));
  198. unlink($tempFile);
  199. if ($this->_isGoodTmpDir($dir)) {
  200. return $dir;
  201. }
  202. }
  203. if ($this->_isGoodTmpDir('/tmp')) {
  204. return '/tmp';
  205. }
  206. if ($this->_isGoodTmpDir('\\temp')) {
  207. return '\\temp';
  208. }
  209. Zend_Cache::throwException('Could not determine temp directory, please specify a cache_dir manually');
  210. }
  211. /**
  212. * Verify if the given temporary directory is readable and writable
  213. *
  214. * @param string $dir temporary directory
  215. *
  216. * @return boolean true if the directory is ok
  217. */
  218. protected function _isGoodTmpDir ($dir)
  219. {
  220. if (is_readable($dir)) {
  221. if (is_writable($dir)) {
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. /**
  228. * Make sure if we enable logging that the Zend_Log class
  229. * is available.
  230. * Create a default log object if none is set.
  231. *
  232. * @throws Zend_Cache_Exception
  233. * @return void
  234. */
  235. protected function _loggerSanity ()
  236. {
  237. if (!isset( $this->_directives['logging'] ) || !$this->_directives['logging']) {
  238. return;
  239. }
  240. if (isset( $this->_directives['logger'] )) {
  241. if ($this->_directives['logger'] instanceof Zend_Log) {
  242. return;
  243. }
  244. Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
  245. }
  246. // Create a default logger to the standard output stream
  247. require_once 'Zend/Log.php';
  248. require_once 'Zend/Log/Writer/Stream.php';
  249. require_once 'Zend/Log/Filter/Priority.php';
  250. $logger = new Zend_Log( new Zend_Log_Writer_Stream( 'php://output' ) );
  251. $logger->addFilter(new Zend_Log_Filter_Priority( Zend_Log::WARN, '<=' ));
  252. $this->_directives['logger'] = $logger;
  253. }
  254. /**
  255. * Log a message at the WARN (4) priority.
  256. *
  257. * @param string $message
  258. *
  259. * @throws Zend_Cache_Exception
  260. * @return void
  261. */
  262. protected function _log ($message, $priority = 4)
  263. {
  264. if (!$this->_directives['logging']) {
  265. return;
  266. }
  267. if (!isset( $this->_directives['logger'] )) {
  268. Zend_Cache::throwException('Logging is enabled but logger is not set.');
  269. }
  270. $logger = $this->_directives['logger'];
  271. if (!$logger instanceof Zend_Log) {
  272. Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
  273. }
  274. $logger->log($message, $priority);
  275. }
  276. }