Example #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);
     }
 }
Example #2
0
 /**
  * Attempts to retrieve the format from the header identified by $headerName which is found in the $request object.
  * A default value will be returned if the format can not be retrieved.
  * As opposed to the getFormat() method, this method will also check if $serializer supports the retrieved
  * format. If it doesn't, an exception is thrown.
  * @param Request $request
  * @param string $headerName
  * @param EncoderInterface $serializer
  * @return string
  */
 public function getSupportedFormat(Request $request, $headerName, EncoderInterface $serializer)
 {
     $format = $this->parseFormatFromHeader($request, $headerName);
     if (!$serializer->supportsEncoding($format)) {
         throw new UnsupportedFormatException(sprintf('Format %s is unsupported', $format), UnsupportedFormatException::CODE);
     }
     return $format;
 }
 /**
  * @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;
 }
Example #4
0
 public function addEncoder($format, EncoderInterface $encoder)
 {
     $this->encoders[$format] = $encoder;
     $encoder->setSerializer($this);
 }
Example #5
0
 /**
  * @param string           $format  format name
  * @param EncoderInterface $encoder
  */
 public function setEncoder($format, EncoderInterface $encoder)
 {
     $this->encoders[$format] = $encoder;
     if ($encoder instanceof SerializerAwareInterface) {
         $encoder->setSerializer($this);
     }
 }
 /**
  * Writes the labels row of the XML file
  */
 protected function writeLabels()
 {
     fwrite($this->handler, $this->encoder->encode($this->labels, $this->format));
 }