Message.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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: Message.php 14777 2009-04-09 04:40:39Z danlo $
  21. */
  22. /**
  23. * Class for custom messages
  24. *
  25. * @category Zend
  26. * @package Zend_Queue
  27. * @subpackage Custom
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. /**
  32. * We want be able to delete messages and we to serialize objects
  33. * via a getBody() and setBody()
  34. *
  35. * This is different that doing the serialization directly next to the object
  36. * because you may want to update getBody() and setBody() to do a json
  37. * conversion instead of a php serialize
  38. *
  39. * You could also just overload the Zend_Queue::send() and Zend_Queue::receive()
  40. * functions to do the serialization/json encoding, but I wanted to give an
  41. * good example that overloaded near everything except for the adapter.
  42. */
  43. class Custom_Message extends Zend_Queue_Message
  44. {
  45. /**
  46. * We adjusted the constructor to accept both an array and an object.
  47. */
  48. public function __construct($mixed) {
  49. // we still have to support the code in Zend_Queue::receive that
  50. // passes in an array
  51. if (is_array($mixed)) {
  52. parent::__construct($mixed);
  53. } elseif (is_object($mixed)) {
  54. $this->setBody($mixed);
  55. $this->_connected = false;
  56. }
  57. }
  58. /**
  59. * We need to get the underlying body as a string
  60. *
  61. * @return string
  62. */
  63. public function __toString() {
  64. return $this->_data['body'];
  65. }
  66. /**
  67. * Sets the message body
  68. *
  69. * @param serializable $mixed
  70. */
  71. public function setBody($mixed)
  72. {
  73. $this->_data['body'] = serialize($mixed);
  74. }
  75. /**
  76. * Gets the message body
  77. *
  78. * @return $mixed
  79. */
  80. public function getBody()
  81. {
  82. return unserialize($this->_data['body']);
  83. }
  84. /**
  85. * Deletes the message.
  86. *
  87. * Note you cannot be disconnected from queue.
  88. *
  89. * $throw is set to to true, because most of the time you want to know if
  90. * there is an error. However, in Custom_Messages::__destruct() exceptions
  91. * cannot be thrown.
  92. *
  93. * These does not create a circular reference loop. Because deleteMessage
  94. * asks the queue service to delete the message, the message located here
  95. * is NOT deleted.
  96. *
  97. * @param boolean $throw defaults to true. Throw a message if there is an error
  98. *
  99. * @throws Zend_Queue_Exception if not connected
  100. */
  101. public function delete($throw = true)
  102. {
  103. if ($this->_connected) {
  104. if ($this->getQueue()->getAdapter()->isSupported('deleteMessage')) {
  105. $this->getQueue()->deleteMessage($this);
  106. }
  107. } elseif ($throw) {
  108. /**
  109. * @see Zend_Queue_Exception
  110. */
  111. require_once 'Zend/Queue/Exception.php';
  112. throw new Zend_Queue_Exception('Disconnected from queue. Cannot delete message from queue.');
  113. }
  114. }
  115. }