Manager.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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-2015 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. /** @see Zend_Cache */
  24. #require_once 'Zend/Cache.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Cache
  28. * @copyright Copyright (c) 2005-2015 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. * @throws Zend_Cache_Exception
  188. */
  189. public function setCacheTemplate($name, $options)
  190. {
  191. if ($options instanceof Zend_Config) {
  192. $options = $options->toArray();
  193. } elseif (!is_array($options)) {
  194. #require_once 'Zend/Cache/Exception.php';
  195. throw new Zend_Cache_Exception('Options passed must be in'
  196. . ' an associative array or instance of Zend_Config');
  197. }
  198. $this->_optionTemplates[$name] = $options;
  199. return $this;
  200. }
  201. /**
  202. * Check if the named configuration template
  203. *
  204. * @param string $name
  205. * @return bool
  206. */
  207. public function hasCacheTemplate($name)
  208. {
  209. if (isset($this->_optionTemplates[$name])) {
  210. return true;
  211. }
  212. return false;
  213. }
  214. /**
  215. * Get the named configuration template
  216. *
  217. * @param string $name
  218. * @return array
  219. */
  220. public function getCacheTemplate($name)
  221. {
  222. if (isset($this->_optionTemplates[$name])) {
  223. return $this->_optionTemplates[$name];
  224. }
  225. }
  226. /**
  227. * Pass an array containing changes to be applied to a named
  228. * configuration
  229. * template
  230. *
  231. * @param string $name
  232. * @param array $options
  233. * @return Zend_Cache_Manager
  234. * @throws Zend_Cache_Exception for invalid options format or if option templates do not have $name
  235. */
  236. public function setTemplateOptions($name, $options)
  237. {
  238. if ($options instanceof Zend_Config) {
  239. $options = $options->toArray();
  240. } elseif (!is_array($options)) {
  241. #require_once 'Zend/Cache/Exception.php';
  242. throw new Zend_Cache_Exception('Options passed must be in'
  243. . ' an associative array or instance of Zend_Config');
  244. }
  245. if (!isset($this->_optionTemplates[$name])) {
  246. throw new Zend_Cache_Exception('A cache configuration template'
  247. . 'does not exist with the name "' . $name . '"');
  248. }
  249. $this->_optionTemplates[$name]
  250. = $this->_mergeOptions($this->_optionTemplates[$name], $options);
  251. return $this;
  252. }
  253. /**
  254. * Simple method to merge two configuration arrays
  255. *
  256. * @param array $current
  257. * @param array $options
  258. * @return array
  259. */
  260. protected function _mergeOptions(array $current, array $options)
  261. {
  262. if (isset($options['frontend']['name'])) {
  263. $current['frontend']['name'] = $options['frontend']['name'];
  264. }
  265. if (isset($options['backend']['name'])) {
  266. $current['backend']['name'] = $options['backend']['name'];
  267. }
  268. if (isset($options['frontend']['options'])) {
  269. foreach ($options['frontend']['options'] as $key => $value) {
  270. $current['frontend']['options'][$key] = $value;
  271. }
  272. }
  273. if (isset($options['backend']['options'])) {
  274. foreach ($options['backend']['options'] as $key => $value) {
  275. $current['backend']['options'][$key] = $value;
  276. }
  277. }
  278. if (isset($options['frontend']['customFrontendNaming'])) {
  279. $current['frontend']['customFrontendNaming'] = $options['frontend']['customFrontendNaming'];
  280. }
  281. if (isset($options['backend']['customBackendNaming'])) {
  282. $current['backend']['customBackendNaming'] = $options['backend']['customBackendNaming'];
  283. }
  284. if (isset($options['frontendBackendAutoload'])) {
  285. $current['frontendBackendAutoload'] = $options['frontendBackendAutoload'];
  286. }
  287. return $current;
  288. }
  289. }