Response.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /** @see Zend_Amf_Constants */
  22. require_once 'Zend/Amf/Constants.php';
  23. /** @see Zend_Amf_Parse_OutputStream */
  24. require_once 'Zend/Amf/Parse/OutputStream.php';
  25. /** @see 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-2010 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. if (is_object($header->data)) {
  87. // Workaround for PHP5 with E_STRICT enabled complaining about "Only variables should be passed by reference"
  88. $placeholder = null;
  89. $serializer->writeTypeMarker($placeholder, null, $header->data);
  90. } else {
  91. $serializer->writeTypeMarker($header->data);
  92. }
  93. }
  94. // loop through the AMF bodies that need to be returned.
  95. $bodyCount = count($this->_bodies);
  96. $stream->writeInt($bodyCount);
  97. foreach ($this->_bodies as $body) {
  98. $serializer = new Zend_Amf_Parse_Amf0_Serializer($stream);
  99. $stream->writeUTF($body->getTargetURI());
  100. $stream->writeUTF($body->getResponseURI());
  101. $stream->writeLong(Zend_Amf_Constants::UNKNOWN_CONTENT_LENGTH);
  102. $bodyData = $body->getData();
  103. $markerType = ($this->_objectEncoding == Zend_Amf_Constants::AMF0_OBJECT_ENCODING) ? null : Zend_Amf_Constants::AMF0_AMF3;
  104. if (is_object($bodyData)) {
  105. // Workaround for PHP5 with E_STRICT enabled complaining about "Only variables should be passed by reference"
  106. $placeholder = null;
  107. $serializer->writeTypeMarker($placeholder, $markerType, $bodyData);
  108. } else {
  109. $serializer->writeTypeMarker($bodyData, $markerType);
  110. }
  111. }
  112. return $this;
  113. }
  114. /**
  115. * Return the output stream content
  116. *
  117. * @return string The contents of the output stream
  118. */
  119. public function getResponse()
  120. {
  121. return $this->_outputStream->getStream();
  122. }
  123. /**
  124. * Return the output stream content
  125. *
  126. * @return string
  127. */
  128. public function __toString()
  129. {
  130. return $this->getResponse();
  131. }
  132. /**
  133. * Add an AMF body to be sent to the Flash Player
  134. *
  135. * @param Zend_Amf_Value_MessageBody $body
  136. * @return Zend_Amf_Response
  137. */
  138. public function addAmfBody(Zend_Amf_Value_MessageBody $body)
  139. {
  140. $this->_bodies[] = $body;
  141. return $this;
  142. }
  143. /**
  144. * Return an array of AMF bodies to be serialized
  145. *
  146. * @return array
  147. */
  148. public function getAmfBodies()
  149. {
  150. return $this->_bodies;
  151. }
  152. /**
  153. * Add an AMF Header to be sent back to the flash player
  154. *
  155. * @param Zend_Amf_Value_MessageHeader $header
  156. * @return Zend_Amf_Response
  157. */
  158. public function addAmfHeader(Zend_Amf_Value_MessageHeader $header)
  159. {
  160. $this->_headers[] = $header;
  161. return $this;
  162. }
  163. /**
  164. * Retrieve attached AMF message headers
  165. *
  166. * @return array Array of Zend_Amf_Value_MessageHeader objects
  167. */
  168. public function getAmfHeaders()
  169. {
  170. return $this->_headers;
  171. }
  172. /**
  173. * Set the AMF encoding that will be used for serialization
  174. *
  175. * @param int $encoding
  176. * @return Zend_Amf_Response
  177. */
  178. public function setObjectEncoding($encoding)
  179. {
  180. $this->_objectEncoding = $encoding;
  181. return $this;
  182. }
  183. }