You are here

public function ContentHubEntityRequestHandler::handle in Acquia Content Hub 8

Handles a REST API request.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match.

\Symfony\Component\HttpFoundation\Request $request: The HTTP request object.

\Drupal\rest\RestResourceConfigInterface $_rest_resource_config: The REST resource config entity.

Return value

\Drupal\rest\ResourceResponseInterface|\Symfony\Component\HttpFoundation\Response The REST resource response.

Overrides RequestHandler::handle

File

src/Controller/ContentHubEntityRequestHandler.php, line 104

Class

ContentHubEntityRequestHandler
Decorates the REST module's RequestHandler.

Namespace

Drupal\acquia_contenthub\Controller

Code

public function handle(RouteMatchInterface $route_match, Request $request, RestResourceConfigInterface $_rest_resource_config = NULL) {

  // We only support one method, one format, and use one of the derivatives of
  // only one resource plugin. (We receive the exact plugin ID via the route
  // defaults).
  $method = 'GET';
  $format = 'acquia_contenthub_cdf';

  // Force the acquia_contenthub_cdf format.
  if ($request
    ->getRequestFormat() !== $format) {
    $request
      ->setRequestFormat($format);
  }
  $resource = $this->resourcePluginManager
    ->createInstance($route_match
    ->getRouteObject()
    ->getDefault('_acquia_content_hub_rest_resource_plugin_id'));

  // EVERYTHING BELOW THIS IS MERE DUPLICATION OF THE DECORATED CLASS IN THE
  // MOST MINIMAL WAY POSSIBLE AND REMOVING THE DEPENDENCY ON CONFIG ENTITIES.
  // Determine the request parameters that should be passed to the resource
  // plugin.
  $route_parameters = $route_match
    ->getParameters();
  $parameters = [];

  // Filter out all internal parameters starting with "_".
  foreach ($route_parameters as $key => $parameter) {
    if ($key[0] !== '_') {
      $parameters[] = $parameter;
    }
  }

  // Invoke the operation on the resource plugin.
  $response = call_user_func_array([
    $resource,
    $method,
  ], array_merge($parameters, [
    $request,
  ]));

  // Render response.
  $data = $response
    ->getResponseData();
  $context = new RenderContext();
  if (!$context
    ->isEmpty()) {
    $response
      ->addCacheableDependency($context
      ->pop());
  }

  // Adding cacheable dependency on the config entity for this particular
  // entity type.
  // All requests served by this route will have the cache tag of the config
  // entity that provided the response so if that config entity changes, then
  // this will automatically invalidate the caches of all responses
  // associated with it.
  $entity_type_id = $data
    ->getEntityTypeId();
  if ($content_hub_config_entity = $this->entityManager
    ->getContentHubEntityTypeConfigurationEntity($entity_type_id)) {
    $response
      ->addCacheableDependency($content_hub_config_entity);
  }
  $response->headers
    ->set('Content-Type', $request
    ->getMimeType($format));
  return $response;
}