/zf/library/Zend/Cache/Manager.php
PHP | 298 lines | 163 code | 19 blank | 116 comment | 18 complexity | 4cd2813db347d87d47403341b27ba1b6 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
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-2011 Zend Technologies USA Inc. (http://www.zend.com) 18 * @license http://framework.zend.com/license/new-bsd New BSD License 19 * @version $Id: Manager.php 23775 2011-03-01 17:25:24Z ralph $ 20 */ 21 22/** @see Zend_Cache_Exception */ 23require_once 'Zend/Cache/Exception.php'; 24 25/** @see Zend_Cache */ 26require_once 'Zend/Cache.php'; 27 28/** 29 * @category Zend 30 * @package Zend_Cache 31 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) 32 * @license http://framework.zend.com/license/new-bsd New BSD License 33 */ 34class Zend_Cache_Manager 35{ 36 /** 37 * Constant holding reserved name for default Page Cache 38 */ 39 const PAGECACHE = 'page'; 40 41 /** 42 * Constant holding reserved name for default Page Tag Cache 43 */ 44 const PAGETAGCACHE = 'pagetag'; 45 46 /** 47 * Array of caches stored by the Cache Manager instance 48 * 49 * @var array 50 */ 51 protected $_caches = array(); 52 53 /** 54 * Array of ready made configuration templates for lazy 55 * loading caches. 56 * 57 * @var array 58 */ 59 protected $_optionTemplates = array( 60 // Simple Common Default 61 'default' => array( 62 'frontend' => array( 63 'name' => 'Core', 64 'options' => array( 65 'automatic_serialization' => true, 66 ), 67 ), 68 'backend' => array( 69 'name' => 'File', 70 'options' => array( 71 // use system temp dir by default of file backend 72 // 'cache_dir' => '../cache', 73 ), 74 ), 75 ), 76 77 // Static Page HTML Cache 78 'page' => array( 79 'frontend' => array( 80 'name' => 'Capture', 81 'options' => array( 82 'ignore_user_abort' => true, 83 ), 84 ), 85 'backend' => array( 86 'name' => 'Static', 87 'options' => array( 88 'public_dir' => '../public', 89 ), 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 // use system temp dir by default of file backend 106 // 'cache_dir' => '../cache', 107 // use default umask of file backend 108 // 'cache_file_umask' => 0644 109 ), 110 ), 111 ), 112 ); 113 114 /** 115 * Set a new cache for the Cache Manager to contain 116 * 117 * @param string $name 118 * @param Zend_Cache_Core $cache 119 * @return Zend_Cache_Manager 120 */ 121 public function setCache($name, Zend_Cache_Core $cache) 122 { 123 $this->_caches[$name] = $cache; 124 return $this; 125 } 126 127 /** 128 * Check if the Cache Manager contains the named cache object, or a named 129 * configuration template to lazy load the cache object 130 * 131 * @param string $name 132 * @return bool 133 */ 134 public function hasCache($name) 135 { 136 if (isset($this->_caches[$name]) 137 || $this->hasCacheTemplate($name) 138 ) { 139 return true; 140 } 141 return false; 142 } 143 144 /** 145 * Fetch the named cache object, or instantiate and return a cache object 146 * using a named configuration template 147 * 148 * @param string $name 149 * @return Zend_Cache_Core 150 */ 151 public function getCache($name) 152 { 153 if (isset($this->_caches[$name])) { 154 return $this->_caches[$name]; 155 } 156 if (isset($this->_optionTemplates[$name])) { 157 if ($name == self::PAGECACHE 158 && (!isset($this->_optionTemplates[$name]['backend']['options']['tag_cache']) 159 || !$this->_optionTemplates[$name]['backend']['options']['tag_cache'] instanceof Zend_Cache_Core) 160 ) { 161 $this->_optionTemplates[$name]['backend']['options']['tag_cache'] 162 = $this->getCache(self::PAGETAGCACHE); 163 } 164 165 $this->_caches[$name] = Zend_Cache::factory( 166 $this->_optionTemplates[$name]['frontend']['name'], 167 $this->_optionTemplates[$name]['backend']['name'], 168 isset($this->_optionTemplates[$name]['frontend']['options']) ? $this->_optionTemplates[$name]['frontend']['options'] : array(), 169 isset($this->_optionTemplates[$name]['backend']['options']) ? $this->_optionTemplates[$name]['backend']['options'] : array(), 170 isset($this->_optionTemplates[$name]['frontend']['customFrontendNaming']) ? $this->_optionTemplates[$name]['frontend']['customFrontendNaming'] : false, 171 isset($this->_optionTemplates[$name]['backend']['customBackendNaming']) ? $this->_optionTemplates[$name]['backend']['customBackendNaming'] : false, 172 isset($this->_optionTemplates[$name]['frontendBackendAutoload']) ? $this->_optionTemplates[$name]['frontendBackendAutoload'] : false 173 ); 174 175 return $this->_caches[$name]; 176 } 177 } 178 179 /** 180 * Fetch all available caches 181 * 182 * @return array An array of all available caches with it's names as key 183 */ 184 public function getCaches() 185 { 186 $caches = $this->_caches; 187 foreach ($this->_optionTemplates as $name => $tmp) { 188 if (!isset($caches[$name])) { 189 $caches[$name] = $this->getCache($name); 190 } 191 } 192 return $caches; 193 } 194 195 /** 196 * Set a named configuration template from which a cache object can later 197 * be lazy loaded 198 * 199 * @param string $name 200 * @param array $options 201 * @return Zend_Cache_Manager 202 */ 203 public function setCacheTemplate($name, $options) 204 { 205 if ($options instanceof Zend_Config) { 206 $options = $options->toArray(); 207 } elseif (!is_array($options)) { 208 require_once 'Zend/Cache/Exception.php'; 209 throw new Zend_Cache_Exception('Options passed must be in' 210 . ' an associative array or instance of Zend_Config'); 211 } 212 $this->_optionTemplates[$name] = $options; 213 return $this; 214 } 215 216 /** 217 * Check if the named configuration template 218 * 219 * @param string $name 220 * @return bool 221 */ 222 public function hasCacheTemplate($name) 223 { 224 if (isset($this->_optionTemplates[$name])) { 225 return true; 226 } 227 return false; 228 } 229 230 /** 231 * Get the named configuration template 232 * 233 * @param string $name 234 * @return array 235 */ 236 public function getCacheTemplate($name) 237 { 238 if (isset($this->_optionTemplates[$name])) { 239 return $this->_optionTemplates[$name]; 240 } 241 } 242 243 /** 244 * Pass an array containing changes to be applied to a named 245 * configuration 246 * template 247 * 248 * @param string $name 249 * @param array $options 250 * @return Zend_Cache_Manager 251 * @throws Zend_Cache_Exception for invalid options format or if option templates do not have $name 252 */ 253 public function setTemplateOptions($name, $options) 254 { 255 if ($options instanceof Zend_Config) { 256 $options = $options->toArray(); 257 } elseif (!is_array($options)) { 258 require_once 'Zend/Cache/Exception.php'; 259 throw new Zend_Cache_Exception('Options passed must be in' 260 . ' an associative array or instance of Zend_Config'); 261 } 262 if (!isset($this->_optionTemplates[$name])) { 263 throw new Zend_Cache_Exception('A cache configuration template' 264 . 'does not exist with the name "' . $name . '"'); 265 } 266 $this->_optionTemplates[$name] 267 = $this->_mergeOptions($this->_optionTemplates[$name], $options); 268 return $this; 269 } 270 271 /** 272 * Simple method to merge two configuration arrays 273 * 274 * @param array $current 275 * @param array $options 276 * @return array 277 */ 278 protected function _mergeOptions(array $current, array $options) 279 { 280 if (isset($options['frontend']['name'])) { 281 $current['frontend']['name'] = $options['frontend']['name']; 282 } 283 if (isset($options['backend']['name'])) { 284 $current['backend']['name'] = $options['backend']['name']; 285 } 286 if (isset($options['frontend']['options'])) { 287 foreach ($options['frontend']['options'] as $key=>$value) { 288 $current['frontend']['options'][$key] = $value; 289 } 290 } 291 if (isset($options['backend']['options'])) { 292 foreach ($options['backend']['options'] as $key=>$value) { 293 $current['backend']['options'][$key] = $value; 294 } 295 } 296 return $current; 297 } 298}