/framework/vendor/zend/Zend/Cache/Backend/Test.php
PHP | 410 lines | 157 code | 32 blank | 221 comment | 23 complexity | 0c669454704c3591c67a2ba80030f711 MD5 | raw file
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 * @subpackage Zend_Cache_Backend 18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) 19 * @license http://framework.zend.com/license/new-bsd New BSD License 20 * @version $Id: Test.php 21292 2010-03-02 10:25:22Z mabe $ 21 */ 22 23 24/** 25 * @see Zend_Cache_Backend_Interface 26 */ 27require_once 'Zend/Cache/Backend/ExtendedInterface.php'; 28 29/** 30 * @see Zend_Cache_Backend 31 */ 32require_once 'Zend/Cache/Backend.php'; 33 34/** 35 * @package Zend_Cache 36 * @subpackage Zend_Cache_Backend 37 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) 38 * @license http://framework.zend.com/license/new-bsd New BSD License 39 */ 40class Zend_Cache_Backend_Test extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface 41{ 42 /** 43 * Available options 44 * 45 * @var array available options 46 */ 47 protected $_options = array(); 48 49 /** 50 * Frontend or Core directives 51 * 52 * @var array directives 53 */ 54 protected $_directives = array(); 55 56 /** 57 * Array to log actions 58 * 59 * @var array $_log 60 */ 61 private $_log = array(); 62 63 /** 64 * Current index for log array 65 * 66 * @var int $_index 67 */ 68 private $_index = 0; 69 70 /** 71 * Constructor 72 * 73 * @param array $options associative array of options 74 * @return void 75 */ 76 public function __construct($options = array()) 77 { 78 $this->_addLog('construct', array($options)); 79 } 80 81 /** 82 * Set the frontend directives 83 * 84 * @param array $directives assoc of directives 85 * @return void 86 */ 87 public function setDirectives($directives) 88 { 89 $this->_addLog('setDirectives', array($directives)); 90 } 91 92 /** 93 * Test if a cache is available for the given id and (if yes) return it (false else) 94 * 95 * For this test backend only, if $id == 'false', then the method will return false 96 * if $id == 'serialized', the method will return a serialized array 97 * ('foo' else) 98 * 99 * @param string $id Cache id 100 * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested 101 * @return string Cached datas (or false) 102 */ 103 public function load($id, $doNotTestCacheValidity = false) 104 { 105 $this->_addLog('get', array($id, $doNotTestCacheValidity)); 106 if ( $id == 'false' 107 || $id == 'd8523b3ee441006261eeffa5c3d3a0a7' 108 || $id == 'e83249ea22178277d5befc2c5e2e9ace' 109 || $id == '40f649b94977c0a6e76902e2a0b43587') 110 { 111 return false; 112 } 113 if ($id=='serialized') { 114 return serialize(array('foo')); 115 } 116 if ($id=='serialized2') { 117 return serialize(array('headers' => array(), 'data' => 'foo')); 118 } 119 if (($id=='71769f39054f75894288e397df04e445') or ($id=='615d222619fb20b527168340cebd0578')) { 120 return serialize(array('foo', 'bar')); 121 } 122 if (($id=='8a02d218a5165c467e7a5747cc6bd4b6') or ($id=='648aca1366211d17cbf48e65dc570bee')) { 123 return serialize(array('foo', 'bar')); 124 } 125 return 'foo'; 126 } 127 128 /** 129 * Test if a cache is available or not (for the given id) 130 * 131 * For this test backend only, if $id == 'false', then the method will return false 132 * (123456 else) 133 * 134 * @param string $id Cache id 135 * @return mixed|false false (a cache is not available) or "last modified" timestamp (int) of the available cache record 136 */ 137 public function test($id) 138 { 139 $this->_addLog('test', array($id)); 140 if ($id=='false') { 141 return false; 142 } 143 if (($id=='3c439c922209e2cb0b54d6deffccd75a')) { 144 return false; 145 } 146 return 123456; 147 } 148 149 /** 150 * Save some string datas into a cache record 151 * 152 * For this test backend only, if $id == 'false', then the method will return false 153 * (true else) 154 * 155 * @param string $data Datas to cache 156 * @param string $id Cache id 157 * @param array $tags Array of strings, the cache record will be tagged by each string entry 158 * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime) 159 * @return boolean True if no problem 160 */ 161 public function save($data, $id, $tags = array(), $specificLifetime = false) 162 { 163 $this->_addLog('save', array($data, $id, $tags)); 164 if ($id=='false') { 165 return false; 166 } 167 return true; 168 } 169 170 /** 171 * Remove a cache record 172 * 173 * For this test backend only, if $id == 'false', then the method will return false 174 * (true else) 175 * 176 * @param string $id Cache id 177 * @return boolean True if no problem 178 */ 179 public function remove($id) 180 { 181 $this->_addLog('remove', array($id)); 182 if ($id=='false') { 183 return false; 184 } 185 return true; 186 } 187 188 /** 189 * Clean some cache records 190 * 191 * For this test backend only, if $mode == 'false', then the method will return false 192 * (true else) 193 * 194 * Available modes are : 195 * Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used) 196 * Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used) 197 * Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags 198 * ($tags can be an array of strings or a single string) 199 * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags} 200 * ($tags can be an array of strings or a single string) 201 * 202 * @param string $mode Clean mode 203 * @param array $tags Array of tags 204 * @return boolean True if no problem 205 */ 206 public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array()) 207 { 208 $this->_addLog('clean', array($mode, $tags)); 209 if ($mode=='false') { 210 return false; 211 } 212 return true; 213 } 214 215 /** 216 * Get the last log 217 * 218 * @return string The last log 219 */ 220 public function getLastLog() 221 { 222 return $this->_log[$this->_index - 1]; 223 } 224 225 /** 226 * Get the log index 227 * 228 * @return int Log index 229 */ 230 public function getLogIndex() 231 { 232 return $this->_index; 233 } 234 235 /** 236 * Get the complete log array 237 * 238 * @return array Complete log array 239 */ 240 public function getAllLogs() 241 { 242 return $this->_log; 243 } 244 245 /** 246 * Return true if the automatic cleaning is available for the backend 247 * 248 * @return boolean 249 */ 250 public function isAutomaticCleaningAvailable() 251 { 252 return true; 253 } 254 255 /** 256 * Return an array of stored cache ids 257 * 258 * @return array array of stored cache ids (string) 259 */ 260 public function getIds() 261 { 262 return array( 263 'prefix_id1', 'prefix_id2' 264 ); 265 } 266 267 /** 268 * Return an array of stored tags 269 * 270 * @return array array of stored tags (string) 271 */ 272 public function getTags() 273 { 274 return array( 275 'tag1', 'tag2' 276 ); 277 } 278 279 /** 280 * Return an array of stored cache ids which match given tags 281 * 282 * In case of multiple tags, a logical AND is made between tags 283 * 284 * @param array $tags array of tags 285 * @return array array of matching cache ids (string) 286 */ 287 public function getIdsMatchingTags($tags = array()) 288 { 289 if ($tags == array('tag1', 'tag2')) { 290 return array('prefix_id1', 'prefix_id2'); 291 } 292 293 return array(); 294 } 295 296 /** 297 * Return an array of stored cache ids which don't match given tags 298 * 299 * In case of multiple tags, a logical OR is made between tags 300 * 301 * @param array $tags array of tags 302 * @return array array of not matching cache ids (string) 303 */ 304 public function getIdsNotMatchingTags($tags = array()) 305 { 306 if ($tags == array('tag3', 'tag4')) { 307 return array('prefix_id3', 'prefix_id4'); 308 } 309 310 return array(); 311 } 312 313 /** 314 * Return an array of stored cache ids which match any given tags 315 * 316 * In case of multiple tags, a logical AND is made between tags 317 * 318 * @param array $tags array of tags 319 * @return array array of any matching cache ids (string) 320 */ 321 public function getIdsMatchingAnyTags($tags = array()) 322 { 323 if ($tags == array('tag5', 'tag6')) { 324 return array('prefix_id5', 'prefix_id6'); 325 } 326 327 return array(); 328 } 329 330 /** 331 * Return the filling percentage of the backend storage 332 * 333 * @return int integer between 0 and 100 334 */ 335 public function getFillingPercentage() 336 { 337 return 50; 338 } 339 340 /** 341 * Return an array of metadatas for the given cache id 342 * 343 * The array must include these keys : 344 * - expire : the expire timestamp 345 * - tags : a string array of tags 346 * - mtime : timestamp of last modification time 347 * 348 * @param string $id cache id 349 * @return array array of metadatas (false if the cache id is not found) 350 */ 351 public function getMetadatas($id) 352 { 353 return false; 354 } 355 356 /** 357 * Give (if possible) an extra lifetime to the given cache id 358 * 359 * @param string $id cache id 360 * @param int $extraLifetime 361 * @return boolean true if ok 362 */ 363 public function touch($id, $extraLifetime) 364 { 365 return true; 366 } 367 368 /** 369 * Return an associative array of capabilities (booleans) of the backend 370 * 371 * The array must include these keys : 372 * - automatic_cleaning (is automating cleaning necessary) 373 * - tags (are tags supported) 374 * - expired_read (is it possible to read expired cache records 375 * (for doNotTestCacheValidity option for example)) 376 * - priority does the backend deal with priority when saving 377 * - infinite_lifetime (is infinite lifetime can work with this backend) 378 * - get_list (is it possible to get the list of cache ids and the complete list of tags) 379 * 380 * @return array associative of with capabilities 381 */ 382 public function getCapabilities() 383 { 384 return array( 385 'automatic_cleaning' => true, 386 'tags' => true, 387 'expired_read' => false, 388 'priority' => true, 389 'infinite_lifetime' => true, 390 'get_list' => true 391 ); 392 } 393 394 /** 395 * Add an event to the log array 396 * 397 * @param string $methodName MethodName 398 * @param array $args Arguments 399 * @return void 400 */ 401 private function _addLog($methodName, $args) 402 { 403 $this->_log[$this->_index] = array( 404 'methodName' => $methodName, 405 'args' => $args 406 ); 407 $this->_index = $this->_index + 1; 408 } 409 410}