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

/library/Zend/Cache/Cache.php

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