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