Manager.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. 'cache_file_umask' => 0644
  107. ),
  108. ),
  109. ),
  110. );
  111. /**
  112. * Set a new cache for the Cache Manager to contain
  113. *
  114. * @param string $name
  115. * @param Zend_Cache_Core $cache
  116. * @return Zend_Cache_Manager
  117. */
  118. public function setCache($name, Zend_Cache_Core $cache)
  119. {
  120. $this->_caches[$name] = $cache;
  121. return $this;
  122. }
  123. /**
  124. * Check if the Cache Manager contains the named cache object, or a named
  125. * configuration template to lazy load the cache object
  126. *
  127. * @param string $name
  128. * @return bool
  129. */
  130. public function hasCache($name)
  131. {
  132. if (isset($this->_caches[$name])
  133. || $this->hasCacheTemplate($name)
  134. ) {
  135. return true;
  136. }
  137. return false;
  138. }
  139. /**
  140. * Fetch the named cache object, or instantiate and return a cache object
  141. * using a named configuration template
  142. *
  143. * @param string $name
  144. * @return Zend_Cache_Core
  145. */
  146. public function getCache($name)
  147. {
  148. if (isset($this->_caches[$name])) {
  149. return $this->_caches[$name];
  150. }
  151. if (isset($this->_optionTemplates[$name])) {
  152. if ($name == self::PAGECACHE
  153. && (!isset($this->_optionTemplates[$name]['backend']['options']['tag_cache'])
  154. || !$this->_optionTemplates[$name]['backend']['options']['tag_cache'] instanceof Zend_Cache_Core)
  155. ) {
  156. $this->_optionTemplates[$name]['backend']['options']['tag_cache']
  157. = $this->getCache(self::PAGETAGCACHE );
  158. }
  159. $this->_caches[$name] = Zend_Cache::factory(
  160. $this->_optionTemplates[$name]['frontend']['name'],
  161. $this->_optionTemplates[$name]['backend']['name'],
  162. isset($this->_optionTemplates[$name]['frontend']['options']) ? $this->_optionTemplates[$name]['frontend']['options'] : array(),
  163. isset($this->_optionTemplates[$name]['backend']['options']) ? $this->_optionTemplates[$name]['backend']['options'] : array()
  164. );
  165. return $this->_caches[$name];
  166. }
  167. }
  168. /**
  169. * Set a named configuration template from which a cache object can later
  170. * be lazy loaded
  171. *
  172. * @param string $name
  173. * @param array $options
  174. * @return Zend_Cache_Manager
  175. */
  176. public function setCacheTemplate($name, $options)
  177. {
  178. if ($options instanceof Zend_Config) {
  179. $options = $options->toArray();
  180. } elseif (!is_array($options)) {
  181. require_once 'Zend/Cache/Exception.php';
  182. throw new Zend_Cache_Exception('Options passed must be in'
  183. . ' an associative array or instance of Zend_Config');
  184. }
  185. $this->_optionTemplates[$name] = $options;
  186. return $this;
  187. }
  188. /**
  189. * Check if the named configuration template
  190. *
  191. * @param string $name
  192. * @return bool
  193. */
  194. public function hasCacheTemplate($name)
  195. {
  196. if (isset($this->_optionTemplates[$name])) {
  197. return true;
  198. }
  199. return false;
  200. }
  201. /**
  202. * Get the named configuration template
  203. *
  204. * @param string $name
  205. * @return array
  206. */
  207. public function getCacheTemplate($name)
  208. {
  209. if (isset($this->_optionTemplates[$name])) {
  210. return $this->_optionTemplates[$name];
  211. }
  212. }
  213. /**
  214. * Pass an array containing changes to be applied to a named
  215. * configuration
  216. * template
  217. *
  218. * @param string $name
  219. * @param array $options
  220. * @return Zend_Cache_Manager
  221. * @throws Zend_Cache_Exception for invalid options format or if option templates do not have $name
  222. */
  223. public function setTemplateOptions($name, $options)
  224. {
  225. if ($options instanceof Zend_Config) {
  226. $options = $options->toArray();
  227. } elseif (!is_array($options)) {
  228. require_once 'Zend/Cache/Exception.php';
  229. throw new Zend_Cache_Exception('Options passed must be in'
  230. . ' an associative array or instance of Zend_Config');
  231. }
  232. if (!isset($this->_optionTemplates[$name])) {
  233. throw new Zend_Cache_Exception('A cache configuration template'
  234. . 'does not exist with the name "' . $name . '"');
  235. }
  236. $this->_optionTemplates[$name]
  237. = $this->_mergeOptions($this->_optionTemplates[$name], $options);
  238. return $this;
  239. }
  240. /**
  241. * Simple method to merge two configuration arrays
  242. *
  243. * @param array $current
  244. * @param array $options
  245. * @return array
  246. */
  247. protected function _mergeOptions(array $current, array $options)
  248. {
  249. if (isset($options['frontend']['name'])) {
  250. $current['frontend']['name'] = $options['frontend']['name'];
  251. }
  252. if (isset($options['backend']['name'])) {
  253. $current['backend']['name'] = $options['backend']['name'];
  254. }
  255. if (isset($options['frontend']['options'])) {
  256. foreach ($options['frontend']['options'] as $key=>$value) {
  257. $current['frontend']['options'][$key] = $value;
  258. }
  259. }
  260. if (isset($options['backend']['options'])) {
  261. foreach ($options['backend']['options'] as $key=>$value) {
  262. $current['backend']['options'][$key] = $value;
  263. }
  264. }
  265. return $current;
  266. }
  267. }