Пример #1
0
 /**
  * Print string to the ouput stream
  *
  * @param Entity\Response | NULL $response  On NULL print No Content
  * @return void
  */
 public function printResponse(Collection\Response $response = null)
 {
     // Response NULL means no content
     if ($response->count() === 0) {
         if (!headers_sent()) {
             header('No Content', null, 204);
         }
         return;
     }
     // Set HTTP headers
     if (!headers_sent()) {
         header('OK', null, 200);
         header('Content-Type: application/json');
         //@TODO correct status code
     }
     // print json encoded string
     $response = $this->createRawResponseFromResponse($response);
     $response = $this->protocolLayerStack->handleResponse($response);
     echo $response;
 }
Пример #2
0
 /**
  * Parse the result given by the JSON RPC Server
  *
  * @param Collection\Request $request
  * @param mixed $result
  * @param mixed $info
  * @return Collection\Response
  */
 protected function parseResponse(Collection\Request $request, $result, $info)
 {
     // Rewind to collection to start from the beginning
     $request->rewind();
     if (empty($result) && $request->isBatch() === false && $request->current()->getId() !== null) {
         throw new Exception\InvalidResponse('Client parse error', static::CLIENT_PARSE_ERROR);
     }
     // First go through the layer stack
     $result = $this->protocolLayerStack->handleResponse($result);
     $json = json_decode($result, true);
     if (is_array($json) === false) {
         throw new Exception\InvalidResponse('Client parse error', static::CLIENT_PARSE_ERROR);
     }
     // Check varaibles
     $hasIdKey = array_key_exists('id', $json);
     $isBatch = $request->isBatch();
     // Check if we have a batch result
     if ($isBatch === true && $hasIdKey === true) {
         throw new Exception\InvalidResponse('Client expected batch result', static::CLIENT_EXPECTED_BATCH);
         // check if we have single result
     } else {
         if ($isBatch === false && $hasIdKey === false) {
             throw new Exception\InvalidResponse('Client expected single result', static::CLIENT_EXPECTED_SINGLE);
         }
     }
     // No batch, so we make it one
     if ($isBatch === false) {
         $json = array($json);
     }
     // Create collection response
     $responses = new Collection\Response();
     // Loop through all the responses
     foreach ($json as $rawResponse) {
         $response = new Entity\Response();
         // Fetch the items from the response array
         $id = $this->getArrayItem('id', $rawResponse);
         $jsonrpc = $this->getArrayItem('jsonrpc', $rawResponse);
         $result = $this->getArrayItem('result', $rawResponse, null);
         $error = $this->getArrayItem('error', $rawResponse, null);
         // Set general data
         $response->setId($id)->setJsonrpc($jsonrpc);
         // Determine which item to set
         if ($error === null) {
             $response->setResult($result);
         } else {
             // hydrate error information
             $errorObject = new Entity\Error();
             $errorObject->setCode($this->getArrayItem('code', $error));
             $errorObject->setData($this->getArrayItem('data', $error));
             $errorObject->setMessage($this->getArrayItem('message', $error));
             $response->setError($errorObject);
         }
         // When callback has been registered use this
         if ($request->hasIdCallback($response->getId())) {
             $request->performCallback($response);
         }
         // append the response to the collection
         $responses->append($response);
     }
     // return the collection
     return $responses;
 }
 public function testHandleRequest()
 {
     $stack = new ProtocolLayerStack();
     $layerA = new \ClientProtocolLayer();
     $layerB = new \ClientProtocolLayer();
     $stack->addLayer($layerA, ProtocolLayerStack::PLACEMENT_TOP);
     $stack->addLayer($layerB, ProtocolLayerStack::PLACEMENT_TOP);
     $resultA = $stack->handleRequest('hello');
     $resultB = $stack->handleRequest('something');
     $this->assertEquals('world', $resultA);
     $this->assertEquals('something', $resultB);
 }