Response.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_Amf
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Amf_Constants */
  21. require_once 'Zend/Amf/Constants.php';
  22. /** Zend_Amf_Parse_OutputStream */
  23. require_once 'Zend/Amf/Parse/OutputStream.php';
  24. /** Zend_Amf_Parse_Amf0_Serializer */
  25. require_once 'Zend/Amf/Parse/Amf0/Serializer.php';
  26. /**
  27. * Handles converting the PHP object ready for response back into AMF
  28. *
  29. * @package Zend_Amf
  30. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Amf_Response
  34. {
  35. /**
  36. * @var int Object encoding for response
  37. */
  38. protected $_objectEncoding = 0;
  39. /**
  40. * Array of Zend_Amf_Value_MessageBody objects
  41. * @var array
  42. */
  43. protected $_bodies = array();
  44. /**
  45. * Array of Zend_Amf_Value_MessageHeader objects
  46. * @var array
  47. */
  48. protected $_headers = array();
  49. /**
  50. * @var Zend_Amf_Parse_OutputStream
  51. */
  52. protected $_outputStream;
  53. /**
  54. * Instantiate new output stream and start serialization
  55. *
  56. * @return Zend_Amf_Response
  57. */
  58. public function finalize()
  59. {
  60. $this->_outputStream = new Zend_Amf_Parse_OutputStream();
  61. $this->writeMessage($this->_outputStream);
  62. return $this;
  63. }
  64. /**
  65. * Serialize the PHP data types back into Actionscript and
  66. * create and AMF stream.
  67. *
  68. * @param Zend_Amf_Parse_OutputStream $stream
  69. * @return Zend_Amf_Response
  70. */
  71. public function writeMessage(Zend_Amf_Parse_OutputStream $stream)
  72. {
  73. $objectEncoding = $this->_objectEncoding;
  74. //Write encoding to start of stream. Preamble byte is written of two byte Unsigned Short
  75. $stream->writeByte(0x00);
  76. $stream->writeByte($objectEncoding);
  77. // Loop through the AMF Headers that need to be returned.
  78. $headerCount = count($this->_headers);
  79. $stream->writeInt($headerCount);
  80. foreach ($this->getAmfHeaders() as $header) {
  81. $serializer = new Zend_Amf_Parse_Amf0_Serializer($stream);
  82. $stream->writeUTF($header->name);
  83. $stream->writeByte($header->mustRead);
  84. $stream->writeLong(Zend_Amf_Constants::UNKNOWN_CONTENT_LENGTH);
  85. $serializer->writeTypeMarker($header->data);
  86. }
  87. // loop through the AMF bodies that need to be returned.
  88. $bodyCount = count($this->_bodies);
  89. $stream->writeInt($bodyCount);
  90. foreach ($this->_bodies as $body) {
  91. $serializer = new Zend_Amf_Parse_Amf0_Serializer($stream);
  92. $stream->writeUTF($body->getTargetURI());
  93. $stream->writeUTF($body->getResponseURI());
  94. $stream->writeLong(Zend_Amf_Constants::UNKNOWN_CONTENT_LENGTH);
  95. if($this->_objectEncoding == Zend_Amf_Constants::AMF0_OBJECT_ENCODING) {
  96. $serializer->writeTypeMarker($body->getData());
  97. } else {
  98. // Content is AMF3
  99. $serializer->writeTypeMarker($body->getData(),Zend_Amf_Constants::AMF0_AMF3);
  100. }
  101. }
  102. return $this;
  103. }
  104. /**
  105. * Return the output stream content
  106. *
  107. * @return string The contents of the output stream
  108. */
  109. public function getResponse()
  110. {
  111. return $this->_outputStream->getStream();
  112. }
  113. /**
  114. * Return the output stream content
  115. *
  116. * @return string
  117. */
  118. public function __toString()
  119. {
  120. return $this->getResponse();
  121. }
  122. /**
  123. * Add an AMF body to be sent to the Flash Player
  124. *
  125. * @param Zend_Amf_Value_MessageBody $body
  126. * @return Zend_Amf_Response
  127. */
  128. public function addAmfBody(Zend_Amf_Value_MessageBody $body)
  129. {
  130. $this->_bodies[] = $body;
  131. return $this;
  132. }
  133. /**
  134. * Return an array of AMF bodies to be serialized
  135. *
  136. * @return array
  137. */
  138. public function getAmfBodies()
  139. {
  140. return $this->_bodies;
  141. }
  142. /**
  143. * Add an AMF Header to be sent back to the flash player
  144. *
  145. * @param Zend_Amf_Value_MessageHeader $header
  146. * @return Zend_Amf_Response
  147. */
  148. public function addAmfHeader(Zend_Amf_Value_MessageHeader $header)
  149. {
  150. $this->_headers[] = $header;
  151. return $this;
  152. }
  153. /**
  154. * Retrieve attached AMF message headers
  155. *
  156. * @return array Array of Zend_Amf_Value_MessageHeader objects
  157. */
  158. public function getAmfHeaders()
  159. {
  160. return $this->_headers;
  161. }
  162. /**
  163. * Set the AMF encoding that will be used for serialization
  164. *
  165. * @param int $encoding
  166. * @return Zend_Amf_Response
  167. */
  168. public function setObjectEncoding($encoding)
  169. {
  170. $this->_objectEncoding = $encoding;
  171. return $this;
  172. }
  173. }