Manager.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. * Constant holding reserved name for default Page Tag Cache
  37. */
  38. const PAGETAGCACHE = 'pagetag';
  39. /**
  40. * Array of caches stored by the Cache Manager instance
  41. *
  42. * @var array
  43. */
  44. protected $_caches = array();
  45. /**
  46. * Array of ready made configuration templates for lazy
  47. * loading caches.
  48. *
  49. * @var array
  50. */
  51. protected $_optionTemplates = array(
  52. // Null Cache (Enforce Null/Empty Values)
  53. 'skeleton' => array(
  54. 'frontend' => array(
  55. 'name' => null,
  56. 'options' => array(),
  57. ),
  58. 'backend' => array(
  59. 'name' => null,
  60. 'options' => array(),
  61. ),
  62. ),
  63. // Simple Common Default
  64. 'default' => array(
  65. 'frontend' => array(
  66. 'name' => 'Core',
  67. 'options' => array(
  68. 'automatic_serialization' => true,
  69. ),
  70. ),
  71. 'backend' => array(
  72. 'name' => 'File',
  73. 'options' => array(
  74. 'cache_dir' => '../cache',
  75. ),
  76. ),
  77. ),
  78. // Static Page HTML Cache
  79. 'page' => array(
  80. 'frontend' => array(
  81. 'name' => 'Capture',
  82. 'options' => array(
  83. 'ignore_user_abort' => true,
  84. ),
  85. ),
  86. 'backend' => array(
  87. 'name' => 'Static',
  88. 'options' => array(
  89. 'public_dir' => '../public',
  90. ),
  91. ),
  92. ),
  93. // Tag Cache
  94. 'pagetag' => array(
  95. 'frontend' => array(
  96. 'name' => 'Core',
  97. 'options' => array(
  98. 'automatic_serialization' => true,
  99. 'lifetime' => null
  100. ),
  101. ),
  102. 'backend' => array(
  103. 'name' => 'File',
  104. 'options' => array(
  105. 'cache_dir' => '../cache',
  106. ),
  107. ),
  108. ),
  109. );
  110. /**
  111. * Set a new cache for the Cache Manager to contain
  112. *
  113. * @param string $name
  114. * @param Zend_Cache_Core $cache
  115. * @return Zend_Cache_Manager
  116. */
  117. public function setCache($name, Zend_Cache_Core $cache)
  118. {
  119. $this->_caches[$name] = $cache;
  120. return $this;
  121. }
  122. /**
  123. * Check if the Cache Manager contains the named cache object, or a named
  124. * configuration template to lazy load the cache object
  125. *
  126. * @param string $name
  127. * @return bool
  128. */
  129. public function hasCache($name)
  130. {
  131. if (isset($this->_caches[$name])
  132. || $this->hasCacheTemplate($name)
  133. ) {
  134. return true;
  135. }
  136. return false;
  137. }
  138. /**
  139. * Fetch the named cache object, or instantiate and return a cache object
  140. * using a named configuration template
  141. *
  142. * @param string $name
  143. * @return Zend_Cache_Core
  144. */
  145. public function getCache($name)
  146. {
  147. if (isset($this->_caches[$name])) {
  148. return $this->_caches[$name];
  149. }
  150. if (isset($this->_optionTemplates[$name])) {
  151. if ($name == self::PAGECACHE
  152. && (!isset($this->_optionTemplates[$name]['backend']['options']['tag_cache'])
  153. || !$this->_optionTemplates[$name]['backend']['options']['tag_cache'] instanceof Zend_Cache_Core)
  154. ) {
  155. $this->_optionTemplates[$name]['backend']['options']['tag_cache']
  156. = $this->getCache(self::PAGETAGCACHE );
  157. }
  158. $this->_caches[$name] = Zend_Cache::factory(
  159. $this->_optionTemplates[$name]['frontend']['name'],
  160. $this->_optionTemplates[$name]['backend']['name'],
  161. isset($this->_optionTemplates[$name]['frontend']['options']) ? $this->_optionTemplates[$name]['frontend']['options'] : array(),
  162. isset($this->_optionTemplates[$name]['backend']['options']) ? $this->_optionTemplates[$name]['backend']['options'] : array()
  163. );
  164. return $this->_caches[$name];
  165. }
  166. }
  167. /**
  168. * Set a named configuration template from which a cache object can later
  169. * be lazy loaded
  170. *
  171. * @param string $name
  172. * @param array $options
  173. * @return Zend_Cache_Manager
  174. */
  175. public function setCacheTemplate($name, $options)
  176. {
  177. if ($options instanceof Zend_Config) {
  178. $options = $options->toArray();
  179. } elseif (!is_array($options)) {
  180. require_once 'Zend/Cache/Exception.php';
  181. throw new Zend_Cache_Exception('Options passed must be in'
  182. . ' an associative array or instance of Zend_Config');
  183. }
  184. $this->_optionTemplates[$name] = $options;
  185. return $this;
  186. }
  187. /**
  188. * Check if the named configuration template
  189. *
  190. * @param string $name
  191. * @return bool
  192. */
  193. public function hasCacheTemplate($name)
  194. {
  195. if (isset($this->_optionTemplates[$name])) {
  196. return true;
  197. }
  198. return false;
  199. }
  200. /**
  201. * Get the named configuration template
  202. *
  203. * @param string $name
  204. * @return array
  205. */
  206. public function getCacheTemplate($name)
  207. {
  208. if (isset($this->_optionTemplates[$name])) {
  209. return $this->_optionTemplates[$name];
  210. }
  211. }
  212. /**
  213. * Pass an array containing changes to be applied to a named
  214. * configuration
  215. * template
  216. *
  217. * @param string $name
  218. * @param array $options
  219. * @return Zend_Cache_Manager
  220. * @throws Zend_Cache_Exception for invalid options format or if option templates do not have $name
  221. */
  222. public function setTemplateOptions($name, $options)
  223. {
  224. if ($options instanceof Zend_Config) {
  225. $options = $options->toArray();
  226. } elseif (!is_array($options)) {
  227. require_once 'Zend/Cache/Exception.php';
  228. throw new Zend_Cache_Exception('Options passed must be in'
  229. . ' an associative array or instance of Zend_Config');
  230. }
  231. if (!isset($this->_optionTemplates[$name])) {
  232. throw new Zend_Cache_Exception('A cache configuration template'
  233. . 'does not exist with the name "' . $name . '"');
  234. }
  235. $this->_optionTemplates[$name]
  236. = $this->_mergeOptions($this->_optionTemplates[$name], $options);
  237. return $this;
  238. }
  239. /**
  240. * Simple method to merge two configuration arrays
  241. *
  242. * @param array $current
  243. * @param array $options
  244. * @return array
  245. */
  246. protected function _mergeOptions(array $current, array $options)
  247. {
  248. if (isset($options['frontend']['name'])) {
  249. $current['frontend']['name'] = $options['frontend']['name'];
  250. }
  251. if (isset($options['backend']['name'])) {
  252. $current['backend']['name'] = $options['backend']['name'];
  253. }
  254. if (isset($options['frontend']['options'])) {
  255. foreach ($options['frontend']['options'] as $key=>$value) {
  256. $current['frontend']['options'][$key] = $value;
  257. }
  258. }
  259. if (isset($options['backend']['options'])) {
  260. foreach ($options['backend']['options'] as $key=>$value) {
  261. $current['backend']['options'][$key] = $value;
  262. }
  263. }
  264. return $current;
  265. }
  266. }