Version 1
[yaffs-website] / vendor / symfony / http-kernel / Tests / Profiler / Mock / MemcacheMock.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests\Profiler\Mock;
13
14 /**
15  * MemcacheMock for simulating Memcache extension in tests.
16  *
17  * @author Andrej Hudec <pulzarraider@gmail.com>
18  */
19 class MemcacheMock
20 {
21     private $connected = false;
22     private $storage = array();
23
24     /**
25      * Open memcached server connection.
26      *
27      * @param string $host
28      * @param int    $port
29      * @param int    $timeout
30      *
31      * @return bool
32      */
33     public function connect($host, $port = null, $timeout = null)
34     {
35         if ('127.0.0.1' == $host && 11211 == $port) {
36             $this->connected = true;
37
38             return true;
39         }
40
41         return false;
42     }
43
44     /**
45      * Open memcached server persistent connection.
46      *
47      * @param string $host
48      * @param int    $port
49      * @param int    $timeout
50      *
51      * @return bool
52      */
53     public function pconnect($host, $port = null, $timeout = null)
54     {
55         if ('127.0.0.1' == $host && 11211 == $port) {
56             $this->connected = true;
57
58             return true;
59         }
60
61         return false;
62     }
63
64     /**
65      * Add a memcached server to connection pool.
66      *
67      * @param string   $host
68      * @param int      $port
69      * @param bool     $persistent
70      * @param int      $weight
71      * @param int      $timeout
72      * @param int      $retry_interval
73      * @param bool     $status
74      * @param callable $failure_callback
75      * @param int      $timeoutms
76      *
77      * @return bool
78      */
79     public function addServer($host, $port = 11211, $persistent = null, $weight = null, $timeout = null, $retry_interval = null, $status = null, $failure_callback = null, $timeoutms = null)
80     {
81         if ('127.0.0.1' == $host && 11211 == $port) {
82             $this->connected = true;
83
84             return true;
85         }
86
87         return false;
88     }
89
90     /**
91      * Add an item to the server only if such key doesn't exist at the server yet.
92      *
93      * @param string $key
94      * @param mixed  $var
95      * @param int    $flag
96      * @param int    $expire
97      *
98      * @return bool
99      */
100     public function add($key, $var, $flag = null, $expire = null)
101     {
102         if (!$this->connected) {
103             return false;
104         }
105
106         if (!isset($this->storage[$key])) {
107             $this->storeData($key, $var);
108
109             return true;
110         }
111
112         return false;
113     }
114
115     /**
116      * Store data at the server.
117      *
118      * @param string $key
119      * @param string $var
120      * @param int    $flag
121      * @param int    $expire
122      *
123      * @return bool
124      */
125     public function set($key, $var, $flag = null, $expire = null)
126     {
127         if (!$this->connected) {
128             return false;
129         }
130
131         $this->storeData($key, $var);
132
133         return true;
134     }
135
136     /**
137      * Replace value of the existing item.
138      *
139      * @param string $key
140      * @param mixed  $var
141      * @param int    $flag
142      * @param int    $expire
143      *
144      * @return bool
145      */
146     public function replace($key, $var, $flag = null, $expire = null)
147     {
148         if (!$this->connected) {
149             return false;
150         }
151
152         if (isset($this->storage[$key])) {
153             $this->storeData($key, $var);
154
155             return true;
156         }
157
158         return false;
159     }
160
161     /**
162      * Retrieve item from the server.
163      *
164      * @param string|array $key
165      * @param int|array    $flags
166      *
167      * @return mixed
168      */
169     public function get($key, &$flags = null)
170     {
171         if (!$this->connected) {
172             return false;
173         }
174
175         if (is_array($key)) {
176             $result = array();
177             foreach ($key as $k) {
178                 if (isset($this->storage[$k])) {
179                     $result[] = $this->getData($k);
180                 }
181             }
182
183             return $result;
184         }
185
186         return $this->getData($key);
187     }
188
189     /**
190      * Delete item from the server.
191      *
192      * @param string $key
193      *
194      * @return bool
195      */
196     public function delete($key)
197     {
198         if (!$this->connected) {
199             return false;
200         }
201
202         if (isset($this->storage[$key])) {
203             unset($this->storage[$key]);
204
205             return true;
206         }
207
208         return false;
209     }
210
211     /**
212      * Flush all existing items at the server.
213      *
214      * @return bool
215      */
216     public function flush()
217     {
218         if (!$this->connected) {
219             return false;
220         }
221
222         $this->storage = array();
223
224         return true;
225     }
226
227     /**
228      * Close memcached server connection.
229      *
230      * @return bool
231      */
232     public function close()
233     {
234         $this->connected = false;
235
236         return true;
237     }
238
239     private function getData($key)
240     {
241         if (isset($this->storage[$key])) {
242             return unserialize($this->storage[$key]);
243         }
244
245         return false;
246     }
247
248     private function storeData($key, $value)
249     {
250         $this->storage[$key] = serialize($value);
251
252         return true;
253     }
254 }