MemcachedMock.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Tests\Profiler\Mock;
  11. /**
  12. * MemcachedMock for simulating Memcached extension in tests.
  13. *
  14. * @author Andrej Hudec <pulzarraider@gmail.com>
  15. */
  16. class MemcachedMock
  17. {
  18. private $connected = false;
  19. private $storage = array();
  20. /**
  21. * Set a Memcached option.
  22. *
  23. * @param int $option
  24. * @param mixed $value
  25. *
  26. * @return bool
  27. */
  28. public function setOption($option, $value)
  29. {
  30. return true;
  31. }
  32. /**
  33. * Add a memcached server to connection pool.
  34. *
  35. * @param string $host
  36. * @param int $port
  37. * @param int $weight
  38. *
  39. * @return bool
  40. */
  41. public function addServer($host, $port = 11211, $weight = 0)
  42. {
  43. if ('127.0.0.1' == $host && 11211 == $port) {
  44. $this->connected = true;
  45. return true;
  46. }
  47. return false;
  48. }
  49. /**
  50. * Add an item to the server only if such key doesn't exist at the server yet.
  51. *
  52. * @param string $key
  53. * @param mixed $value
  54. * @param int $expiration
  55. *
  56. * @return bool
  57. */
  58. public function add($key, $value, $expiration = 0)
  59. {
  60. if (!$this->connected) {
  61. return false;
  62. }
  63. if (!isset($this->storage[$key])) {
  64. $this->storeData($key, $value);
  65. return true;
  66. }
  67. return false;
  68. }
  69. /**
  70. * Store data at the server.
  71. *
  72. * @param string $key
  73. * @param mixed $value
  74. * @param int $expiration
  75. *
  76. * @return bool
  77. */
  78. public function set($key, $value, $expiration = null)
  79. {
  80. if (!$this->connected) {
  81. return false;
  82. }
  83. $this->storeData($key, $value);
  84. return true;
  85. }
  86. /**
  87. * Replace value of the existing item.
  88. *
  89. * @param string $key
  90. * @param mixed $value
  91. * @param int $expiration
  92. *
  93. * @return bool
  94. */
  95. public function replace($key, $value, $expiration = null)
  96. {
  97. if (!$this->connected) {
  98. return false;
  99. }
  100. if (isset($this->storage[$key])) {
  101. $this->storeData($key, $value);
  102. return true;
  103. }
  104. return false;
  105. }
  106. /**
  107. * Retrieve item from the server.
  108. *
  109. * @param string $key
  110. * @param callable $cache_cb
  111. * @param float $cas_token
  112. *
  113. * @return bool
  114. */
  115. public function get($key, $cache_cb = null, &$cas_token = null)
  116. {
  117. if (!$this->connected) {
  118. return false;
  119. }
  120. return $this->getData($key);
  121. }
  122. /**
  123. * Append data to an existing item.
  124. *
  125. * @param string $key
  126. * @param string $value
  127. *
  128. * @return bool
  129. */
  130. public function append($key, $value)
  131. {
  132. if (!$this->connected) {
  133. return false;
  134. }
  135. if (isset($this->storage[$key])) {
  136. $this->storeData($key, $this->getData($key).$value);
  137. return true;
  138. }
  139. return false;
  140. }
  141. /**
  142. * Delete item from the server.
  143. *
  144. * @param string $key
  145. *
  146. * @return bool
  147. */
  148. public function delete($key)
  149. {
  150. if (!$this->connected) {
  151. return false;
  152. }
  153. if (isset($this->storage[$key])) {
  154. unset($this->storage[$key]);
  155. return true;
  156. }
  157. return false;
  158. }
  159. /**
  160. * Flush all existing items at the server.
  161. *
  162. * @return bool
  163. */
  164. public function flush()
  165. {
  166. if (!$this->connected) {
  167. return false;
  168. }
  169. $this->storage = array();
  170. return true;
  171. }
  172. private function getData($key)
  173. {
  174. if (isset($this->storage[$key])) {
  175. return unserialize($this->storage[$key]);
  176. }
  177. return false;
  178. }
  179. private function storeData($key, $value)
  180. {
  181. $this->storage[$key] = serialize($value);
  182. return true;
  183. }
  184. }