PageRenderTime 30ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/vendor/zend/Zend/Cache/Backend/Test.php

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