PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Zend/Cache/Backend/Test.php

https://bitbucket.org/micromax/vox-fw
PHP | 413 lines | 159 code | 33 blank | 221 comment | 37 complexity | 2b086b4eeefdf639cf5de9d79f722f24 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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. || $id == '88161989b73a4cbfd0b701c446115a99'
  101. || $id == '205fc79cba24f0f0018eb92c7c8b3ba4'
  102. || $id == '170720e35f38150b811f68a937fb042d')
  103. {
  104. return false;
  105. }
  106. if ($id=='serialized') {
  107. return serialize(array('foo'));
  108. }
  109. if ($id=='serialized2') {
  110. return serialize(array('headers' => array(), 'data' => 'foo'));
  111. }
  112. if ( $id == '71769f39054f75894288e397df04e445' || $id == '615d222619fb20b527168340cebd0578'
  113. || $id == '8a02d218a5165c467e7a5747cc6bd4b6' || $id == '648aca1366211d17cbf48e65dc570bee'
  114. || $id == '4a923ef02d7f997ca14d56dfeae25ea7') {
  115. return serialize(array('foo', 'bar'));
  116. }
  117. return 'foo';
  118. }
  119. /**
  120. * Test if a cache is available or not (for the given id)
  121. *
  122. * For this test backend only, if $id == 'false', then the method will return false
  123. * (123456 else)
  124. *
  125. * @param string $id Cache id
  126. * @return mixed|false false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  127. */
  128. public function test($id)
  129. {
  130. $this->_addLog('test', array($id));
  131. if ($id=='false') {
  132. return false;
  133. }
  134. if (($id=='3c439c922209e2cb0b54d6deffccd75a')) {
  135. return false;
  136. }
  137. return 123456;
  138. }
  139. /**
  140. * Save some string datas into a cache record
  141. *
  142. * For this test backend only, if $id == 'false', then the method will return false
  143. * (true else)
  144. *
  145. * @param string $data Datas to cache
  146. * @param string $id Cache id
  147. * @param array $tags Array of strings, the cache record will be tagged by each string entry
  148. * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
  149. * @return boolean True if no problem
  150. */
  151. public function save($data, $id, $tags = array(), $specificLifetime = false)
  152. {
  153. $this->_addLog('save', array($data, $id, $tags));
  154. if (substr($id,-5)=='false') {
  155. return false;
  156. }
  157. return true;
  158. }
  159. /**
  160. * Remove a cache record
  161. *
  162. * For this test backend only, if $id == 'false', then the method will return false
  163. * (true else)
  164. *
  165. * @param string $id Cache id
  166. * @return boolean True if no problem
  167. */
  168. public function remove($id)
  169. {
  170. $this->_addLog('remove', array($id));
  171. if (substr($id,-5)=='false') {
  172. return false;
  173. }
  174. return true;
  175. }
  176. /**
  177. * Clean some cache records
  178. *
  179. * For this test backend only, if $mode == 'false', then the method will return false
  180. * (true else)
  181. *
  182. * Available modes are :
  183. * Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used)
  184. * Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used)
  185. * Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags
  186. * ($tags can be an array of strings or a single string)
  187. * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
  188. * ($tags can be an array of strings or a single string)
  189. *
  190. * @param string $mode Clean mode
  191. * @param array $tags Array of tags
  192. * @return boolean True if no problem
  193. */
  194. public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
  195. {
  196. $this->_addLog('clean', array($mode, $tags));
  197. if ($mode=='false') {
  198. return false;
  199. }
  200. return true;
  201. }
  202. /**
  203. * Get the last log
  204. *
  205. * @return string The last log
  206. */
  207. public function getLastLog()
  208. {
  209. return $this->_log[$this->_index - 1];
  210. }
  211. /**
  212. * Get the log index
  213. *
  214. * @return int Log index
  215. */
  216. public function getLogIndex()
  217. {
  218. return $this->_index;
  219. }
  220. /**
  221. * Get the complete log array
  222. *
  223. * @return array Complete log array
  224. */
  225. public function getAllLogs()
  226. {
  227. return $this->_log;
  228. }
  229. /**
  230. * Return true if the automatic cleaning is available for the backend
  231. *
  232. * @return boolean
  233. */
  234. public function isAutomaticCleaningAvailable()
  235. {
  236. return true;
  237. }
  238. /**
  239. * Return an array of stored cache ids
  240. *
  241. * @return array array of stored cache ids (string)
  242. */
  243. public function getIds()
  244. {
  245. return array(
  246. 'prefix_id1', 'prefix_id2'
  247. );
  248. }
  249. /**
  250. * Return an array of stored tags
  251. *
  252. * @return array array of stored tags (string)
  253. */
  254. public function getTags()
  255. {
  256. return array(
  257. 'tag1', 'tag2'
  258. );
  259. }
  260. /**
  261. * Return an array of stored cache ids which match given tags
  262. *
  263. * In case of multiple tags, a logical AND is made between tags
  264. *
  265. * @param array $tags array of tags
  266. * @return array array of matching cache ids (string)
  267. */
  268. public function getIdsMatchingTags($tags = array())
  269. {
  270. if ($tags == array('tag1', 'tag2')) {
  271. return array('prefix_id1', 'prefix_id2');
  272. }
  273. return array();
  274. }
  275. /**
  276. * Return an array of stored cache ids which don't match given tags
  277. *
  278. * In case of multiple tags, a logical OR is made between tags
  279. *
  280. * @param array $tags array of tags
  281. * @return array array of not matching cache ids (string)
  282. */
  283. public function getIdsNotMatchingTags($tags = array())
  284. {
  285. if ($tags == array('tag3', 'tag4')) {
  286. return array('prefix_id3', 'prefix_id4');
  287. }
  288. return array();
  289. }
  290. /**
  291. * Return an array of stored cache ids which match any given tags
  292. *
  293. * In case of multiple tags, a logical AND is made between tags
  294. *
  295. * @param array $tags array of tags
  296. * @return array array of any matching cache ids (string)
  297. */
  298. public function getIdsMatchingAnyTags($tags = array())
  299. {
  300. if ($tags == array('tag5', 'tag6')) {
  301. return array('prefix_id5', 'prefix_id6');
  302. }
  303. return array();
  304. }
  305. /**
  306. * Return the filling percentage of the backend storage
  307. *
  308. * @return int integer between 0 and 100
  309. */
  310. public function getFillingPercentage()
  311. {
  312. return 50;
  313. }
  314. /**
  315. * Return an array of metadatas for the given cache id
  316. *
  317. * The array must include these keys :
  318. * - expire : the expire timestamp
  319. * - tags : a string array of tags
  320. * - mtime : timestamp of last modification time
  321. *
  322. * @param string $id cache id
  323. * @return array array of metadatas (false if the cache id is not found)
  324. */
  325. public function getMetadatas($id)
  326. {
  327. return false;
  328. }
  329. /**
  330. * Give (if possible) an extra lifetime to the given cache id
  331. *
  332. * @param string $id cache id
  333. * @param int $extraLifetime
  334. * @return boolean true if ok
  335. */
  336. public function touch($id, $extraLifetime)
  337. {
  338. return true;
  339. }
  340. /**
  341. * Return an associative array of capabilities (booleans) of the backend
  342. *
  343. * The array must include these keys :
  344. * - automatic_cleaning (is automating cleaning necessary)
  345. * - tags (are tags supported)
  346. * - expired_read (is it possible to read expired cache records
  347. * (for doNotTestCacheValidity option for example))
  348. * - priority does the backend deal with priority when saving
  349. * - infinite_lifetime (is infinite lifetime can work with this backend)
  350. * - get_list (is it possible to get the list of cache ids and the complete list of tags)
  351. *
  352. * @return array associative of with capabilities
  353. */
  354. public function getCapabilities()
  355. {
  356. return array(
  357. 'automatic_cleaning' => true,
  358. 'tags' => true,
  359. 'expired_read' => false,
  360. 'priority' => true,
  361. 'infinite_lifetime' => true,
  362. 'get_list' => true
  363. );
  364. }
  365. /**
  366. * Add an event to the log array
  367. *
  368. * @param string $methodName MethodName
  369. * @param array $args Arguments
  370. * @return void
  371. */
  372. private function _addLog($methodName, $args)
  373. {
  374. $this->_log[$this->_index] = array(
  375. 'methodName' => $methodName,
  376. 'args' => $args
  377. );
  378. $this->_index = $this->_index + 1;
  379. }
  380. }