function JsonapiMaintenanceModeSubscriber::onMaintenanceModeRequest

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/EventSubscriber/JsonapiMaintenanceModeSubscriber.php \Drupal\jsonapi\EventSubscriber\JsonapiMaintenanceModeSubscriber::onMaintenanceModeRequest()
  2. 11.x core/modules/jsonapi/src/EventSubscriber/JsonapiMaintenanceModeSubscriber.php \Drupal\jsonapi\EventSubscriber\JsonapiMaintenanceModeSubscriber::onMaintenanceModeRequest()

Returns response when site is in maintenance mode and user is not exempt.

Parameters

\Symfony\Component\HttpKernel\Event\RequestEvent $event: The event to process.

File

core/modules/jsonapi/src/EventSubscriber/JsonapiMaintenanceModeSubscriber.php, line 70

Class

JsonapiMaintenanceModeSubscriber
Maintenance mode subscriber for JSON:API requests.

Namespace

Drupal\jsonapi\EventSubscriber

Code

public function onMaintenanceModeRequest(RequestEvent $event) {
  $request = $event->getRequest();
  if ($request->getRequestFormat() !== 'api_json') {
    return;
  }
  // Retry-After will be random within a range defined in jsonapi settings.
  // The goals are to keep it short and to reduce the thundering herd problem.
  $header_settings = $this->config
    ->get('jsonapi.settings')
    ->get('maintenance_header_retry_seconds');
  $retry_after_time = rand($header_settings['min'], $header_settings['max']);
  $http_exception = new HttpException(503, $this->maintenanceMode
    ->getSiteMaintenanceMessage());
  $document = new JsonApiDocumentTopLevel(new ErrorCollection([
    $http_exception,
  ]), new NullIncludedData(), new LinkCollection([]));
  $response = new ResourceResponse($document, $http_exception->getStatusCode(), [
    'Content-Type' => 'application/vnd.api+json',
    'Retry-After' => $retry_after_time,
  ]);
  // Calling RequestEvent::setResponse() also stops propagation of event.
  $event->setResponse($response);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.