File indexing completed on 2024-12-22 05:36:57
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Queue 0017 * @subpackage Message 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** 0024 * @category Zend 0025 * @package Zend_Queue 0026 * @subpackage Message 0027 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0028 * @license http://framework.zend.com/license/new-bsd New BSD License 0029 */ 0030 class Zend_Queue_Message_Iterator implements Iterator, Countable 0031 { 0032 /** 0033 * The data for the queue message 0034 * 0035 * @var array 0036 */ 0037 protected $_data = array(); 0038 0039 /** 0040 * Connected is true if we have a reference to a live 0041 * Zend_Queue_Adapter_AdapterInterface object. 0042 * This is false after the Message has been deserialized. 0043 * 0044 * @var boolean 0045 */ 0046 protected $_connected = true; 0047 0048 /** 0049 * Zend_Queue_Adapter_AdapterInterface parent class or instance 0050 * 0051 * @var Zend_Queue_Adapter_AdapterInterface 0052 */ 0053 protected $_queue = null; 0054 0055 /** 0056 * Name of the class of the Zend_Queue_Adapter_AdapterInterface object. 0057 * 0058 * @var string 0059 */ 0060 protected $_queueClass = null; 0061 0062 /** 0063 * Zend_Queue_Message class name 0064 * 0065 * @var string 0066 */ 0067 protected $_messageClass = 'Zend_Queue_Message'; 0068 0069 /** 0070 * Iterator pointer. 0071 * 0072 * @var integer 0073 */ 0074 protected $_pointer = 0; 0075 0076 /** 0077 * Constructor 0078 * 0079 * @param array $options ('queue', 'messageClass', 'data'=>array()); 0080 * @return void 0081 */ 0082 public function __construct(array $options = array()) 0083 { 0084 if (isset($options['queue'])) { 0085 $this->_queue = $options['queue']; 0086 $this->_queueClass = get_class($this->_queue); 0087 $this->_connected = true; 0088 } else { 0089 $this->_connected = false; 0090 } 0091 if (isset($options['messageClass'])) { 0092 $this->_messageClass = $options['messageClass']; 0093 } 0094 0095 if (!is_array($options['data'])) { 0096 // require_once 'Zend/Queue/Exception.php'; 0097 throw new Zend_Queue_Exception('array optionsuration must have $options[\'data\'] = array'); 0098 } 0099 0100 // load the message class 0101 $classname = $this->_messageClass; 0102 if (!class_exists($classname)) { 0103 // require_once 'Zend/Loader.php'; 0104 Zend_Loader::loadClass($classname); 0105 } 0106 0107 // for each of the messages 0108 foreach ($options['data'] as $data) { 0109 // construct the message parameters 0110 $message = array('data' => $data); 0111 0112 // If queue has not been set, then use the default. 0113 if (empty($message['queue'])) { 0114 $message['queue'] = $this->_queue; 0115 } 0116 0117 // construct the message and add it to _data[]; 0118 $this->_data[] = new $classname($message); 0119 } 0120 } 0121 0122 /** 0123 * Store queue and data in serialized object 0124 * 0125 * @return array 0126 */ 0127 public function __sleep() 0128 { 0129 return array('_data', '_queueClass', '_messageClass', '_pointer'); 0130 } 0131 0132 /** 0133 * Setup to do on wakeup. 0134 * A de-serialized Message should not be assumed to have access to a live 0135 * queue connection, so set _connected = false. 0136 * 0137 * @return void 0138 */ 0139 public function __wakeup() 0140 { 0141 $this->_connected = false; 0142 } 0143 0144 /** 0145 * Returns all data as an array. 0146 * 0147 * Used for debugging. 0148 * 0149 * @return array 0150 */ 0151 public function toArray() 0152 { 0153 // @todo This works only if we have iterated through 0154 // the result set once to instantiate the messages. 0155 foreach ($this->_data as $i => $message) { 0156 $this->_data[$i] = $message->toArray(); 0157 } 0158 return $this->_data; 0159 } 0160 0161 /** 0162 * Returns the queue object, or null if this is disconnected message set 0163 * 0164 * @return Zend_Queue|null 0165 */ 0166 public function getQueue() 0167 { 0168 return $this->_queue; 0169 } 0170 0171 /** 0172 * Set the queue object, to re-establish a live connection 0173 * to the queue for a Message that has been de-serialized. 0174 * 0175 * @param Zend_Queue_Adapter_AdapterInterface $queue 0176 * @return boolean 0177 * @throws Zend_Queue_Exception 0178 */ 0179 public function setQueue(Zend_Queue $queue) 0180 { 0181 $this->_queue = $queue; 0182 $this->_connected = false; 0183 0184 // @todo This works only if we have iterated through 0185 // the result set once to instantiate the rows. 0186 foreach ($this->_data as $i => $message) { 0187 $this->_connected = $this->_connected || $message->setQueue($queue); 0188 } 0189 0190 return $this->_connected; 0191 } 0192 0193 /** 0194 * Query the class name of the Queue object for which this 0195 * Message was created. 0196 * 0197 * @return string 0198 */ 0199 public function getQueueClass() 0200 { 0201 return $this->_queueClass; 0202 } 0203 0204 /* 0205 * Iterator implementation 0206 */ 0207 0208 /** 0209 * Rewind the Iterator to the first element. 0210 * Similar to the reset() function for arrays in PHP. 0211 * Required by interface Iterator. 0212 * 0213 * @return void 0214 */ 0215 public function rewind() 0216 { 0217 $this->_pointer = 0; 0218 } 0219 0220 /** 0221 * Return the current element. 0222 * Similar to the current() function for arrays in PHP 0223 * Required by interface Iterator. 0224 * 0225 * @return Zend_Queue_Message current element from the collection 0226 */ 0227 public function current() 0228 { 0229 return (($this->valid() === false) 0230 ? null 0231 : $this->_data[$this->_pointer]); // return the messages object 0232 } 0233 0234 /** 0235 * Return the identifying key of the current element. 0236 * Similar to the key() function for arrays in PHP. 0237 * Required by interface Iterator. 0238 * 0239 * @return integer 0240 */ 0241 public function key() 0242 { 0243 return $this->_pointer; 0244 } 0245 0246 /** 0247 * Move forward to next element. 0248 * Similar to the next() function for arrays in PHP. 0249 * Required by interface Iterator. 0250 * 0251 * @return void 0252 */ 0253 public function next() 0254 { 0255 ++$this->_pointer; 0256 } 0257 0258 /** 0259 * Check if there is a current element after calls to rewind() or next(). 0260 * Used to check if we've iterated to the end of the collection. 0261 * Required by interface Iterator. 0262 * 0263 * @return bool False if there's nothing more to iterate over 0264 */ 0265 public function valid() 0266 { 0267 return $this->_pointer < count($this); 0268 } 0269 0270 /* 0271 * Countable Implementation 0272 */ 0273 0274 /** 0275 * Returns the number of elements in the collection. 0276 * 0277 * Implements Countable::count() 0278 * 0279 * @return integer 0280 */ 0281 public function count() 0282 { 0283 return count($this->_data); 0284 } 0285 }