Response.php 5.4 KB

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