[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Cache / Manager.php
blob8bde702e6d71829361d59758281503b1ddac8feb
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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$
22 /** @see Zend_Cache_Exception */
23 require_once 'Zend/Cache/Exception.php';
25 /** @see Zend_Cache */
26 require_once 'Zend/Cache.php';
28 /**
29 * @category Zend
30 * @package Zend_Cache
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
34 class Zend_Cache_Manager
36 /**
37 * Constant holding reserved name for default Page Cache
39 const PAGECACHE = 'page';
41 /**
42 * Constant holding reserved name for default Page Tag Cache
44 const PAGETAGCACHE = 'pagetag';
46 /**
47 * Array of caches stored by the Cache Manager instance
49 * @var array
51 protected $_caches = array();
53 /**
54 * Array of ready made configuration templates for lazy
55 * loading caches.
57 * @var array
59 protected $_optionTemplates = array(
60 // Null Cache (Enforce Null/Empty Values)
61 'skeleton' => array(
62 'frontend' => array(
63 'name' => null,
64 'options' => array(),
66 'backend' => array(
67 'name' => null,
68 'options' => array(),
71 // Simple Common Default
72 'default' => array(
73 'frontend' => array(
74 'name' => 'Core',
75 'options' => array(
76 'automatic_serialization' => true,
79 'backend' => array(
80 'name' => 'File',
81 'options' => array(
82 'cache_dir' => '../cache',
86 // Static Page HTML Cache
87 'page' => array(
88 'frontend' => array(
89 'name' => 'Capture',
90 'options' => array(
91 'ignore_user_abort' => true,
94 'backend' => array(
95 'name' => 'Static',
96 'options' => array(
97 'public_dir' => '../public',
101 // Tag Cache
102 'pagetag' => array(
103 'frontend' => array(
104 'name' => 'Core',
105 'options' => array(
106 'automatic_serialization' => true,
107 'lifetime' => null
110 'backend' => array(
111 'name' => 'File',
112 'options' => array(
113 'cache_dir' => '../cache',
114 'cache_file_umask' => 0644
121 * Set a new cache for the Cache Manager to contain
123 * @param string $name
124 * @param Zend_Cache_Core $cache
125 * @return Zend_Cache_Manager
127 public function setCache($name, Zend_Cache_Core $cache)
129 $this->_caches[$name] = $cache;
130 return $this;
134 * Check if the Cache Manager contains the named cache object, or a named
135 * configuration template to lazy load the cache object
137 * @param string $name
138 * @return bool
140 public function hasCache($name)
142 if (isset($this->_caches[$name])
143 || $this->hasCacheTemplate($name)
145 return true;
147 return false;
151 * Fetch the named cache object, or instantiate and return a cache object
152 * using a named configuration template
154 * @param string $name
155 * @return Zend_Cache_Core
157 public function getCache($name)
159 if (isset($this->_caches[$name])) {
160 return $this->_caches[$name];
162 if (isset($this->_optionTemplates[$name])) {
163 if ($name == self::PAGECACHE
164 && (!isset($this->_optionTemplates[$name]['backend']['options']['tag_cache'])
165 || !$this->_optionTemplates[$name]['backend']['options']['tag_cache'] instanceof Zend_Cache_Core)
167 $this->_optionTemplates[$name]['backend']['options']['tag_cache']
168 = $this->getCache(self::PAGETAGCACHE);
170 $this->_caches[$name] = Zend_Cache::factory(
171 $this->_optionTemplates[$name]['frontend']['name'],
172 $this->_optionTemplates[$name]['backend']['name'],
173 isset($this->_optionTemplates[$name]['frontend']['options']) ? $this->_optionTemplates[$name]['frontend']['options'] : array(),
174 isset($this->_optionTemplates[$name]['backend']['options']) ? $this->_optionTemplates[$name]['backend']['options'] : array(),
175 isset($this->_optionTemplates[$name]['frontend']['customFrontendNaming']) ? $this->_optionTemplates[$name]['frontend']['customFrontendNaming'] : false,
176 isset($this->_optionTemplates[$name]['backend']['customBackendNaming']) ? $this->_optionTemplates[$name]['backend']['customBackendNaming'] : false,
177 isset($this->_optionTemplates[$name]['frontendBackendAutoload']) ? $this->_optionTemplates[$name]['frontendBackendAutoload'] : false
179 return $this->_caches[$name];
184 * Set a named configuration template from which a cache object can later
185 * be lazy loaded
187 * @param string $name
188 * @param array $options
189 * @return Zend_Cache_Manager
191 public function setCacheTemplate($name, $options)
193 if ($options instanceof Zend_Config) {
194 $options = $options->toArray();
195 } elseif (!is_array($options)) {
196 require_once 'Zend/Cache/Exception.php';
197 throw new Zend_Cache_Exception('Options passed must be in'
198 . ' an associative array or instance of Zend_Config');
200 $this->_optionTemplates[$name] = $options;
201 return $this;
205 * Check if the named configuration template
207 * @param string $name
208 * @return bool
210 public function hasCacheTemplate($name)
212 if (isset($this->_optionTemplates[$name])) {
213 return true;
215 return false;
219 * Get the named configuration template
221 * @param string $name
222 * @return array
224 public function getCacheTemplate($name)
226 if (isset($this->_optionTemplates[$name])) {
227 return $this->_optionTemplates[$name];
232 * Pass an array containing changes to be applied to a named
233 * configuration
234 * template
236 * @param string $name
237 * @param array $options
238 * @return Zend_Cache_Manager
239 * @throws Zend_Cache_Exception for invalid options format or if option templates do not have $name
241 public function setTemplateOptions($name, $options)
243 if ($options instanceof Zend_Config) {
244 $options = $options->toArray();
245 } elseif (!is_array($options)) {
246 require_once 'Zend/Cache/Exception.php';
247 throw new Zend_Cache_Exception('Options passed must be in'
248 . ' an associative array or instance of Zend_Config');
250 if (!isset($this->_optionTemplates[$name])) {
251 throw new Zend_Cache_Exception('A cache configuration template'
252 . 'does not exist with the name "' . $name . '"');
254 $this->_optionTemplates[$name]
255 = $this->_mergeOptions($this->_optionTemplates[$name], $options);
256 return $this;
260 * Simple method to merge two configuration arrays
262 * @param array $current
263 * @param array $options
264 * @return array
266 protected function _mergeOptions(array $current, array $options)
268 if (isset($options['frontend']['name'])) {
269 $current['frontend']['name'] = $options['frontend']['name'];
271 if (isset($options['backend']['name'])) {
272 $current['backend']['name'] = $options['backend']['name'];
274 if (isset($options['frontend']['options'])) {
275 foreach ($options['frontend']['options'] as $key=>$value) {
276 $current['frontend']['options'][$key] = $value;
279 if (isset($options['backend']['options'])) {
280 foreach ($options['backend']['options'] as $key=>$value) {
281 $current['backend']['options'][$key] = $value;
284 return $current;