/zf/library/Zend/Cache/Manager.php

http://github.com/eryx/php-framework-benchmark · PHP · 298 lines · 163 code · 19 blank · 116 comment · 18 complexity · 4cd2813db347d87d47403341b27ba1b6 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. * @version $Id: Manager.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /** @see Zend_Cache_Exception */
  22. require_once 'Zend/Cache/Exception.php';
  23. /** @see Zend_Cache */
  24. require_once 'Zend/Cache.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Cache
  28. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Cache_Manager
  32. {
  33. /**
  34. * Constant holding reserved name for default Page Cache
  35. */
  36. const PAGECACHE = 'page';
  37. /**
  38. * Constant holding reserved name for default Page Tag Cache
  39. */
  40. const PAGETAGCACHE = 'pagetag';
  41. /**
  42. * Array of caches stored by the Cache Manager instance
  43. *
  44. * @var array
  45. */
  46. protected $_caches = array();
  47. /**
  48. * Array of ready made configuration templates for lazy
  49. * loading caches.
  50. *
  51. * @var array
  52. */
  53. protected $_optionTemplates = array(
  54. // Simple Common Default
  55. 'default' => array(
  56. 'frontend' => array(
  57. 'name' => 'Core',
  58. 'options' => array(
  59. 'automatic_serialization' => true,
  60. ),
  61. ),
  62. 'backend' => array(
  63. 'name' => 'File',
  64. 'options' => array(
  65. // use system temp dir by default of file backend
  66. // 'cache_dir' => '../cache',
  67. ),
  68. ),
  69. ),
  70. // Static Page HTML Cache
  71. 'page' => array(
  72. 'frontend' => array(
  73. 'name' => 'Capture',
  74. 'options' => array(
  75. 'ignore_user_abort' => true,
  76. ),
  77. ),
  78. 'backend' => array(
  79. 'name' => 'Static',
  80. 'options' => array(
  81. 'public_dir' => '../public',
  82. ),
  83. ),
  84. ),
  85. // Tag Cache
  86. 'pagetag' => array(
  87. 'frontend' => array(
  88. 'name' => 'Core',
  89. 'options' => array(
  90. 'automatic_serialization' => true,
  91. 'lifetime' => null
  92. ),
  93. ),
  94. 'backend' => array(
  95. 'name' => 'File',
  96. 'options' => array(
  97. // use system temp dir by default of file backend
  98. // 'cache_dir' => '../cache',
  99. // use default umask of file backend
  100. // 'cache_file_umask' => 0644
  101. ),
  102. ),
  103. ),
  104. );
  105. /**
  106. * Set a new cache for the Cache Manager to contain
  107. *
  108. * @param string $name
  109. * @param Zend_Cache_Core $cache
  110. * @return Zend_Cache_Manager
  111. */
  112. public function setCache($name, Zend_Cache_Core $cache)
  113. {
  114. $this->_caches[$name] = $cache;
  115. return $this;
  116. }
  117. /**
  118. * Check if the Cache Manager contains the named cache object, or a named
  119. * configuration template to lazy load the cache object
  120. *
  121. * @param string $name
  122. * @return bool
  123. */
  124. public function hasCache($name)
  125. {
  126. if (isset($this->_caches[$name])
  127. || $this->hasCacheTemplate($name)
  128. ) {
  129. return true;
  130. }
  131. return false;
  132. }
  133. /**
  134. * Fetch the named cache object, or instantiate and return a cache object
  135. * using a named configuration template
  136. *
  137. * @param string $name
  138. * @return Zend_Cache_Core
  139. */
  140. public function getCache($name)
  141. {
  142. if (isset($this->_caches[$name])) {
  143. return $this->_caches[$name];
  144. }
  145. if (isset($this->_optionTemplates[$name])) {
  146. if ($name == self::PAGECACHE
  147. && (!isset($this->_optionTemplates[$name]['backend']['options']['tag_cache'])
  148. || !$this->_optionTemplates[$name]['backend']['options']['tag_cache'] instanceof Zend_Cache_Core)
  149. ) {
  150. $this->_optionTemplates[$name]['backend']['options']['tag_cache']
  151. = $this->getCache(self::PAGETAGCACHE);
  152. }
  153. $this->_caches[$name] = Zend_Cache::factory(
  154. $this->_optionTemplates[$name]['frontend']['name'],
  155. $this->_optionTemplates[$name]['backend']['name'],
  156. isset($this->_optionTemplates[$name]['frontend']['options']) ? $this->_optionTemplates[$name]['frontend']['options'] : array(),
  157. isset($this->_optionTemplates[$name]['backend']['options']) ? $this->_optionTemplates[$name]['backend']['options'] : array(),
  158. isset($this->_optionTemplates[$name]['frontend']['customFrontendNaming']) ? $this->_optionTemplates[$name]['frontend']['customFrontendNaming'] : false,
  159. isset($this->_optionTemplates[$name]['backend']['customBackendNaming']) ? $this->_optionTemplates[$name]['backend']['customBackendNaming'] : false,
  160. isset($this->_optionTemplates[$name]['frontendBackendAutoload']) ? $this->_optionTemplates[$name]['frontendBackendAutoload'] : false
  161. );
  162. return $this->_caches[$name];
  163. }
  164. }
  165. /**
  166. * Fetch all available caches
  167. *
  168. * @return array An array of all available caches with it's names as key
  169. */
  170. public function getCaches()
  171. {
  172. $caches = $this->_caches;
  173. foreach ($this->_optionTemplates as $name => $tmp) {
  174. if (!isset($caches[$name])) {
  175. $caches[$name] = $this->getCache($name);
  176. }
  177. }
  178. return $caches;
  179. }
  180. /**
  181. * Set a named configuration template from which a cache object can later
  182. * be lazy loaded
  183. *
  184. * @param string $name
  185. * @param array $options
  186. * @return Zend_Cache_Manager
  187. */
  188. public function setCacheTemplate($name, $options)
  189. {
  190. if ($options instanceof Zend_Config) {
  191. $options = $options->toArray();
  192. } elseif (!is_array($options)) {
  193. require_once 'Zend/Cache/Exception.php';
  194. throw new Zend_Cache_Exception('Options passed must be in'
  195. . ' an associative array or instance of Zend_Config');
  196. }
  197. $this->_optionTemplates[$name] = $options;
  198. return $this;
  199. }
  200. /**
  201. * Check if the named configuration template
  202. *
  203. * @param string $name
  204. * @return bool
  205. */
  206. public function hasCacheTemplate($name)
  207. {
  208. if (isset($this->_optionTemplates[$name])) {
  209. return true;
  210. }
  211. return false;
  212. }
  213. /**
  214. * Get the named configuration template
  215. *
  216. * @param string $name
  217. * @return array
  218. */
  219. public function getCacheTemplate($name)
  220. {
  221. if (isset($this->_optionTemplates[$name])) {
  222. return $this->_optionTemplates[$name];
  223. }
  224. }
  225. /**
  226. * Pass an array containing changes to be applied to a named
  227. * configuration
  228. * template
  229. *
  230. * @param string $name
  231. * @param array $options
  232. * @return Zend_Cache_Manager
  233. * @throws Zend_Cache_Exception for invalid options format or if option templates do not have $name
  234. */
  235. public function setTemplateOptions($name, $options)
  236. {
  237. if ($options instanceof Zend_Config) {
  238. $options = $options->toArray();
  239. } elseif (!is_array($options)) {
  240. require_once 'Zend/Cache/Exception.php';
  241. throw new Zend_Cache_Exception('Options passed must be in'
  242. . ' an associative array or instance of Zend_Config');
  243. }
  244. if (!isset($this->_optionTemplates[$name])) {
  245. throw new Zend_Cache_Exception('A cache configuration template'
  246. . 'does not exist with the name "' . $name . '"');
  247. }
  248. $this->_optionTemplates[$name]
  249. = $this->_mergeOptions($this->_optionTemplates[$name], $options);
  250. return $this;
  251. }
  252. /**
  253. * Simple method to merge two configuration arrays
  254. *
  255. * @param array $current
  256. * @param array $options
  257. * @return array
  258. */
  259. protected function _mergeOptions(array $current, array $options)
  260. {
  261. if (isset($options['frontend']['name'])) {
  262. $current['frontend']['name'] = $options['frontend']['name'];
  263. }
  264. if (isset($options['backend']['name'])) {
  265. $current['backend']['name'] = $options['backend']['name'];
  266. }
  267. if (isset($options['frontend']['options'])) {
  268. foreach ($options['frontend']['options'] as $key=>$value) {
  269. $current['frontend']['options'][$key] = $value;
  270. }
  271. }
  272. if (isset($options['backend']['options'])) {
  273. foreach ($options['backend']['options'] as $key=>$value) {
  274. $current['backend']['options'][$key] = $value;
  275. }
  276. }
  277. return $current;
  278. }
  279. }