PageRenderTime 61ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/frapi/library/Zend/Cache.php

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