Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php \Drupal\Core\StackMiddleware\NegotiationMiddleware
  2. 9 core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php \Drupal\Core\StackMiddleware\NegotiationMiddleware

Provides a middleware to determine the content type upon the accept header.

Hierarchy

Expanded class hierarchy of NegotiationMiddleware

3 files declare their use of NegotiationMiddleware
FileServiceProvider.php in core/modules/file/src/FileServiceProvider.php
JsonapiServiceProvider.php in core/modules/jsonapi/src/JsonapiServiceProvider.php
NegotiationMiddlewareTest.php in core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php
1 string reference to 'NegotiationMiddleware'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses NegotiationMiddleware
http_middleware.negotiation in core/core.services.yml
Drupal\Core\StackMiddleware\NegotiationMiddleware

File

core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php, line 12

Namespace

Drupal\Core\StackMiddleware
View source
class NegotiationMiddleware implements HttpKernelInterface {

  /**
   * The wrapped HTTP kernel.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
   */
  protected $app;

  /**
   * Contains a hashmap of format as key and mimetype as value.
   *
   * @var array
   */
  protected $formats = [];

  /**
   * Constructs a new NegotiationMiddleware.
   *
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $app
   *   The wrapper HTTP kernel
   */
  public function __construct(HttpKernelInterface $app) {
    $this->app = $app;
  }

  /**
   * {@inheritdoc}
   */
  public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE) : Response {

    // Register available mime types.
    foreach ($this->formats as $format => $mime_type) {
      $request
        ->setFormat($format, $mime_type);
    }

    // Determine the request format using the negotiator.
    if ($requested_format = $this
      ->getContentType($request)) {
      $request
        ->setRequestFormat($requested_format);
    }
    return $this->app
      ->handle($request, $type, $catch);
  }

  /**
   * Registers a format for a given MIME type.
   *
   * @param string $format
   *   The format.
   * @param string $mime_type
   *   The MIME type.
   *
   * @return $this
   */
  public function registerFormat($format, $mime_type) {
    $this->formats[$format] = $mime_type;
    return $this;
  }

  /**
   * Gets the normalized type of a request.
   *
   * The normalized type is a short, lowercase version of the format, such as
   * 'html', 'json' or 'atom'.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request object from which to extract the content type.
   *
   * @return string
   *   The normalized type of a given request.
   */
  protected function getContentType(Request $request) {

    // AJAX iframe uploads need special handling, because they contain a JSON
    // response wrapped in <textarea>.
    if ($request->request
      ->get('ajax_iframe_upload', FALSE)) {
      return 'iframeupload';
    }
    if ($request->query
      ->has('_format')) {
      return $request->query
        ->get('_format');
    }

    // No format was specified in the request.
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NegotiationMiddleware::$app protected property The wrapped HTTP kernel.
NegotiationMiddleware::$formats protected property Contains a hashmap of format as key and mimetype as value.
NegotiationMiddleware::getContentType protected function Gets the normalized type of a request. 1
NegotiationMiddleware::handle public function
NegotiationMiddleware::registerFormat public function Registers a format for a given MIME type.
NegotiationMiddleware::__construct public function Constructs a new NegotiationMiddleware.