00001 <?php
00026 require_once 'Zend/Cache/Core.php';
00027
00028
00035 class Zend_Cache_Frontend_Class extends Zend_Cache_Core
00036 {
00055 protected $_specificOptions = array(
00056 'cached_entity' => null,
00057 'cache_by_default' => true,
00058 'cached_methods' => array(),
00059 'non_cached_methods' => array()
00060 );
00061
00067 private $_tags = array();
00068
00076 private $_specificLifetime = false;
00077
00083 private $_cachedEntity = null;
00084
00092 private $_cachedEntityLabel = '';
00093
00099 private $_priority = 8;
00100
00108 public function __construct(array $options = array())
00109 {
00110 while (list($name, $value) = each($options)) {
00111 $this->setOption($name, $value);
00112 }
00113 if ($this->_specificOptions['cached_entity'] === null) {
00114 Zend_Cache::throwException('cached_entity must be set !');
00115 }
00116 $this->setCachedEntity($this->_specificOptions['cached_entity']);
00117 $this->setOption('automatic_serialization', true);
00118 }
00119
00126 public function setSpecificLifetime($specificLifetime = false)
00127 {
00128 $this->_specificLifetime = $specificLifetime;
00129 }
00130
00136 public function setPriority($priority)
00137 {
00138 $this->_priority = $priority;
00139 }
00140
00151 public function setOption($name, $value)
00152 {
00153 if ($name == 'cached_entity') {
00154 $this->setCachedEntity($value);
00155 } else {
00156 parent::setOption($name, $value);
00157 }
00158 }
00159
00168 public function setCachedEntity($cachedEntity)
00169 {
00170 if (!is_string($cachedEntity) && !is_object($cachedEntity)) {
00171 Zend_Cache::throwException('cached_entity must be an object or a class name');
00172 }
00173 $this->_cachedEntity = $cachedEntity;
00174 $this->_specificOptions['cached_entity'] = $cachedEntity;
00175 if (is_string($this->_cachedEntity)){
00176 $this->_cachedEntityLabel = $this->_cachedEntity;
00177 } else {
00178 $ro = new ReflectionObject($this->_cachedEntity);
00179 $this->_cachedEntityLabel = $ro->getName();
00180 }
00181 }
00182
00189 public function setTagsArray($tags = array())
00190 {
00191 $this->_tags = $tags;
00192 }
00193
00201 public function __call($name, $parameters)
00202 {
00203 $cacheBool1 = $this->_specificOptions['cache_by_default'];
00204 $cacheBool2 = in_array($name, $this->_specificOptions['cached_methods']);
00205 $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_methods']);
00206 $cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
00207 if (!$cache) {
00208
00209 return call_user_func_array(array($this->_cachedEntity, $name), $parameters);
00210 }
00211 $id = $this->_makeId($name, $parameters);
00212 if ($this->test($id)) {
00213
00214 $result = $this->load($id);
00215 $output = $result[0];
00216 $return = $result[1];
00217 } else {
00218
00219 ob_start();
00220 ob_implicit_flush(false);
00221 $return = call_user_func_array(array($this->_cachedEntity, $name), $parameters);
00222 $output = ob_get_contents();
00223 ob_end_clean();
00224 $data = array($output, $return);
00225 $this->save($data, $id, $this->_tags, $this->_specificLifetime, $this->_priority);
00226 }
00227 echo $output;
00228 return $return;
00229 }
00230
00238 private function _makeId($name, $parameters)
00239 {
00240 return md5($this->_cachedEntityLabel . '__' . $name . '__' . serialize($parameters));
00241 }
00242
00243 }