Manager.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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$
  20. */
  21. /** @see Zend_Cache_Exception */
  22. require_once 'Zend/Cache/Exception.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Cache
  26. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Cache_Manager
  30. {
  31. /**
  32. * Constant holding reserved name for default Page Cache
  33. */
  34. const PAGECACHE = 'page';
  35. /**
  36. * Array of caches stored by the Cache Manager instance
  37. *
  38. * @var array
  39. */
  40. protected $_caches = array();
  41. /**
  42. * Array of ready made configuration templates for lazy
  43. * loading caches.
  44. *
  45. * @var array
  46. */
  47. protected $_optionTemplates = array(
  48. // Null Cache (Enforce Null/Empty Values)
  49. 'skeleton' => array(
  50. 'frontend' => array(
  51. 'name' => null,
  52. 'options' => array(),
  53. ),
  54. 'backend' => array(
  55. 'name' => null,
  56. 'options' => array(),
  57. ),
  58. ),
  59. // Simple Common Default
  60. 'default' => array(
  61. 'frontend' => array(
  62. 'name' => 'Core',
  63. 'options' => array(
  64. 'automatic_serialization' => true,
  65. ),
  66. ),
  67. 'backend' => array(
  68. 'name' => 'File',
  69. 'options' => array(
  70. 'cache_dir' => '../cache',
  71. ),
  72. ),
  73. ),
  74. // Static Page HTML Cache
  75. 'page' => array(
  76. 'frontend' => array(
  77. 'name' => 'Output',
  78. 'options' => array(
  79. 'ignore_user_abort' => true,
  80. ),
  81. ),
  82. 'backend' => array(
  83. 'name' => 'Static',
  84. 'options' => array(
  85. 'public_dir' => '../public',
  86. ),
  87. ),
  88. ),
  89. // Tag Cache
  90. 'tagcache' => array(
  91. 'frontend' => array(
  92. 'name' => 'Core',
  93. 'options' => array(
  94. 'automatic_serialization' => true,
  95. ),
  96. ),
  97. 'backend' => array(
  98. 'name' => 'File',
  99. 'options' => array(
  100. 'cache_dir' => '../cache',
  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('tagcache');
  152. }
  153. $this->_caches[$name] = Zend_Cache::factory(
  154. $this->_optionTemplates[$name]['frontend']['name'],
  155. $this->_optionTemplates[$name]['backend']['name'],
  156. $this->_optionTemplates[$name]['frontend']['options'],
  157. $this->_optionTemplates[$name]['backend']['options']
  158. );
  159. return $this->_caches[$name];
  160. }
  161. }
  162. /**
  163. * Set a named configuration template from which a cache object can later
  164. * be lazy loaded
  165. *
  166. * @param string $name
  167. * @param array $options
  168. * @return Zend_Cache_Manager
  169. */
  170. public function setCacheTemplate($name, $options)
  171. {
  172. if ($options instanceof Zend_Config) {
  173. $options = $options->toArray();
  174. } elseif (!is_array($options)) {
  175. require_once 'Zend/Cache/Exception.php';
  176. throw new Zend_Cache_Exception('Options passed must be in'
  177. . ' an associative array or instance of Zend_Config');
  178. }
  179. $this->_optionTemplates[$name] = $options;
  180. return $this;
  181. }
  182. /**
  183. * Check if the named configuration template
  184. *
  185. * @param string $name
  186. * @return bool
  187. */
  188. public function hasCacheTemplate($name)
  189. {
  190. if (isset($this->_optionTemplates[$name])) {
  191. return true;
  192. }
  193. return false;
  194. }
  195. /**
  196. * Get the named configuration template
  197. *
  198. * @param string $name
  199. * @return array
  200. */
  201. public function getCacheTemplate($name)
  202. {
  203. if (isset($this->_optionTemplates[$name])) {
  204. return $this->_optionTemplates[$name];
  205. }
  206. }
  207. /**
  208. * Pass an array containing changes to be applied to a named
  209. * configuration
  210. * template
  211. *
  212. * @param string $name
  213. * @param array $options
  214. * @return Zend_Cache_Manager
  215. * @throws Zend_Cache_Exception for invalid options format or if option templates do not have $name
  216. */
  217. public function setTemplateOptions($name, $options)
  218. {
  219. if ($options instanceof Zend_Config) {
  220. $options = $options->toArray();
  221. } elseif (!is_array($options)) {
  222. require_once 'Zend/Cache/Exception.php';
  223. throw new Zend_Cache_Exception('Options passed must be in'
  224. . ' an associative array or instance of Zend_Config');
  225. }
  226. if (!isset($this->_optionTemplates[$name])) {
  227. throw new Zend_Cache_Exception('A cache configuration template'
  228. . 'does not exist with the name "' . $name . '"');
  229. }
  230. $this->_optionTemplates[$name]
  231. = $this->_mergeOptions($this->_optionTemplates[$name], $options);
  232. return $this;
  233. }
  234. /**
  235. * Simple method to merge two configuration arrays
  236. *
  237. * @param array $current
  238. * @param array $options
  239. * @return array
  240. */
  241. protected function _mergeOptions(array $current, array $options)
  242. {
  243. if (isset($options['frontend']['name'])) {
  244. $current['frontend']['name'] = $options['frontend']['name'];
  245. }
  246. if (isset($options['backend']['name'])) {
  247. $current['backend']['name'] = $options['backend']['name'];
  248. }
  249. if (isset($options['frontend']['options'])) {
  250. foreach ($options['frontend']['options'] as $key=>$value) {
  251. $current['frontend']['options'][$key] = $value;
  252. }
  253. }
  254. if (isset($options['backend']['options'])) {
  255. foreach ($options['backend']['options'] as $key=>$value) {
  256. $current['backend']['options'][$key] = $value;
  257. }
  258. }
  259. return $current;
  260. }
  261. }