You are here

class FormatSetter in JSON:API 8

Same name and namespace in other branches
  1. 8.2 src/StackMiddleware/FormatSetter.php \Drupal\jsonapi\StackMiddleware\FormatSetter

Sets the 'api_json' format on all requests to JSON API-managed routes.

@internal

Hierarchy

  • class \Drupal\jsonapi\StackMiddleware\FormatSetter implements \Symfony\Component\HttpKernel\HttpKernelInterface

Expanded class hierarchy of FormatSetter

1 string reference to 'FormatSetter'
jsonapi.services.yml in ./jsonapi.services.yml
jsonapi.services.yml
1 service uses FormatSetter
jsonapi.http_middleware.format_setter in ./jsonapi.services.yml
Drupal\jsonapi\StackMiddleware\FormatSetter

File

src/StackMiddleware/FormatSetter.php, line 14

Namespace

Drupal\jsonapi\StackMiddleware
View source
class FormatSetter implements HttpKernelInterface {

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

  /**
   * The JSON API base path.
   *
   * @var string
   */
  protected $jsonApiBasePath;

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

  /**
   * {@inheritdoc}
   */
  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
    if ($this
      ->isJsonApiRequest($request)) {
      $request
        ->setRequestFormat('api_json');
    }
    return $this->httpKernel
      ->handle($request, $type, $catch);
  }

  /**
   * Checks whether the current request is a JSON API request.
   *
   * Inspects:
   * - request parameters
   * - request path (uses a heuristic, because e.g. language negotiation may use
   *   path prefixes)
   * - 'Accept' request header value.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   *
   * @return bool
   *   Whether the current request is a JSON API request.
   */
  protected function isJsonApiRequest(Request $request) {
    $is_jsonapi_route = $request->attributes
      ->get(Routes::JSON_API_ROUTE_FLAG_KEY);

    // Check if the path indicates that the request intended to target a JSON
    // API route (but may not have because of an incorrect parameter or minor
    // typo).
    $jsonapi_route_intended = strpos($request
      ->getPathInfo(), "{$this->jsonApiBasePath}/") !== FALSE;

    // Check if the 'Accept' header includes the JSON API MIME type.
    $request_has_jsonapi_media_type = count(array_filter($request
      ->getAcceptableContentTypes(), function ($accept) {
      return strpos($accept, 'application/vnd.api+json') === 0;
    }));
    return $is_jsonapi_route || $jsonapi_route_intended && $request_has_jsonapi_media_type;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormatSetter::$httpKernel protected property The wrapped HTTP kernel.
FormatSetter::$jsonApiBasePath protected property The JSON API base path.
FormatSetter::handle public function Handles a Request to convert it to a Response.
FormatSetter::isJsonApiRequest protected function Checks whether the current request is a JSON API request.
FormatSetter::__construct public function Constructs a FormatSetter object.