*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Cache / Frontend / Class.php
blob6c4ef471b863917ddb0103fbb1f05563a5994078
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 * @subpackage Zend_Cache_Frontend
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Class.php 16541 2009-07-07 06:59:03Z bkarwin $
23 /**
24 * @see Zend_Cache_Core
26 require_once 'Zend/Cache/Core.php';
29 /**
30 * @package Zend_Cache
31 * @subpackage Zend_Cache_Frontend
32 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
33 * @license http://framework.zend.com/license/new-bsd New BSD License
35 class Zend_Cache_Frontend_Class extends Zend_Cache_Core
37 /**
38 * Available options
40 * ====> (mixed) cached_entity :
41 * - if set to a class name, we will cache an abstract class and will use only static calls
42 * - if set to an object, we will cache this object methods
44 * ====> (boolean) cache_by_default :
45 * - if true, method calls will be cached by default
47 * ====> (array) cached_methods :
48 * - an array of method names which will be cached (even if cache_by_default = false)
50 * ====> (array) non_cached_methods :
51 * - an array of method names which won't be cached (even if cache_by_default = true)
53 * @var array available options
55 protected $_specificOptions = array(
56 'cached_entity' => null,
57 'cache_by_default' => true,
58 'cached_methods' => array(),
59 'non_cached_methods' => array()
62 /**
63 * Tags array
65 * @var array
67 private $_tags = array();
69 /**
70 * SpecificLifetime value
72 * false => no specific life time
74 * @var int
76 private $_specificLifetime = false;
78 /**
79 * The cached object or the name of the cached abstract class
81 * @var mixed
83 private $_cachedEntity = null;
85 /**
86 * The class name of the cached object or cached abstract class
88 * Used to differentiate between different classes with the same method calls.
90 * @var string
92 private $_cachedEntityLabel = '';
94 /**
95 * Priority (used by some particular backends)
97 * @var int
99 private $_priority = 8;
102 * Constructor
104 * @param array $options Associative array of options
105 * @throws Zend_Cache_Exception
106 * @return void
108 public function __construct(array $options = array())
110 while (list($name, $value) = each($options)) {
111 $this->setOption($name, $value);
113 if ($this->_specificOptions['cached_entity'] === null) {
114 Zend_Cache::throwException('cached_entity must be set !');
116 $this->setCachedEntity($this->_specificOptions['cached_entity']);
117 $this->setOption('automatic_serialization', true);
121 * Set a specific life time
123 * @param int $specificLifetime
124 * @return void
126 public function setSpecificLifetime($specificLifetime = false)
128 $this->_specificLifetime = $specificLifetime;
132 * Set the priority (used by some particular backends)
134 * @param int $priority integer between 0 (very low priority) and 10 (maximum priority)
136 public function setPriority($priority)
138 $this->_priority = $priority;
142 * Public frontend to set an option
144 * Just a wrapper to get a specific behaviour for cached_entity
146 * @param string $name Name of the option
147 * @param mixed $value Value of the option
148 * @throws Zend_Cache_Exception
149 * @return void
151 public function setOption($name, $value)
153 if ($name == 'cached_entity') {
154 $this->setCachedEntity($value);
155 } else {
156 parent::setOption($name, $value);
161 * Specific method to set the cachedEntity
163 * if set to a class name, we will cache an abstract class and will use only static calls
164 * if set to an object, we will cache this object methods
166 * @param mixed $cachedEntity
168 public function setCachedEntity($cachedEntity)
170 if (!is_string($cachedEntity) && !is_object($cachedEntity)) {
171 Zend_Cache::throwException('cached_entity must be an object or a class name');
173 $this->_cachedEntity = $cachedEntity;
174 $this->_specificOptions['cached_entity'] = $cachedEntity;
175 if (is_string($this->_cachedEntity)){
176 $this->_cachedEntityLabel = $this->_cachedEntity;
177 } else {
178 $ro = new ReflectionObject($this->_cachedEntity);
179 $this->_cachedEntityLabel = $ro->getName();
184 * Set the cache array
186 * @param array $tags
187 * @return void
189 public function setTagsArray($tags = array())
191 $this->_tags = $tags;
195 * Main method : call the specified method or get the result from cache
197 * @param string $name Method name
198 * @param array $parameters Method parameters
199 * @return mixed Result
201 public function __call($name, $parameters)
203 $cacheBool1 = $this->_specificOptions['cache_by_default'];
204 $cacheBool2 = in_array($name, $this->_specificOptions['cached_methods']);
205 $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_methods']);
206 $cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
207 if (!$cache) {
208 // We do not have not cache
209 return call_user_func_array(array($this->_cachedEntity, $name), $parameters);
211 $id = $this->_makeId($name, $parameters);
212 if ($this->test($id)) {
213 // A cache is available
214 $result = $this->load($id);
215 $output = $result[0];
216 $return = $result[1];
217 } else {
218 // A cache is not available
219 ob_start();
220 ob_implicit_flush(false);
221 $return = call_user_func_array(array($this->_cachedEntity, $name), $parameters);
222 $output = ob_get_contents();
223 ob_end_clean();
224 $data = array($output, $return);
225 $this->save($data, $id, $this->_tags, $this->_specificLifetime, $this->_priority);
227 echo $output;
228 return $return;
232 * Make a cache id from the method name and parameters
234 * @param string $name Method name
235 * @param array $parameters Method parameters
236 * @return string Cache id
238 private function _makeId($name, $parameters)
240 return md5($this->_cachedEntityLabel . '__' . $name . '__' . serialize($parameters));