|
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_Queue |
|
17 * @subpackage Adapter |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Array.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Queue_Adapter_AdapterAbstract |
|
25 */ |
|
26 require_once 'Zend/Queue/Adapter/AdapterAbstract.php'; |
|
27 |
|
28 /** |
|
29 * Class for using a standard PHP array as a queue |
|
30 * |
|
31 * @category Zend |
|
32 * @package Zend_Queue |
|
33 * @subpackage Adapter |
|
34 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
35 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
36 */ |
|
37 class Zend_Queue_Adapter_Array extends Zend_Queue_Adapter_AdapterAbstract |
|
38 { |
|
39 /** |
|
40 * @var array |
|
41 */ |
|
42 protected $_data = array(); |
|
43 |
|
44 /** |
|
45 * Constructor |
|
46 * |
|
47 * @param array|Zend_Config $options |
|
48 * @param Zend_Queue|null $queue |
|
49 * @return void |
|
50 */ |
|
51 public function __construct($options, Zend_Queue $queue = null) |
|
52 { |
|
53 parent::__construct($options, $queue); |
|
54 } |
|
55 |
|
56 /******************************************************************** |
|
57 * Queue management functions |
|
58 *********************************************************************/ |
|
59 |
|
60 /** |
|
61 * Does a queue already exist? |
|
62 * |
|
63 * Throws an exception if the adapter cannot determine if a queue exists. |
|
64 * use isSupported('isExists') to determine if an adapter can test for |
|
65 * queue existance. |
|
66 * |
|
67 * @param string $name |
|
68 * @return boolean |
|
69 */ |
|
70 public function isExists($name) |
|
71 { |
|
72 return array_key_exists($name, $this->_data); |
|
73 } |
|
74 |
|
75 /** |
|
76 * Create a new queue |
|
77 * |
|
78 * Visibility timeout is how long a message is left in the queue "invisible" |
|
79 * to other readers. If the message is acknowleged (deleted) before the |
|
80 * timeout, then the message is deleted. However, if the timeout expires |
|
81 * then the message will be made available to other queue readers. |
|
82 * |
|
83 * @param string $name queue name |
|
84 * @param integer $timeout default visibility timeout |
|
85 * @return boolean |
|
86 */ |
|
87 public function create($name, $timeout=null) |
|
88 { |
|
89 if ($this->isExists($name)) { |
|
90 return false; |
|
91 } |
|
92 if ($timeout === null) { |
|
93 $timeout = self::CREATE_TIMEOUT_DEFAULT; |
|
94 } |
|
95 $this->_data[$name] = array(); |
|
96 |
|
97 return true; |
|
98 } |
|
99 |
|
100 /** |
|
101 * Delete a queue and all of it's messages |
|
102 * |
|
103 * Returns false if the queue is not found, true if the queue exists |
|
104 * |
|
105 * @param string $name queue name |
|
106 * @return boolean |
|
107 */ |
|
108 public function delete($name) |
|
109 { |
|
110 $found = isset($this->_data[$name]); |
|
111 |
|
112 if ($found) { |
|
113 unset($this->_data[$name]); |
|
114 } |
|
115 |
|
116 return $found; |
|
117 } |
|
118 |
|
119 /** |
|
120 * Get an array of all available queues |
|
121 * |
|
122 * Not all adapters support getQueues(), use isSupported('getQueues') |
|
123 * to determine if the adapter supports this feature. |
|
124 * |
|
125 * @return array |
|
126 */ |
|
127 public function getQueues() |
|
128 { |
|
129 return array_keys($this->_data); |
|
130 } |
|
131 |
|
132 /** |
|
133 * Return the approximate number of messages in the queue |
|
134 * |
|
135 * @param Zend_Queue $queue |
|
136 * @return integer |
|
137 * @throws Zend_Queue_Exception |
|
138 */ |
|
139 public function count(Zend_Queue $queue=null) |
|
140 { |
|
141 if ($queue === null) { |
|
142 $queue = $this->_queue; |
|
143 } |
|
144 |
|
145 if (!isset($this->_data[$queue->getName()])) { |
|
146 /** |
|
147 * @see Zend_Queue_Exception |
|
148 */ |
|
149 require_once 'Zend/Queue/Exception.php'; |
|
150 throw new Zend_Queue_Exception('Queue does not exist'); |
|
151 } |
|
152 |
|
153 return count($this->_data[$queue->getName()]); |
|
154 } |
|
155 |
|
156 /******************************************************************** |
|
157 * Messsage management functions |
|
158 *********************************************************************/ |
|
159 |
|
160 /** |
|
161 * Send a message to the queue |
|
162 * |
|
163 * @param string $message Message to send to the active queue |
|
164 * @param Zend_Queue $queue |
|
165 * @return Zend_Queue_Message |
|
166 * @throws Zend_Queue_Exception |
|
167 */ |
|
168 public function send($message, Zend_Queue $queue=null) |
|
169 { |
|
170 if ($queue === null) { |
|
171 $queue = $this->_queue; |
|
172 } |
|
173 |
|
174 if (!$this->isExists($queue->getName())) { |
|
175 require_once 'Zend/Queue/Exception.php'; |
|
176 throw new Zend_Queue_Exception('Queue does not exist:' . $queue->getName()); |
|
177 } |
|
178 |
|
179 // create the message |
|
180 $data = array( |
|
181 'message_id' => md5(uniqid(rand(), true)), |
|
182 'body' => $message, |
|
183 'md5' => md5($message), |
|
184 'handle' => null, |
|
185 'created' => time(), |
|
186 'queue_name' => $queue->getName(), |
|
187 ); |
|
188 |
|
189 // add $data to the "queue" |
|
190 $this->_data[$queue->getName()][] = $data; |
|
191 |
|
192 $options = array( |
|
193 'queue' => $queue, |
|
194 'data' => $data, |
|
195 ); |
|
196 |
|
197 $classname = $queue->getMessageClass(); |
|
198 if (!class_exists($classname)) { |
|
199 require_once 'Zend/Loader.php'; |
|
200 Zend_Loader::loadClass($classname); |
|
201 } |
|
202 return new $classname($options); |
|
203 } |
|
204 |
|
205 /** |
|
206 * Get messages in the queue |
|
207 * |
|
208 * @param integer $maxMessages Maximum number of messages to return |
|
209 * @param integer $timeout Visibility timeout for these messages |
|
210 * @param Zend_Queue $queue |
|
211 * @return Zend_Queue_Message_Iterator |
|
212 */ |
|
213 public function receive($maxMessages = null, $timeout = null, Zend_Queue $queue = null) |
|
214 { |
|
215 if ($maxMessages === null) { |
|
216 $maxMessages = 1; |
|
217 } |
|
218 if ($timeout === null) { |
|
219 $timeout = self::RECEIVE_TIMEOUT_DEFAULT; |
|
220 } |
|
221 if ($queue === null) { |
|
222 $queue = $this->_queue; |
|
223 } |
|
224 |
|
225 $data = array(); |
|
226 if ($maxMessages > 0) { |
|
227 $start_time = microtime(true); |
|
228 |
|
229 $count = 0; |
|
230 $temp = &$this->_data[$queue->getName()]; |
|
231 foreach ($temp as $key=>&$msg) { |
|
232 // count check has to be first, as someone can ask for 0 messages |
|
233 // ZF-7650 |
|
234 if ($count >= $maxMessages) { |
|
235 break; |
|
236 } |
|
237 |
|
238 if (($msg['handle'] === null) |
|
239 || ($msg['timeout'] + $timeout < $start_time) |
|
240 ) { |
|
241 $msg['handle'] = md5(uniqid(rand(), true)); |
|
242 $msg['timeout'] = microtime(true); |
|
243 $data[] = $msg; |
|
244 $count++; |
|
245 } |
|
246 |
|
247 } |
|
248 } |
|
249 |
|
250 $options = array( |
|
251 'queue' => $queue, |
|
252 'data' => $data, |
|
253 'messageClass' => $queue->getMessageClass(), |
|
254 ); |
|
255 |
|
256 $classname = $queue->getMessageSetClass(); |
|
257 if (!class_exists($classname)) { |
|
258 require_once 'Zend/Loader.php'; |
|
259 Zend_Loader::loadClass($classname); |
|
260 } |
|
261 return new $classname($options); |
|
262 } |
|
263 |
|
264 /** |
|
265 * Delete a message from the queue |
|
266 * |
|
267 * Returns true if the message is deleted, false if the deletion is |
|
268 * unsuccessful. |
|
269 * |
|
270 * @param Zend_Queue_Message $message |
|
271 * @return boolean |
|
272 * @throws Zend_Queue_Exception |
|
273 */ |
|
274 public function deleteMessage(Zend_Queue_Message $message) |
|
275 { |
|
276 // load the queue |
|
277 $queue = &$this->_data[$message->queue_name]; |
|
278 |
|
279 foreach ($queue as $key => &$msg) { |
|
280 if ($msg['handle'] == $message->handle) { |
|
281 unset($queue[$key]); |
|
282 return true; |
|
283 } |
|
284 } |
|
285 |
|
286 return false; |
|
287 } |
|
288 |
|
289 /******************************************************************** |
|
290 * Supporting functions |
|
291 *********************************************************************/ |
|
292 |
|
293 /** |
|
294 * Return a list of queue capabilities functions |
|
295 * |
|
296 * $array['function name'] = true or false |
|
297 * true is supported, false is not supported. |
|
298 * |
|
299 * @param string $name |
|
300 * @return array |
|
301 */ |
|
302 public function getCapabilities() |
|
303 { |
|
304 return array( |
|
305 'create' => true, |
|
306 'delete' => true, |
|
307 'send' => true, |
|
308 'receive' => true, |
|
309 'deleteMessage' => true, |
|
310 'getQueues' => true, |
|
311 'count' => true, |
|
312 'isExists' => true, |
|
313 ); |
|
314 } |
|
315 |
|
316 /******************************************************************** |
|
317 * Functions that are not part of the Zend_Queue_Adapter_Abstract |
|
318 *********************************************************************/ |
|
319 |
|
320 /** |
|
321 * serialize |
|
322 */ |
|
323 public function __sleep() |
|
324 { |
|
325 return array('_data'); |
|
326 } |
|
327 |
|
328 /* |
|
329 * These functions are debug helpers. |
|
330 */ |
|
331 |
|
332 /** |
|
333 * returns underlying _data array |
|
334 * $queue->getAdapter()->getData(); |
|
335 * |
|
336 * @return $this; |
|
337 */ |
|
338 public function getData() |
|
339 { |
|
340 return $this->_data; |
|
341 } |
|
342 |
|
343 /** |
|
344 * sets the underlying _data array |
|
345 * $queue->getAdapter()->setData($data); |
|
346 * |
|
347 * @param $data array |
|
348 * @return $this; |
|
349 */ |
|
350 public function setData($data) |
|
351 { |
|
352 $this->_data = $data; |
|
353 return $this; |
|
354 } |
|
355 } |