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
- class \Drupal\jsonapi_resources\Unstable\ResourceResponseFactory
Expanded class hierarchy of ResourceResponseFactory
1 file declares its use of ResourceResponseFactory
- ResourceBase.php in src/
Resource/ ResourceBase.php
1 string reference to 'ResourceResponseFactory'
1 service uses ResourceResponseFactory
File
- src/
Unstable/ ResourceResponseFactory.php, line 26
Namespace
Drupal\jsonapi_resources\UnstableView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ResourceResponseFactory:: |
protected | property | The include resolver. | |
ResourceResponseFactory:: |
public | function | Builds a response with the appropriate wrapped document. | |
ResourceResponseFactory:: |
private | function | Gets includes for the given response data. | |
ResourceResponseFactory:: |
public | function | ResourceResponseFactory constructor. |