2
0

Messages.php 4.8 KB

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