MemcacheMock.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. * MemcacheMock for simulating Memcache extension in tests.
  13. *
  14. * @author Andrej Hudec <pulzarraider@gmail.com>
  15. */
  16. class MemcacheMock
  17. {
  18. private $connected = false;
  19. private $storage = array();
  20. /**
  21. * Open memcached server connection.
  22. *
  23. * @param string $host
  24. * @param int $port
  25. * @param int $timeout
  26. *
  27. * @return bool
  28. */
  29. public function connect($host, $port = null, $timeout = null)
  30. {
  31. if ('127.0.0.1' == $host && 11211 == $port) {
  32. $this->connected = true;
  33. return true;
  34. }
  35. return false;
  36. }
  37. /**
  38. * Open memcached server persistent connection.
  39. *
  40. * @param string $host
  41. * @param int $port
  42. * @param int $timeout
  43. *
  44. * @return bool
  45. */
  46. public function pconnect($host, $port = null, $timeout = null)
  47. {
  48. if ('127.0.0.1' == $host && 11211 == $port) {
  49. $this->connected = true;
  50. return true;
  51. }
  52. return false;
  53. }
  54. /**
  55. * Add a memcached server to connection pool.
  56. *
  57. * @param string $host
  58. * @param int $port
  59. * @param bool $persistent
  60. * @param int $weight
  61. * @param int $timeout
  62. * @param int $retry_interval
  63. * @param bool $status
  64. * @param callable $failure_callback
  65. * @param int $timeoutms
  66. *
  67. * @return bool
  68. */
  69. public function addServer($host, $port = 11211, $persistent = null, $weight = null, $timeout = null, $retry_interval = null, $status = null, $failure_callback = null, $timeoutms = null)
  70. {
  71. if ('127.0.0.1' == $host && 11211 == $port) {
  72. $this->connected = true;
  73. return true;
  74. }
  75. return false;
  76. }
  77. /**
  78. * Add an item to the server only if such key doesn't exist at the server yet.
  79. *
  80. * @param string $key
  81. * @param mixed $var
  82. * @param int $flag
  83. * @param int $expire
  84. *
  85. * @return bool
  86. */
  87. public function add($key, $var, $flag = null, $expire = null)
  88. {
  89. if (!$this->connected) {
  90. return false;
  91. }
  92. if (!isset($this->storage[$key])) {
  93. $this->storeData($key, $var);
  94. return true;
  95. }
  96. return false;
  97. }
  98. /**
  99. * Store data at the server.
  100. *
  101. * @param string $key
  102. * @param string $var
  103. * @param int $flag
  104. * @param int $expire
  105. *
  106. * @return bool
  107. */
  108. public function set($key, $var, $flag = null, $expire = null)
  109. {
  110. if (!$this->connected) {
  111. return false;
  112. }
  113. $this->storeData($key, $var);
  114. return true;
  115. }
  116. /**
  117. * Replace value of the existing item.
  118. *
  119. * @param string $key
  120. * @param mixed $var
  121. * @param int $flag
  122. * @param int $expire
  123. *
  124. * @return bool
  125. */
  126. public function replace($key, $var, $flag = null, $expire = null)
  127. {
  128. if (!$this->connected) {
  129. return false;
  130. }
  131. if (isset($this->storage[$key])) {
  132. $this->storeData($key, $var);
  133. return true;
  134. }
  135. return false;
  136. }
  137. /**
  138. * Retrieve item from the server.
  139. *
  140. * @param string|array $key
  141. * @param int|array $flags
  142. *
  143. * @return mixed
  144. */
  145. public function get($key, &$flags = null)
  146. {
  147. if (!$this->connected) {
  148. return false;
  149. }
  150. if (\is_array($key)) {
  151. $result = array();
  152. foreach ($key as $k) {
  153. if (isset($this->storage[$k])) {
  154. $result[] = $this->getData($k);
  155. }
  156. }
  157. return $result;
  158. }
  159. return $this->getData($key);
  160. }
  161. /**
  162. * Delete item from the server.
  163. *
  164. * @param string $key
  165. *
  166. * @return bool
  167. */
  168. public function delete($key)
  169. {
  170. if (!$this->connected) {
  171. return false;
  172. }
  173. if (isset($this->storage[$key])) {
  174. unset($this->storage[$key]);
  175. return true;
  176. }
  177. return false;
  178. }
  179. /**
  180. * Flush all existing items at the server.
  181. *
  182. * @return bool
  183. */
  184. public function flush()
  185. {
  186. if (!$this->connected) {
  187. return false;
  188. }
  189. $this->storage = array();
  190. return true;
  191. }
  192. /**
  193. * Close memcached server connection.
  194. *
  195. * @return bool
  196. */
  197. public function close()
  198. {
  199. $this->connected = false;
  200. return true;
  201. }
  202. private function getData($key)
  203. {
  204. if (isset($this->storage[$key])) {
  205. return unserialize($this->storage[$key]);
  206. }
  207. return false;
  208. }
  209. private function storeData($key, $value)
  210. {
  211. $this->storage[$key] = serialize($value);
  212. return true;
  213. }
  214. }