PageRenderTime 64ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Amf/Response/StreamResponse.php

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