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