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