PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Cache.php

https://bitbucket.org/Ebozavrik/test-application
PHP | 261 lines | 116 code | 20 blank | 125 comment | 28 complexity | 57d973647eec6fc8eac4d4d7575dec47 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. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Cache.php 24656 2012-02-26 06:02:53Z adamlundrigan $
  20. */
  21. /**
  22. * @package Zend_Cache
  23. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  24. * @license http://framework.zend.com/license/new-bsd New BSD License
  25. */
  26. abstract class Zend_Cache
  27. {
  28. /**
  29. * Standard frontends
  30. *
  31. * @var array
  32. */
  33. public static $standardFrontends = array( 'Core', 'Output', 'Class', 'File', 'Function', 'Page' );
  34. /**
  35. * Standard backends
  36. *
  37. * @var array
  38. */
  39. public static $standardBackends = array( 'File', 'Sqlite', 'Memcached', 'Libmemcached', 'Apc', 'ZendPlatform',
  40. 'Xcache', 'TwoLevels', 'WinCache', 'ZendServer_Disk', 'ZendServer_ShMem' );
  41. /**
  42. * Standard backends which implement the ExtendedInterface
  43. *
  44. * @var array
  45. */
  46. public static $standardExtendedBackends = array( 'File', 'Apc', 'TwoLevels', 'Memcached', 'Libmemcached', 'Sqlite', 'WinCache' );
  47. /**
  48. * Only for backward compatibility (may be removed in next major release)
  49. *
  50. * @var array
  51. * @deprecated
  52. */
  53. public static $availableFrontends = array( 'Core', 'Output', 'Class', 'File', 'Function', 'Page' );
  54. /**
  55. * Only for backward compatibility (may be removed in next major release)
  56. *
  57. * @var array
  58. * @deprecated
  59. */
  60. public static $availableBackends = array( 'File', 'Sqlite', 'Memcached', 'Libmemcached', 'Apc', 'ZendPlatform', 'Xcache', 'WinCache', 'TwoLevels' );
  61. /**
  62. * Consts for clean() method
  63. */
  64. const CLEANING_MODE_ALL = 'all';
  65. const CLEANING_MODE_OLD = 'old';
  66. const CLEANING_MODE_MATCHING_TAG = 'matchingTag';
  67. const CLEANING_MODE_NOT_MATCHING_TAG = 'notMatchingTag';
  68. const CLEANING_MODE_MATCHING_ANY_TAG = 'matchingAnyTag';
  69. /**
  70. * Factory
  71. *
  72. * @param mixed $frontend frontend name (string) or Zend_Cache_Frontend_ object
  73. * @param mixed $backend backend name (string) or Zend_Cache_Backend_ object
  74. * @param array $frontendOptions associative array of options for the corresponding frontend constructor
  75. * @param array $backendOptions associative array of options for the corresponding backend constructor
  76. * @param boolean $customFrontendNaming if true, the frontend argument is used as a complete class name ; if false, the frontend argument is used as the end of "Zend_Cache_Frontend_[...]" class name
  77. * @param boolean $customBackendNaming if true, the backend argument is used as a complete class name ; if false, the backend argument is used as the end of "Zend_Cache_Backend_[...]" class name
  78. * @param boolean $autoload if true, there will no require_once for backend and frontend (useful only for custom backends/frontends)
  79. *
  80. * @throws Zend_Cache_Exception
  81. * @return Zend_Cache_Core|Zend_Cache_Frontend
  82. */
  83. public static function factory ($frontend, $backend, $frontendOptions = array(), $backendOptions = array(), $customFrontendNaming = false, $customBackendNaming = false, $autoload = false)
  84. {
  85. if (is_string($backend)) {
  86. $backendObject = self::_makeBackend($backend, $backendOptions, $customBackendNaming, $autoload);
  87. } else {
  88. if (( is_object($backend) ) && ( in_array('Zend_Cache_Backend_Interface', class_implements($backend)) )) {
  89. $backendObject = $backend;
  90. } else {
  91. self::throwException('backend must be a backend name (string) or an object which implements Zend_Cache_Backend_Interface');
  92. }
  93. }
  94. if (is_string($frontend)) {
  95. $frontendObject = self::_makeFrontend($frontend, $frontendOptions, $customFrontendNaming, $autoload);
  96. } else {
  97. if (is_object($frontend)) {
  98. $frontendObject = $frontend;
  99. } else {
  100. self::throwException('frontend must be a frontend name (string) or an object');
  101. }
  102. }
  103. $frontendObject->setBackend($backendObject);
  104. return $frontendObject;
  105. }
  106. /**
  107. * Backend Constructor
  108. *
  109. * @param string $backend
  110. * @param array $backendOptions
  111. * @param boolean $customBackendNaming
  112. * @param boolean $autoload
  113. *
  114. * @return Zend_Cache_Backend
  115. */
  116. public static function _makeBackend ($backend, $backendOptions, $customBackendNaming = false, $autoload = false)
  117. {
  118. if (!$customBackendNaming) {
  119. $backend = self::_normalizeName($backend);
  120. }
  121. if (in_array($backend, Zend_Cache::$standardBackends)) {
  122. // we use a standard backend
  123. $backendClass = 'Zend_Cache_Backend_' . $backend;
  124. // security controls are explicit
  125. require_once str_replace('_', DIRECTORY_SEPARATOR, $backendClass) . '.php';
  126. } else {
  127. // we use a custom backend
  128. if (!preg_match('~^[\w\\\\]+$~D', $backend)) {
  129. Zend_Cache::throwException("Invalid backend name [$backend]");
  130. }
  131. if (!$customBackendNaming) {
  132. // we use this boolean to avoid an API break
  133. $backendClass = 'Zend_Cache_Backend_' . $backend;
  134. } else {
  135. $backendClass = $backend;
  136. }
  137. if (!$autoload) {
  138. $file = str_replace('_', DIRECTORY_SEPARATOR, $backendClass) . '.php';
  139. if (!( self::_isReadable($file) )) {
  140. self::throwException("file $file not found in include_path");
  141. }
  142. require_once $file;
  143. }
  144. }
  145. return new $backendClass( $backendOptions );
  146. }
  147. /**
  148. * Frontend Constructor
  149. *
  150. * @param string $frontend
  151. * @param array $frontendOptions
  152. * @param boolean $customFrontendNaming
  153. * @param boolean $autoload
  154. *
  155. * @return Zend_Cache_Core|Zend_Cache_Frontend
  156. */
  157. public static function _makeFrontend ($frontend, $frontendOptions = array(), $customFrontendNaming = false, $autoload = false)
  158. {
  159. if (!$customFrontendNaming) {
  160. $frontend = self::_normalizeName($frontend);
  161. }
  162. if (in_array($frontend, self::$standardFrontends)) {
  163. // we use a standard frontend
  164. // For perfs reasons, with frontend == 'Core', we can interact with the Core itself
  165. $frontendClass = 'Zend_Cache_' . ( $frontend != 'Core' ? 'Frontend_' : '' ) . $frontend;
  166. // security controls are explicit
  167. require_once str_replace('_', DIRECTORY_SEPARATOR, $frontendClass) . '.php';
  168. } else {
  169. // we use a custom frontend
  170. if (!preg_match('~^[\w\\\\]+$~D', $frontend)) {
  171. Zend_Cache::throwException("Invalid frontend name [$frontend]");
  172. }
  173. if (!$customFrontendNaming) {
  174. // we use this boolean to avoid an API break
  175. $frontendClass = 'Zend_Cache_Frontend_' . $frontend;
  176. } else {
  177. $frontendClass = $frontend;
  178. }
  179. if (!$autoload) {
  180. $file = str_replace('_', DIRECTORY_SEPARATOR, $frontendClass) . '.php';
  181. if (!( self::_isReadable($file) )) {
  182. self::throwException("file $file not found in include_path");
  183. }
  184. require_once $file;
  185. }
  186. }
  187. return new $frontendClass( $frontendOptions );
  188. }
  189. /**
  190. * Throw an exception
  191. *
  192. * Note : for perf reasons, the "load" of Zend/Cache/Exception is dynamic
  193. *
  194. * @param string $msg Message for the exception
  195. *
  196. * @throws Zend_Cache_Exception
  197. */
  198. public static function throwException ($msg, Exception $e = null)
  199. {
  200. // For perfs reasons, we use this dynamic inclusion
  201. require_once 'Zend/Cache/Exception.php';
  202. throw new Zend_Cache_Exception( $msg, 0, $e );
  203. }
  204. /**
  205. * Normalize frontend and backend names to allow multiple words TitleCased
  206. *
  207. * @param string $name Name to normalize
  208. *
  209. * @return string
  210. */
  211. protected static function _normalizeName ($name)
  212. {
  213. $name = ucfirst(strtolower($name));
  214. $name = str_replace(array( '-', '_', '.' ), ' ', $name);
  215. $name = ucwords($name);
  216. $name = str_replace(' ', '', $name);
  217. if (stripos($name, 'ZendServer') === 0) {
  218. $name = 'ZendServer_' . substr($name, strlen('ZendServer'));
  219. }
  220. return $name;
  221. }
  222. /**
  223. * Returns TRUE if the $filename is readable, or FALSE otherwise.
  224. * This function uses the PHP include_path, where PHP's is_readable()
  225. * does not.
  226. *
  227. * Note : this method comes from Zend_Loader (see #ZF-2891 for details)
  228. *
  229. * @param string $filename
  230. *
  231. * @return boolean
  232. */
  233. private static function _isReadable ($filename)
  234. {
  235. if (!$fh = @fopen($filename, 'r', true)) {
  236. return false;
  237. }
  238. @fclose($fh);
  239. return true;
  240. }
  241. }