Manager.php 8.6 KB

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