You are here

FormatSetter.php in JSON:API 8.2

Same filename and directory in other branches
  1. 8 src/StackMiddleware/FormatSetter.php

File

src/StackMiddleware/FormatSetter.php
View source
<?php

namespace Drupal\jsonapi\StackMiddleware;

use Symfony\Component\HttpFoundation\AcceptHeader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
 * Sets the 'api_json' for requests with a JSON:API Content-Type header.
 *
 * @internal JSON:API maintains no PHP API since its API is the HTTP API. This
 *   class may change at any time and this will break any dependencies on it.
 *
 * @see https://www.drupal.org/project/jsonapi/issues/3032787
 * @see jsonapi.api.php
 */
final class FormatSetter implements HttpKernelInterface {

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

  /**
   * Constructs a FormatSetter object.
   *
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
   *   The decorated kernel.
   */
  public function __construct(HttpKernelInterface $http_kernel) {
    $this->httpKernel = $http_kernel;
  }

  /**
   * {@inheritdoc}
   */
  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
    $accepted = AcceptHeader::fromString($request->headers
      ->get('Accept'));
    if ($accepted
      ->get('application/vnd.api+json')) {
      $request
        ->setRequestFormat('api_json');
    }
    return $this->httpKernel
      ->handle($request, $type, $catch);
  }

}

Classes

Namesort descending Description
FormatSetter Sets the 'api_json' for requests with a JSON:API Content-Type header.