Exemplo n.º 1
0
 /**
  * @dataProvider dataProviderForTestGetBest
  */
 public function testGetBest($accept, $priorities, $expected)
 {
     $accept = $this->negotiator->getBest($accept, $priorities);
     if (null === $accept) {
         $this->assertNull($expected);
     } else {
         $this->assertInstanceOf('Negotiation\\AcceptEncoding', $accept);
         $this->assertEquals($expected, $accept->getValue());
     }
 }
Exemplo n.º 2
0
 /**
  * Handles a request to convert it to a response
  *
  * @param Request $request A Request instance
  * @param int     $type    The type of the request
  * @param bool    $catch   Whether to catch exceptions or not
  *
  * @return Response
  *
  * @throws Exception When an Exception occurs during processing
  */
 public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
 {
     $acceptHeader = $request->headers->get('Accept');
     if ($acceptHeader !== null && !empty($this->formatPriorities)) {
         $accept = $this->formatNegotiator->getBest($acceptHeader, $this->formatPriorities);
         $request->attributes->set('_accept', $accept);
         if ($accept !== null) {
             /** @var Accept $accept */
             if (false === strpos($accept->getType(), '*')) {
                 $mimeType = $accept->getType();
                 $format = $this->getFormat($mimeType);
                 $request->attributes->set('_mime_type', $mimeType);
                 $request->attributes->set('_format', $format);
             }
         }
     }
     $acceptLanguageHeader = $request->headers->get('Accept-Language');
     if ($acceptLanguageHeader !== null && !empty($this->languagePriorities)) {
         $acceptLanguage = $this->languageNegotiator->getBest($acceptLanguageHeader, $this->languagePriorities);
         $request->attributes->set('_accept_language', $acceptLanguage);
         if ($acceptLanguage !== null) {
             /** @var AcceptLanguage $acceptLanguage */
             $language = $acceptLanguage->getType();
             $request->attributes->set('_language', $language);
         }
     }
     $acceptEncodingHeader = $request->headers->get('Accept-Encoding');
     if ($acceptEncodingHeader !== null && !empty($this->encodingPriorities)) {
         $acceptEncoding = $this->encodingNegotiator->getBest($acceptEncodingHeader, $this->encodingPriorities);
         $request->attributes->set('_accept_encoding', $acceptEncoding);
         if ($acceptEncoding !== null) {
             /** @var AcceptEncoding $acceptEncoding */
             $encoding = $acceptEncoding->getType();
             $request->attributes->set('_encoding', $encoding);
         }
     }
     $acceptCharsetHeader = $request->headers->get('Accept-Charset');
     if ($acceptCharsetHeader !== null && !empty($this->charsetPriorities)) {
         $acceptCharset = $this->charsetNegotiator->getBest($acceptCharsetHeader, $this->charsetPriorities);
         $request->attributes->set('_accept_charset', $acceptCharset);
         if ($acceptCharset !== null) {
             /** @var AcceptCharset $acceptCharset */
             $charset = $acceptCharset->getType();
             $request->attributes->set('_charset', $charset);
         }
     }
     try {
         $this->decodeBody($request);
     } catch (BadRequestHttpException $exception) {
         if ($catch === true) {
             return $this->handleException($exception, $request);
         }
         throw $exception;
     }
     return $this->kernel->handle($request, $type, $catch);
 }
 public function testGetBestRespectsQualityOfSource()
 {
     $accept = $this->negotiator->getBest('gzip;q=0.7,identity', array('identity;q=0.5', 'gzip;q=0.9'));
     $this->assertInstanceOf('Negotiation\\AcceptEncoding', $accept);
     $this->assertEquals('gzip', $accept->getType());
 }