You are here

final class ResourceResponseFactory in JSON:API Resources 8

Creates JSON:API response objects.

@internal Do not use this factory directly. Use \Drupal\jsonapi\Resource\ResourceBase::createJsonapiResponse() instead.

Hierarchy

Expanded class hierarchy of ResourceResponseFactory

1 file declares its use of ResourceResponseFactory
ResourceBase.php in src/Resource/ResourceBase.php
1 string reference to 'ResourceResponseFactory'
jsonapi_resources.services.yml in ./jsonapi_resources.services.yml
jsonapi_resources.services.yml
1 service uses ResourceResponseFactory
jsonapi_resources.resource_response_factory in ./jsonapi_resources.services.yml
Drupal\jsonapi_resources\Unstable\ResourceResponseFactory

File

src/Unstable/ResourceResponseFactory.php, line 26

Namespace

Drupal\jsonapi_resources\Unstable
View source
final class ResourceResponseFactory {

  /**
   * The include resolver.
   *
   * @var \Drupal\jsonapi\IncludeResolver
   */
  protected $includeResolver;

  /**
   * ResourceResponseFactory constructor.
   *
   * @param \Drupal\jsonapi\IncludeResolver $include_resolver
   *   The include resolver.
   */
  public function __construct(IncludeResolver $include_resolver) {
    $this->includeResolver = $include_resolver;
  }

  /**
   * Builds a response with the appropriate wrapped document.
   *
   * @param \Drupal\jsonapi\JsonApiResource\ResourceObjectData $data
   *   The data to wrap.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request object.
   * @param int $response_code
   *   The response code.
   * @param array $headers
   *   An array of response headers.
   * @param \Drupal\jsonapi\JsonApiResource\LinkCollection $links
   *   The URLs to which to link. A 'self' link is added automatically.
   * @param array $meta
   *   (optional) The top-level metadata.
   *
   * @return \Drupal\jsonapi\ResourceResponse
   *   The response.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function create(ResourceObjectData $data, Request $request, $response_code = 200, array $headers = [], LinkCollection $links = NULL, array $meta = []) {
    $links = $links ?: new LinkCollection([]);
    if (!$links
      ->hasLinkWithKey('self')) {
      $self_link = new Link(new CacheableMetadata(), Url::fromUri($request
        ->getUri()), 'self');
      $links = $links
        ->withLink('self', $self_link);
    }
    $includes = $this
      ->getIncludes($request, $data);

    // \Drupal\jsonapi\ResourceResponse no longer implements
    // CacheableResponseInterface in Drupal 9.1.
    // Drupal\jsonapi\CacheableResourceResponse has ben added for cacheable
    // responses. Keep compatibility with Drupal < 9.1.
    // See https://www.drupal.org/node/3163310
    $document = new JsonApiDocumentTopLevel($data, $includes, $links, $meta);
    $response = class_exists('\\Drupal\\jsonapi\\CacheableResourceResponse') ? new CacheableResourceResponse($document, $response_code, $headers) : new ResourceResponse($document, $response_code, $headers);

    // Make sure that different sparse fieldsets are cached differently.
    $cache_contexts[] = 'url.query_args:fields';

    // Make sure that different sets of includes are cached differently.
    $cache_contexts[] = 'url.query_args:include';
    $cacheability = (new CacheableMetadata())
      ->addCacheContexts($cache_contexts);
    $response
      ->addCacheableDependency($cacheability);
    return $response;
  }

  /**
   * Gets includes for the given response data.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request object.
   * @param \Drupal\jsonapi\JsonApiResource\ResourceObject|\Drupal\jsonapi\JsonApiResource\ResourceObjectData $data
   *   The response data from which to resolve includes.
   *
   * @return \Drupal\jsonapi\JsonApiResource\IncludedData
   *   A Data object to be included or a NullData object if the request does not
   *   specify any include paths.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  private function getIncludes(Request $request, $data) : IncludedData {
    assert($data instanceof ResourceObject || $data instanceof ResourceObjectData);
    return $request->query
      ->has('include') && ($include_parameter = $request->query
      ->get('include')) && !empty($include_parameter) ? $this->includeResolver
      ->resolve($data, $include_parameter) : new NullIncludedData();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResourceResponseFactory::$includeResolver protected property The include resolver.
ResourceResponseFactory::create public function Builds a response with the appropriate wrapped document.
ResourceResponseFactory::getIncludes private function Gets includes for the given response data.
ResourceResponseFactory::__construct public function ResourceResponseFactory constructor.