示例#1
0
 protected function doKernelResponse(Request $request, Response $response)
 {
     if (!$response instanceof DataResponse) {
         return;
     }
     $routeName = $request->attributes->get('_route');
     $route = $this->routes->get($routeName);
     if (!$route) {
         return;
     }
     $acceptedFormat = $route->getOption(RouteOptions::ACCEPTED_FORMAT);
     if (!$acceptedFormat) {
         $response->setContent('');
         $response->setStatusCode(406);
     }
     if ($this->encoder->supportsEncoding($acceptedFormat) && $acceptedFormat === 'json') {
         $contentType = $request->getMimeType($acceptedFormat);
         $jsonResponse = new JsonResponse($response->getContent());
         $response->setContent($jsonResponse->getContent());
         $response->headers->set('Content-Type', $contentType);
     } elseif ($this->encoder->supportsEncoding($acceptedFormat)) {
         $contentType = $request->getMimeType($acceptedFormat);
         $content = $this->encoder->encode($response->getContent(), $acceptedFormat);
         $response->setContent($content);
         $response->headers->set('Content-Type', $contentType);
     }
 }
示例#2
0
 /**
  * @param string                            $format
  * @param EncoderInterface|DecoderInterface $encoder
  * @param int                               $iterations
  * @param mixed                             $data
  *
  * @return self
  */
 public static function create($format, $encoder, $iterations, $data)
 {
     $bench = new self();
     /*Raw****************************************************************************/
     $bench->format = $format;
     list($keysLength, $dataLength) = $bench->measure($data);
     $bench->dataTime = $bench->measureTime($iterations, array($bench, 'measure'), array($data));
     $bench->dataLength = $keysLength + $dataLength;
     /*Encoded************************************************************************/
     $encoded = $encoder->encode($data, $format);
     $bench->encodedLength = strlen($encoded);
     $bench->encodingTime = $bench->measureTime($iterations, array($encoder, 'encode'), array($data, $format));
     $bench->decodingTime = $bench->measureTime($iterations, array($encoder, 'decode'), array($encoded, $format));
     /*GZip***************************************************************************/
     $gzipEncoded = gzencode($encoded);
     $bench->gzipEncodedLength = strlen($gzipEncoded);
     $bench->gzipEncodingTime = $bench->encodingTime + $bench->measureTime($iterations, 'gzencode', array($encoded));
     $bench->gzipDecodingTime = $bench->decodingTime + $bench->measureTime($iterations, 'gzdecode', array($gzipEncoded));
     /*BZip***************************************************************************/
     $bzipEncoded = bzcompress($encoded);
     $bench->bzipEncodedLength = strlen($bzipEncoded);
     $bench->bzipEncodingTime = $bench->encodingTime + $bench->measureTime($iterations, 'bzcompress', array($encoded));
     $bench->bzipDecodingTime = $bench->decodingTime + $bench->measureTime($iterations, 'bzdecompress', array($bzipEncoded));
     return $bench;
 }
 /**
  * Writes the labels row of the XML file
  */
 protected function writeLabels()
 {
     fwrite($this->handler, $this->encoder->encode($this->labels, $this->format));
 }