File indexing completed on 2024-05-12 06:02:19

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Cache
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /** @see Zend_Cache_Exception */
0023 // require_once 'Zend/Cache/Exception.php';
0024 
0025 /** @see Zend_Cache */
0026 // require_once 'Zend/Cache.php';
0027 
0028 /**
0029  * @category   Zend
0030  * @package    Zend_Cache
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 class Zend_Cache_Manager
0035 {
0036     /**
0037      * Constant holding reserved name for default Page Cache
0038      */
0039     const PAGECACHE = 'page';
0040 
0041     /**
0042      * Constant holding reserved name for default Page Tag Cache
0043      */
0044     const PAGETAGCACHE = 'pagetag';
0045 
0046     /**
0047      * Array of caches stored by the Cache Manager instance
0048      *
0049      * @var array
0050      */
0051     protected $_caches = array();
0052 
0053     /**
0054      * Array of ready made configuration templates for lazy
0055      * loading caches.
0056      *
0057      * @var array
0058      */
0059     protected $_optionTemplates = array(
0060         // Simple Common Default
0061         'default' => array(
0062             'frontend' => array(
0063                 'name'    => 'Core',
0064                 'options' => array(
0065                     'automatic_serialization' => true,
0066                 ),
0067             ),
0068             'backend' => array(
0069                 'name'    => 'File',
0070                 'options' => array(
0071                     // use system temp dir by default of file backend
0072                     // 'cache_dir' => '../cache',
0073                 ),
0074             ),
0075         ),
0076 
0077         // Static Page HTML Cache
0078         'page' => array(
0079             'frontend' => array(
0080                 'name'    => 'Capture',
0081                 'options' => array(
0082                     'ignore_user_abort' => true,
0083                 ),
0084             ),
0085             'backend' => array(
0086                 'name'    => 'Static',
0087                 'options' => array(
0088                     'public_dir' => '../public',
0089                 ),
0090             ),
0091         ),
0092 
0093         // Tag Cache
0094         'pagetag' => array(
0095             'frontend' => array(
0096                 'name'    => 'Core',
0097                 'options' => array(
0098                     'automatic_serialization' => true,
0099                     'lifetime' => null
0100                 ),
0101             ),
0102             'backend' => array(
0103                 'name'    => 'File',
0104                 'options' => array(
0105                     // use system temp dir by default of file backend
0106                     // 'cache_dir' => '../cache',
0107                     // use default umask of file backend
0108                     // 'cache_file_umask' => 0644
0109                 ),
0110             ),
0111         ),
0112     );
0113 
0114     /**
0115      * Set a new cache for the Cache Manager to contain
0116      *
0117      * @param  string $name
0118      * @param  Zend_Cache_Core $cache
0119      * @return Zend_Cache_Manager
0120      */
0121     public function setCache($name, Zend_Cache_Core $cache)
0122     {
0123         $this->_caches[$name] = $cache;
0124         return $this;
0125     }
0126 
0127     /**
0128      * Check if the Cache Manager contains the named cache object, or a named
0129      * configuration template to lazy load the cache object
0130      *
0131      * @param string $name
0132      * @return bool
0133      */
0134     public function hasCache($name)
0135     {
0136         if (isset($this->_caches[$name])
0137             || $this->hasCacheTemplate($name)
0138         ) {
0139             return true;
0140         }
0141         return false;
0142     }
0143 
0144     /**
0145      * Fetch the named cache object, or instantiate and return a cache object
0146      * using a named configuration template
0147      *
0148      * @param  string $name
0149      * @return Zend_Cache_Core
0150      */
0151     public function getCache($name)
0152     {
0153         if (isset($this->_caches[$name])) {
0154             return $this->_caches[$name];
0155         }
0156         if (isset($this->_optionTemplates[$name])) {
0157             if ($name == self::PAGECACHE
0158                 && (!isset($this->_optionTemplates[$name]['backend']['options']['tag_cache'])
0159                 || !$this->_optionTemplates[$name]['backend']['options']['tag_cache'] instanceof Zend_Cache_Core)
0160             ) {
0161                 $this->_optionTemplates[$name]['backend']['options']['tag_cache']
0162                     = $this->getCache(self::PAGETAGCACHE);
0163             }
0164 
0165             $this->_caches[$name] = Zend_Cache::factory(
0166                 $this->_optionTemplates[$name]['frontend']['name'],
0167                 $this->_optionTemplates[$name]['backend']['name'],
0168                 isset($this->_optionTemplates[$name]['frontend']['options']) ? $this->_optionTemplates[$name]['frontend']['options'] : array(),
0169                 isset($this->_optionTemplates[$name]['backend']['options']) ? $this->_optionTemplates[$name]['backend']['options'] : array(),
0170                 isset($this->_optionTemplates[$name]['frontend']['customFrontendNaming']) ? $this->_optionTemplates[$name]['frontend']['customFrontendNaming'] : false,
0171                 isset($this->_optionTemplates[$name]['backend']['customBackendNaming']) ? $this->_optionTemplates[$name]['backend']['customBackendNaming'] : false,
0172                 isset($this->_optionTemplates[$name]['frontendBackendAutoload']) ? $this->_optionTemplates[$name]['frontendBackendAutoload'] : false
0173             );
0174 
0175             return $this->_caches[$name];
0176         }
0177     }
0178 
0179     /**
0180      * Fetch all available caches
0181      *
0182      * @return array An array of all available caches with it's names as key
0183      */
0184     public function getCaches()
0185     {
0186         $caches = $this->_caches;
0187         foreach ($this->_optionTemplates as $name => $tmp) {
0188             if (!isset($caches[$name])) {
0189                 $caches[$name] = $this->getCache($name);
0190             }
0191         }
0192         return $caches;
0193     }
0194 
0195     /**
0196      * Set a named configuration template from which a cache object can later
0197      * be lazy loaded
0198      *
0199      * @param  string $name
0200      * @param  array  $options
0201      * @return Zend_Cache_Manager
0202      * @throws Zend_Cache_Exception
0203      */
0204     public function setCacheTemplate($name, $options)
0205     {
0206         if ($options instanceof Zend_Config) {
0207             $options = $options->toArray();
0208         } elseif (!is_array($options)) {
0209             // require_once 'Zend/Cache/Exception.php';
0210             throw new Zend_Cache_Exception('Options passed must be in'
0211                 . ' an associative array or instance of Zend_Config');
0212         }
0213         $this->_optionTemplates[$name] = $options;
0214         return $this;
0215     }
0216 
0217     /**
0218      * Check if the named configuration template
0219      *
0220      * @param  string $name
0221      * @return bool
0222      */
0223     public function hasCacheTemplate($name)
0224     {
0225         if (isset($this->_optionTemplates[$name])) {
0226             return true;
0227         }
0228         return false;
0229     }
0230 
0231     /**
0232      * Get the named configuration template
0233      *
0234      * @param  string $name
0235      * @return array
0236      */
0237     public function getCacheTemplate($name)
0238     {
0239         if (isset($this->_optionTemplates[$name])) {
0240             return $this->_optionTemplates[$name];
0241         }
0242     }
0243 
0244     /**
0245      * Pass an array containing changes to be applied to a named
0246      * configuration
0247      * template
0248      *
0249      * @param  string $name
0250      * @param  array $options
0251      * @return Zend_Cache_Manager
0252      * @throws Zend_Cache_Exception for invalid options format or if option templates do not have $name
0253      */
0254     public function setTemplateOptions($name, $options)
0255     {
0256         if ($options instanceof Zend_Config) {
0257             $options = $options->toArray();
0258         } elseif (!is_array($options)) {
0259             // require_once 'Zend/Cache/Exception.php';
0260             throw new Zend_Cache_Exception('Options passed must be in'
0261                 . ' an associative array or instance of Zend_Config');
0262         }
0263         if (!isset($this->_optionTemplates[$name])) {
0264             throw new Zend_Cache_Exception('A cache configuration template'
0265                 . 'does not exist with the name "' . $name . '"');
0266         }
0267         $this->_optionTemplates[$name]
0268             = $this->_mergeOptions($this->_optionTemplates[$name], $options);
0269         return $this;
0270     }
0271 
0272     /**
0273      * Simple method to merge two configuration arrays
0274      *
0275      * @param  array $current
0276      * @param  array $options
0277      * @return array
0278      */
0279     protected function _mergeOptions(array $current, array $options)
0280     {
0281         if (isset($options['frontend']['name'])) {
0282             $current['frontend']['name'] = $options['frontend']['name'];
0283         }
0284         if (isset($options['backend']['name'])) {
0285             $current['backend']['name'] = $options['backend']['name'];
0286         }
0287         if (isset($options['frontend']['options'])) {
0288             foreach ($options['frontend']['options'] as $key => $value) {
0289                 $current['frontend']['options'][$key] = $value;
0290             }
0291         }
0292         if (isset($options['backend']['options'])) {
0293             foreach ($options['backend']['options'] as $key => $value) {
0294                 $current['backend']['options'][$key] = $value;
0295             }
0296         }
0297         if (isset($options['frontend']['customFrontendNaming'])) {
0298             $current['frontend']['customFrontendNaming'] = $options['frontend']['customFrontendNaming'];
0299         }
0300         if (isset($options['backend']['customBackendNaming'])) {
0301             $current['backend']['customBackendNaming'] = $options['backend']['customBackendNaming'];
0302         }
0303         if (isset($options['frontendBackendAutoload'])) {
0304             $current['frontendBackendAutoload'] = $options['frontendBackendAutoload'];
0305         }
0306         return $current;
0307     }
0308 }