|
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 Message |
|
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: Iterator.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @category Zend |
|
25 * @package Zend_Queue |
|
26 * @subpackage Message |
|
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
28 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
29 */ |
|
30 class Zend_Queue_Message_Iterator implements Iterator, Countable |
|
31 { |
|
32 /** |
|
33 * The data for the queue message |
|
34 * |
|
35 * @var array |
|
36 */ |
|
37 protected $_data = array(); |
|
38 |
|
39 /** |
|
40 * Connected is true if we have a reference to a live |
|
41 * Zend_Queue_Adapter_AdapterInterface object. |
|
42 * This is false after the Message has been deserialized. |
|
43 * |
|
44 * @var boolean |
|
45 */ |
|
46 protected $_connected = true; |
|
47 |
|
48 /** |
|
49 * Zend_Queue_Adapter_AdapterInterface parent class or instance |
|
50 * |
|
51 * @var Zend_Queue_Adapter_AdapterInterface |
|
52 */ |
|
53 protected $_queue = null; |
|
54 |
|
55 /** |
|
56 * Name of the class of the Zend_Queue_Adapter_AdapterInterface object. |
|
57 * |
|
58 * @var string |
|
59 */ |
|
60 protected $_queueClass = null; |
|
61 |
|
62 /** |
|
63 * Zend_Queue_Message class name |
|
64 * |
|
65 * @var string |
|
66 */ |
|
67 protected $_messageClass = 'Zend_Queue_Message'; |
|
68 |
|
69 /** |
|
70 * Iterator pointer. |
|
71 * |
|
72 * @var integer |
|
73 */ |
|
74 protected $_pointer = 0; |
|
75 |
|
76 /** |
|
77 * Constructor |
|
78 * |
|
79 * @param array $options ('queue', 'messageClass', 'data'=>array()); |
|
80 * @return void |
|
81 */ |
|
82 public function __construct(array $options = array()) |
|
83 { |
|
84 if (isset($options['queue'])) { |
|
85 $this->_queue = $options['queue']; |
|
86 $this->_queueClass = get_class($this->_queue); |
|
87 $this->_connected = true; |
|
88 } else { |
|
89 $this->_connected = false; |
|
90 } |
|
91 if (isset($options['messageClass'])) { |
|
92 $this->_messageClass = $options['messageClass']; |
|
93 } |
|
94 |
|
95 if (!is_array($options['data'])) { |
|
96 require_once 'Zend/Queue/Exception.php'; |
|
97 throw new Zend_Queue_Exception('array optionsuration must have $options[\'data\'] = array'); |
|
98 } |
|
99 |
|
100 // load the message class |
|
101 $classname = $this->_messageClass; |
|
102 if (!class_exists($classname)) { |
|
103 require_once 'Zend/Loader.php'; |
|
104 Zend_Loader::loadClass($classname); |
|
105 } |
|
106 |
|
107 // for each of the messages |
|
108 foreach ($options['data'] as $data) { |
|
109 // construct the message parameters |
|
110 $message = array('data' => $data); |
|
111 |
|
112 // If queue has not been set, then use the default. |
|
113 if (empty($message['queue'])) { |
|
114 $message['queue'] = $this->_queue; |
|
115 } |
|
116 |
|
117 // construct the message and add it to _data[]; |
|
118 $this->_data[] = new $classname($message); |
|
119 } |
|
120 } |
|
121 |
|
122 /** |
|
123 * Store queue and data in serialized object |
|
124 * |
|
125 * @return array |
|
126 */ |
|
127 public function __sleep() |
|
128 { |
|
129 return array('_data', '_queueClass', '_messageClass', '_pointer'); |
|
130 } |
|
131 |
|
132 /** |
|
133 * Setup to do on wakeup. |
|
134 * A de-serialized Message should not be assumed to have access to a live |
|
135 * queue connection, so set _connected = false. |
|
136 * |
|
137 * @return void |
|
138 */ |
|
139 public function __wakeup() |
|
140 { |
|
141 $this->_connected = false; |
|
142 } |
|
143 |
|
144 /** |
|
145 * Returns all data as an array. |
|
146 * |
|
147 * Used for debugging. |
|
148 * |
|
149 * @return array |
|
150 */ |
|
151 public function toArray() |
|
152 { |
|
153 // @todo This works only if we have iterated through |
|
154 // the result set once to instantiate the messages. |
|
155 foreach ($this->_data as $i => $message) { |
|
156 $this->_data[$i] = $message->toArray(); |
|
157 } |
|
158 return $this->_data; |
|
159 } |
|
160 |
|
161 /** |
|
162 * Returns the queue object, or null if this is disconnected message set |
|
163 * |
|
164 * @return Zend_Queue|null |
|
165 */ |
|
166 public function getQueue() |
|
167 { |
|
168 return $this->_queue; |
|
169 } |
|
170 |
|
171 /** |
|
172 * Set the queue object, to re-establish a live connection |
|
173 * to the queue for a Message that has been de-serialized. |
|
174 * |
|
175 * @param Zend_Queue_Adapter_AdapterInterface $queue |
|
176 * @return boolean |
|
177 * @throws Zend_Queue_Exception |
|
178 */ |
|
179 public function setQueue(Zend_Queue $queue) |
|
180 { |
|
181 $this->_queue = $queue; |
|
182 $this->_connected = false; |
|
183 |
|
184 // @todo This works only if we have iterated through |
|
185 // the result set once to instantiate the rows. |
|
186 foreach ($this->_data as $i => $message) { |
|
187 $this->_connected = $this->_connected || $message->setQueue($queue); |
|
188 } |
|
189 |
|
190 return $this->_connected; |
|
191 } |
|
192 |
|
193 /** |
|
194 * Query the class name of the Queue object for which this |
|
195 * Message was created. |
|
196 * |
|
197 * @return string |
|
198 */ |
|
199 public function getQueueClass() |
|
200 { |
|
201 return $this->_queueClass; |
|
202 } |
|
203 |
|
204 /* |
|
205 * Iterator implementation |
|
206 */ |
|
207 |
|
208 /** |
|
209 * Rewind the Iterator to the first element. |
|
210 * Similar to the reset() function for arrays in PHP. |
|
211 * Required by interface Iterator. |
|
212 * |
|
213 * @return void |
|
214 */ |
|
215 public function rewind() |
|
216 { |
|
217 $this->_pointer = 0; |
|
218 } |
|
219 |
|
220 /** |
|
221 * Return the current element. |
|
222 * Similar to the current() function for arrays in PHP |
|
223 * Required by interface Iterator. |
|
224 * |
|
225 * @return Zend_Queue_Message current element from the collection |
|
226 */ |
|
227 public function current() |
|
228 { |
|
229 return (($this->valid() === false) |
|
230 ? null |
|
231 : $this->_data[$this->_pointer]); // return the messages object |
|
232 } |
|
233 |
|
234 /** |
|
235 * Return the identifying key of the current element. |
|
236 * Similar to the key() function for arrays in PHP. |
|
237 * Required by interface Iterator. |
|
238 * |
|
239 * @return integer |
|
240 */ |
|
241 public function key() |
|
242 { |
|
243 return $this->_pointer; |
|
244 } |
|
245 |
|
246 /** |
|
247 * Move forward to next element. |
|
248 * Similar to the next() function for arrays in PHP. |
|
249 * Required by interface Iterator. |
|
250 * |
|
251 * @return void |
|
252 */ |
|
253 public function next() |
|
254 { |
|
255 ++$this->_pointer; |
|
256 } |
|
257 |
|
258 /** |
|
259 * Check if there is a current element after calls to rewind() or next(). |
|
260 * Used to check if we've iterated to the end of the collection. |
|
261 * Required by interface Iterator. |
|
262 * |
|
263 * @return bool False if there's nothing more to iterate over |
|
264 */ |
|
265 public function valid() |
|
266 { |
|
267 return $this->_pointer < count($this); |
|
268 } |
|
269 |
|
270 /* |
|
271 * Countable Implementation |
|
272 */ |
|
273 |
|
274 /** |
|
275 * Returns the number of elements in the collection. |
|
276 * |
|
277 * Implements Countable::count() |
|
278 * |
|
279 * @return integer |
|
280 */ |
|
281 public function count() |
|
282 { |
|
283 return count($this->_data); |
|
284 } |
|
285 } |