/ui/obminclude/lib/Zend/Cache.php

https://github.com/goldoraf/OBM · PHP · 201 lines · 85 code · 17 blank · 99 comment · 18 complexity · 9e504339d5f9efbd2e5f9deb7fc21020 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-2008 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 10893 2008-08-12 08:05:14Z fab $
  20. */
  21. /**
  22. * @package Zend_Cache
  23. * @copyright Copyright (c) 2005-2008 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');
  40. /**
  41. * Only for backward compatibily (may be removed in next major release)
  42. *
  43. * @var array
  44. * @deprecated
  45. */
  46. public static $availableFrontends = array('Core', 'Output', 'Class', 'File', 'Function', 'Page');
  47. /**
  48. * Only for backward compatibily (may be removed in next major release)
  49. *
  50. * @var array
  51. * @deprecated
  52. */
  53. public static $availableBackends = array('File', 'Sqlite', 'Memcached', 'Apc', 'ZendPlatform', 'Xcache');
  54. /**
  55. * Consts for clean() method
  56. */
  57. const CLEANING_MODE_ALL = 'all';
  58. const CLEANING_MODE_OLD = 'old';
  59. const CLEANING_MODE_MATCHING_TAG = 'matchingTag';
  60. const CLEANING_MODE_NOT_MATCHING_TAG = 'notMatchingTag';
  61. /**
  62. * Factory
  63. *
  64. * @param string $frontend frontend name
  65. * @param string $backend backend name
  66. * @param array $frontendOptions associative array of options for the corresponding frontend constructor
  67. * @param array $backendOptions associative array of options for the corresponding backend constructor
  68. * @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
  69. * @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
  70. * @param boolean $autoload if true, there will no require_once for backend and frontend (usefull only for custom backends/frontends)
  71. * @throws Zend_Cache_Exception
  72. * @return Zend_Cache_Frontend
  73. */
  74. public static function factory($frontend, $backend, $frontendOptions = array(), $backendOptions = array(), $customFrontendNaming = false, $customBackendNaming = false, $autoload = false)
  75. {
  76. // because lowercase will fail
  77. if (!$customFrontendNaming) {
  78. $frontend = self::_normalizeName($frontend);
  79. }
  80. if (!$customBackendNaming) {
  81. $backend = self::_normalizeName($backend);
  82. }
  83. // working on the frontend
  84. if (in_array($frontend, self::$standardFrontends)) {
  85. // we use a standard frontend
  86. // For perfs reasons, with frontend == 'Core', we can interact with the Core itself
  87. $frontendClass = 'Zend_Cache_' . ($frontend != 'Core' ? 'Frontend_' : '') . $frontend;
  88. // security controls are explicit
  89. require_once str_replace('_', DIRECTORY_SEPARATOR, $frontendClass) . '.php';
  90. } else {
  91. // we use a custom frontend
  92. if (!preg_match('~^[\w]+$~D', $frontend)) {
  93. Zend_Cache::throwException("Invalid frontend name [$frontend]");
  94. }
  95. if (!$customFrontendNaming) {
  96. // we use this boolean to avoid an API break
  97. $frontendClass = 'Zend_Cache_Frontend_' . $frontend;
  98. } else {
  99. $frontendClass = $frontend;
  100. }
  101. if (!$autoload) {
  102. $file = str_replace('_', DIRECTORY_SEPARATOR, $frontendClass) . '.php';
  103. if (!(self::_isReadable($file))) {
  104. self::throwException("file $file not found in include_path");
  105. }
  106. require_once $file;
  107. }
  108. }
  109. // working on the backend
  110. if (in_array($backend, Zend_Cache::$standardBackends)) {
  111. // we use a standard backend
  112. $backendClass = 'Zend_Cache_Backend_' . $backend;
  113. // security controls are explicit
  114. require_once str_replace('_', DIRECTORY_SEPARATOR, $backendClass) . '.php';
  115. } else {
  116. // we use a custom backend
  117. if (!preg_match('~^[\w]+$~D', $backend)) {
  118. Zend_Cache::throwException("Invalid backend name [$backend]");
  119. }
  120. if (!$customBackendNaming) {
  121. // we use this boolean to avoid an API break
  122. $backendClass = 'Zend_Cache_Backend_' . $backend;
  123. } else {
  124. $backendClass = $backend;
  125. }
  126. if (!$autoload) {
  127. $file = str_replace('_', DIRECTORY_SEPARATOR, $backendClass) . '.php';
  128. if (!(self::_isReadable($file))) {
  129. self::throwException("file $file not found in include_path");
  130. }
  131. require_once $file;
  132. }
  133. }
  134. // Making objects
  135. $frontendObject = new $frontendClass($frontendOptions);
  136. $backendObject = new $backendClass($backendOptions);
  137. $frontendObject->setBackend($backendObject);
  138. return $frontendObject;
  139. }
  140. /**
  141. * Throw an exception
  142. *
  143. * Note : for perf reasons, the "load" of Zend/Cache/Exception is dynamic
  144. * @param string $msg Message for the exception
  145. * @throws Zend_Cache_Exception
  146. */
  147. public static function throwException($msg)
  148. {
  149. // For perfs reasons, we use this dynamic inclusion
  150. require_once 'Zend/Cache/Exception.php';
  151. throw new Zend_Cache_Exception($msg);
  152. }
  153. /**
  154. * Normalize frontend and backend names to allow multiple words TitleCased
  155. *
  156. * @param string $name Name to normalize
  157. * @return string
  158. */
  159. protected static function _normalizeName($name)
  160. {
  161. $name = ucfirst(strtolower($name));
  162. $name = str_replace(array('-', '_', '.'), ' ', $name);
  163. $name = ucwords($name);
  164. $name = str_replace(' ', '', $name);
  165. return $name;
  166. }
  167. /**
  168. * Returns TRUE if the $filename is readable, or FALSE otherwise.
  169. * This function uses the PHP include_path, where PHP's is_readable()
  170. * does not.
  171. *
  172. * Note : this method comes from Zend_Loader (see #ZF-2891 for details)
  173. *
  174. * @param string $filename
  175. * @return boolean
  176. */
  177. private static function _isReadable($filename)
  178. {
  179. if (!$fh = @fopen($filename, 'r', true)) {
  180. return false;
  181. }
  182. return true;
  183. }
  184. }