|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00023 require_once 'Zend/Amf/Constants.php'; 00024 00026 require_once 'Zend/Amf/Parse/OutputStream.php'; 00027 00029 require_once 'Zend/Amf/Parse/Amf0/Serializer.php'; 00030 00038 class Zend_Amf_Response 00039 { 00043 protected $_objectEncoding = 0; 00044 00049 protected $_bodies = array(); 00050 00055 protected $_headers = array(); 00056 00060 protected $_outputStream; 00061 00067 public function finalize() 00068 { 00069 $this->_outputStream = new Zend_Amf_Parse_OutputStream(); 00070 $this->writeMessage($this->_outputStream); 00071 return $this; 00072 } 00073 00081 public function writeMessage(Zend_Amf_Parse_OutputStream $stream) 00082 { 00083 $objectEncoding = $this->_objectEncoding; 00084 00085 //Write encoding to start of stream. Preamble byte is written of two byte Unsigned Short 00086 $stream->writeByte(0x00); 00087 $stream->writeByte($objectEncoding); 00088 00089 // Loop through the AMF Headers that need to be returned. 00090 $headerCount = count($this->_headers); 00091 $stream->writeInt($headerCount); 00092 foreach ($this->getAmfHeaders() as $header) { 00093 $serializer = new Zend_Amf_Parse_Amf0_Serializer($stream); 00094 $stream->writeUTF($header->name); 00095 $stream->writeByte($header->mustRead); 00096 $stream->writeLong(Zend_Amf_Constants::UNKNOWN_CONTENT_LENGTH); 00097 if (is_object($header->data)) { 00098 // Workaround for PHP5 with E_STRICT enabled complaining about 00099 // "Only variables should be passed by reference" 00100 $placeholder = null; 00101 $serializer->writeTypeMarker($placeholder, null, $header->data); 00102 } else { 00103 $serializer->writeTypeMarker($header->data); 00104 } 00105 } 00106 00107 // loop through the AMF bodies that need to be returned. 00108 $bodyCount = count($this->_bodies); 00109 $stream->writeInt($bodyCount); 00110 foreach ($this->_bodies as $body) { 00111 $serializer = new Zend_Amf_Parse_Amf0_Serializer($stream); 00112 $stream->writeUTF($body->getTargetURI()); 00113 $stream->writeUTF($body->getResponseURI()); 00114 $stream->writeLong(Zend_Amf_Constants::UNKNOWN_CONTENT_LENGTH); 00115 $bodyData = $body->getData(); 00116 $markerType = ($this->_objectEncoding == Zend_Amf_Constants::AMF0_OBJECT_ENCODING) ? null : Zend_Amf_Constants::AMF0_AMF3; 00117 if (is_object($bodyData)) { 00118 // Workaround for PHP5 with E_STRICT enabled complaining about 00119 // "Only variables should be passed by reference" 00120 $placeholder = null; 00121 $serializer->writeTypeMarker($placeholder, $markerType, $bodyData); 00122 } else { 00123 $serializer->writeTypeMarker($bodyData, $markerType); 00124 } 00125 } 00126 00127 return $this; 00128 } 00129 00135 public function getResponse() 00136 { 00137 return $this->_outputStream->getStream(); 00138 } 00139 00145 public function __toString() 00146 { 00147 return $this->getResponse(); 00148 } 00149 00156 public function addAmfBody(Zend_Amf_Value_MessageBody $body) 00157 { 00158 $this->_bodies[] = $body; 00159 return $this; 00160 } 00161 00167 public function getAmfBodies() 00168 { 00169 return $this->_bodies; 00170 } 00171 00178 public function addAmfHeader(Zend_Amf_Value_MessageHeader $header) 00179 { 00180 $this->_headers[] = $header; 00181 return $this; 00182 } 00183 00189 public function getAmfHeaders() 00190 { 00191 return $this->_headers; 00192 } 00193 00200 public function setObjectEncoding($encoding) 00201 { 00202 $this->_objectEncoding = $encoding; 00203 return $this; 00204 } 00205 }