Messages.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 Custom
  18. * @copyright Copyright (c) 2005-2008 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 14781 2009-04-09 07:07:24Z danlo $
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Queue
  25. * @subpackage Custom
  26. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. /**
  30. * This class uses the SLP_ArrayIterator
  31. * We are interested in overriding unset() to auto delete the message
  32. */
  33. /** Zend_Queue_Message_Iterator */
  34. require_once('Zend/Queue/Message/Iterator.php');
  35. class Custom_Messages
  36. extends Zend_Queue_Message_Iterator
  37. implements ArrayAccess
  38. {
  39. /**
  40. * Constructor
  41. *
  42. * @param array $config ('queue', 'messageClass', 'data'=>array());
  43. */
  44. public function __construct(array $config=array())
  45. {
  46. if (isset($config['queue'])) {
  47. $this->_queue = $config['queue'];
  48. $this->_queueClass = get_class($this->_queue);
  49. $this->_connected = true;
  50. } else {
  51. $this->_connected = false;
  52. }
  53. if (isset($config['messageClass'])) {
  54. $this->_messageClass = $config['messageClass'];
  55. }
  56. if (isset($config['data']) && ! is_array($config['data'])) {
  57. /**
  58. * @see Zend_Queue_Exception
  59. */
  60. require_once 'Zend/Queue/Exception.php';
  61. throw new Zend_Queue_Exception('array configuration must have $config[\'data\'] = array');
  62. }
  63. // load the message class
  64. $class = $this->_messageClass;
  65. Zend_Loader::loadClass($class);
  66. if (isset($config['data'])) {
  67. // for each of the messages
  68. foreach($config['data'] as $i => $data) {
  69. // construct the message parameters
  70. $message = array('data' => $data);
  71. // If queue has not been set, then use the default.
  72. if (empty($message['queue'])) {
  73. $message['queue'] = $this->_queue;
  74. }
  75. // construct the message and add it to _data[];
  76. $this->_data[] = new $class($message);
  77. }
  78. }
  79. }
  80. /**
  81. * Our destruct will delete all the messages in the queue
  82. *
  83. * Notice: if anything throws a message we are doomed.
  84. * You cannot throw an error in an destructor
  85. */
  86. public function __destruct()
  87. {
  88. if ($this->_connected) {
  89. foreach ($this->_data as $i => $value) {
  90. $value->delete(false);
  91. }
  92. } else {
  93. unset($this->_data);
  94. }
  95. }
  96. /*
  97. * ArrayIterator
  98. */
  99. /**
  100. * @see SPL ArrayIterator::append
  101. */
  102. public function append($value) {
  103. $this->_data[] = $value;
  104. }
  105. /*
  106. * ArrayAccess
  107. */
  108. /**
  109. * @see SPL ArrayAccess::offsetSet
  110. */
  111. public function offsetSet($offset, $value) {
  112. if (! $value instanceof Custom_Message) {
  113. $msg = '$value must be a child or an instance of Custom_Messag';
  114. /**
  115. * @see Zend_Queue_Exception
  116. */
  117. require_once 'Zend/Queue/Exception.php';
  118. throw new Zend_Queue_Exception($msg);
  119. }
  120. $this->_data[$offset] = $value;
  121. return $value;
  122. }
  123. /**
  124. * @see SPL ArrayAccess::offsetGet
  125. */
  126. public function offsetGet($offset) {
  127. return $this->_data[$offset];
  128. }
  129. /**
  130. * @see SPL ArrayAccess::offsetUnset
  131. */
  132. public function offsetUnset($offset) {
  133. if (! $this->_connected) {
  134. $msg = 'Cannot delete message after serialization';
  135. /**
  136. * @see Zend_Queue_Exception
  137. */
  138. require_once 'Zend/Queue/Exception.php';
  139. throw new Zend_Queue_Exception($msg);
  140. }
  141. $this->_data[$offset]->delete(); // Custom_Message added this function
  142. unset($this->_data[$offset]);
  143. }
  144. /**
  145. * @see SPL ArrayAccess::offsetExists
  146. */
  147. public function offsetExists($offset) {
  148. return isSet($this->_data[$offset]);
  149. }
  150. /*
  151. * SeekableIterator implementation
  152. */
  153. /**
  154. * @see SPL SeekableIterator::seek
  155. */
  156. public function seek($index) {
  157. $this->_pointer = $index;
  158. }
  159. }