PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Zend/Cache.php

https://github.com/luisbraschi/ursp
PHP | 250 lines | 111 code | 16 blank | 123 comment | 28 complexity | 9807d5769510c49dcc4698882db72e46 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-2010 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 21974 2010-04-23 17:10:17Z alexander $
  20. */
  21. /**
  22. * @package Zend_Cache
  23. * @copyright Copyright (c) 2005-2010 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', 'Apc', 'ZendPlatform',
  40. 'Xcache', 'TwoLevels', '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', 'Sqlite');
  47. /**
  48. * Only for backward compatibily (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 compatibily (may be removed in next major release)
  56. *
  57. * @var array
  58. * @deprecated
  59. */
  60. public static $availableBackends = array('File', 'Sqlite', 'Memcached', 'Apc', 'ZendPlatform', 'Xcache', '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 (usefull only for custom backends/frontends)
  79. * @throws Zend_Cache_Exception
  80. * @return Zend_Cache_Core|Zend_Cache_Frontend
  81. */
  82. public static function factory($frontend, $backend, $frontendOptions = array(), $backendOptions = array(), $customFrontendNaming = false, $customBackendNaming = false, $autoload = false)
  83. {
  84. if (is_string($backend)) {
  85. $backendObject = self::_makeBackend($backend, $backendOptions, $customBackendNaming, $autoload);
  86. } else {
  87. if ((is_object($backend)) && (in_array('Zend_Cache_Backend_Interface', class_implements($backend)))) {
  88. $backendObject = $backend;
  89. } else {
  90. self::throwException('backend must be a backend name (string) or an object which implements Zend_Cache_Backend_Interface');
  91. }
  92. }
  93. if (is_string($frontend)) {
  94. $frontendObject = self::_makeFrontend($frontend, $frontendOptions, $customFrontendNaming, $autoload);
  95. } else {
  96. if (is_object($frontend)) {
  97. $frontendObject = $frontend;
  98. } else {
  99. self::throwException('frontend must be a frontend name (string) or an object');
  100. }
  101. }
  102. $frontendObject->setBackend($backendObject);
  103. return $frontendObject;
  104. }
  105. /**
  106. * Frontend Constructor
  107. *
  108. * @param string $backend
  109. * @param array $backendOptions
  110. * @param boolean $customBackendNaming
  111. * @param boolean $autoload
  112. * @return Zend_Cache_Backend
  113. */
  114. public static function _makeBackend($backend, $backendOptions, $customBackendNaming = false, $autoload = false)
  115. {
  116. if (!$customBackendNaming) {
  117. $backend = self::_normalizeName($backend);
  118. }
  119. if (in_array($backend, Zend_Cache::$standardBackends)) {
  120. // we use a standard backend
  121. $backendClass = 'Zend_Cache_Backend_' . $backend;
  122. // security controls are explicit
  123. #require_once str_replace('_', DIRECTORY_SEPARATOR, $backendClass) . '.php';
  124. } else {
  125. // we use a custom backend
  126. if (!preg_match('~^[\w]+$~D', $backend)) {
  127. Zend_Cache::throwException("Invalid backend name [$backend]");
  128. }
  129. if (!$customBackendNaming) {
  130. // we use this boolean to avoid an API break
  131. $backendClass = 'Zend_Cache_Backend_' . $backend;
  132. } else {
  133. $backendClass = $backend;
  134. }
  135. if (!$autoload) {
  136. $file = str_replace('_', DIRECTORY_SEPARATOR, $backendClass) . '.php';
  137. if (!(self::_isReadable($file))) {
  138. self::throwException("file $file not found in include_path");
  139. }
  140. #require_once $file;
  141. }
  142. }
  143. return new $backendClass($backendOptions);
  144. }
  145. /**
  146. * Backend Constructor
  147. *
  148. * @param string $frontend
  149. * @param array $frontendOptions
  150. * @param boolean $customFrontendNaming
  151. * @param boolean $autoload
  152. * @return Zend_Cache_Core|Zend_Cache_Frontend
  153. */
  154. public static function _makeFrontend($frontend, $frontendOptions = array(), $customFrontendNaming = false, $autoload = false)
  155. {
  156. if (!$customFrontendNaming) {
  157. $frontend = self::_normalizeName($frontend);
  158. }
  159. if (in_array($frontend, self::$standardFrontends)) {
  160. // we use a standard frontend
  161. // For perfs reasons, with frontend == 'Core', we can interact with the Core itself
  162. $frontendClass = 'Zend_Cache_' . ($frontend != 'Core' ? 'Frontend_' : '') . $frontend;
  163. // security controls are explicit
  164. #require_once str_replace('_', DIRECTORY_SEPARATOR, $frontendClass) . '.php';
  165. } else {
  166. // we use a custom frontend
  167. if (!preg_match('~^[\w]+$~D', $frontend)) {
  168. Zend_Cache::throwException("Invalid frontend name [$frontend]");
  169. }
  170. if (!$customFrontendNaming) {
  171. // we use this boolean to avoid an API break
  172. $frontendClass = 'Zend_Cache_Frontend_' . $frontend;
  173. } else {
  174. $frontendClass = $frontend;
  175. }
  176. if (!$autoload) {
  177. $file = str_replace('_', DIRECTORY_SEPARATOR, $frontendClass) . '.php';
  178. if (!(self::_isReadable($file))) {
  179. self::throwException("file $file not found in include_path");
  180. }
  181. #require_once $file;
  182. }
  183. }
  184. return new $frontendClass($frontendOptions);
  185. }
  186. /**
  187. * Throw an exception
  188. *
  189. * Note : for perf reasons, the "load" of Zend/Cache/Exception is dynamic
  190. * @param string $msg Message for the exception
  191. * @throws Zend_Cache_Exception
  192. */
  193. public static function throwException($msg, Exception $e = null)
  194. {
  195. // For perfs reasons, we use this dynamic inclusion
  196. #require_once 'Zend/Cache/Exception.php';
  197. throw new Zend_Cache_Exception($msg, 0, $e);
  198. }
  199. /**
  200. * Normalize frontend and backend names to allow multiple words TitleCased
  201. *
  202. * @param string $name Name to normalize
  203. * @return string
  204. */
  205. protected static function _normalizeName($name)
  206. {
  207. $name = ucfirst(strtolower($name));
  208. $name = str_replace(array('-', '_', '.'), ' ', $name);
  209. $name = ucwords($name);
  210. $name = str_replace(' ', '', $name);
  211. if (stripos($name, 'ZendServer') === 0) {
  212. $name = 'ZendServer_' . substr($name, strlen('ZendServer'));
  213. }
  214. return $name;
  215. }
  216. /**
  217. * Returns TRUE if the $filename is readable, or FALSE otherwise.
  218. * This function uses the PHP include_path, where PHP's is_readable()
  219. * does not.
  220. *
  221. * Note : this method comes from Zend_Loader (see #ZF-2891 for details)
  222. *
  223. * @param string $filename
  224. * @return boolean
  225. */
  226. private static function _isReadable($filename)
  227. {
  228. if (!$fh = @fopen($filename, 'r', true)) {
  229. return false;
  230. }
  231. @fclose($fh);
  232. return true;
  233. }
  234. }