jsonapi.services.yml | 1 - src/Controller/EntityResource.php | 40 ++++++++++++++++----------------------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/jsonapi.services.yml b/jsonapi.services.yml index 7336d72..841b72c 100644 --- a/jsonapi.services.yml +++ b/jsonapi.services.yml @@ -161,7 +161,6 @@ services: - '@jsonapi.link_manager' - '@jsonapi.resource_type.repository' - '@renderer' - - '@request_stack' # Event subscribers. jsonapi.custom_query_parameter_names_validator.subscriber: diff --git a/src/Controller/EntityResource.php b/src/Controller/EntityResource.php index a5798a8..b52bbb8 100644 --- a/src/Controller/EntityResource.php +++ b/src/Controller/EntityResource.php @@ -4,7 +4,6 @@ namespace Drupal\jsonapi\Controller; use Drupal\Component\Serialization\Json; use Drupal\Core\Cache\CacheableMetadata; -use Drupal\Core\Cache\Context\RequestStackCacheContextBase; use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; @@ -30,7 +29,6 @@ use Drupal\jsonapi\ResourceResponse; use Drupal\jsonapi\ResourceType\ResourceType; use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\ConflictHttpException; @@ -86,13 +84,6 @@ class EntityResource { protected $renderer; /** - * The request stack. - * - * @var \Symfony\Component\HttpFoundation\RequestStack - */ - protected $requestStack; - - /** * Instantiates a EntityResource object. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager @@ -107,17 +98,14 @@ class EntityResource { * The link manager service. * @param \Drupal\Core\Render\RendererInterface $renderer * The renderer. - * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack - * The request stack. */ - public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $field_manager, FieldTypePluginManagerInterface $plugin_manager, LinkManager $link_manager, ResourceTypeRepositoryInterface $resource_type_repository, RendererInterface $renderer, RequestStack $request_stack) { + public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $field_manager, FieldTypePluginManagerInterface $plugin_manager, LinkManager $link_manager, ResourceTypeRepositoryInterface $resource_type_repository, RendererInterface $renderer) { $this->entityTypeManager = $entity_type_manager; $this->fieldManager = $field_manager; $this->pluginManager = $plugin_manager; $this->linkManager = $link_manager; $this->resourceTypeRepository = $resource_type_repository; $this->renderer = $renderer; - $this->requestStack = $request_stack; } /** @@ -136,12 +124,12 @@ class EntityResource { * @throws \Drupal\jsonapi\Exception\EntityAccessDeniedHttpException * Thrown when access to the entity is not allowed. */ - public function getIndividual(ResourceType $resource_type, EntityInterface $entity) { + public function getIndividual(ResourceType $resource_type, EntityInterface $entity, Request $request) { $entity = static::getAccessCheckedEntity($entity); if ($entity instanceof EntityAccessDeniedHttpException) { throw $entity; } - $response = $this->buildWrappedResponse($entity); + $response = $this->buildWrappedResponse($entity, $request); return $response; } @@ -246,7 +234,7 @@ class EntityResource { $entity->save(); // Build response object. - $response = $this->buildWrappedResponse($entity, 201); + $response = $this->buildWrappedResponse($entity, $request, 201); // According to JSON API specification, when a new entity was created // we should send "Location" header to the frontend. @@ -302,7 +290,7 @@ class EntityResource { $this->validate($entity, $field_names); $entity->save(); - return $this->buildWrappedResponse($entity); + return $this->buildWrappedResponse($entity, $request); } /** @@ -397,7 +385,7 @@ class EntityResource { $entity_collection->setTotalCount($total_results); } - $response = $this->respondWithCollection($entity_collection, $resource_type); + $response = $this->respondWithCollection($entity_collection, $resource_type, $request); $response->addCacheableDependency($query_cacheability); @@ -453,7 +441,7 @@ class EntityResource { $cacheable_metadata->addCacheableDependency($referenced_entity); } $entity_collection = new EntityCollection($collection_data); - $response = $this->buildWrappedResponse($entity_collection); + $response = $this->buildWrappedResponse($entity_collection, $request); // $response does not contain the entity list cache tag. We add the // cacheable metadata for the finite list of entities in the relationship. @@ -482,7 +470,7 @@ class EntityResource { public function getRelationship(ResourceType $resource_type, FieldableEntityInterface $entity, $related_field, Request $request, $response_code = 200) { /* @var \Drupal\Core\Field\FieldItemListInterface $field_list */ $field_list = $entity->get($resource_type->getInternalName($related_field)); - $response = $this->buildWrappedResponse($field_list, $response_code); + $response = $this->buildWrappedResponse($field_list, $request, $response_code); return $response; } @@ -800,6 +788,8 @@ class EntityResource { * * @param mixed $data * The data to wrap. + * @param \Symfony\Component\HttpFoundation\Request $request + * The request object. * @param int $response_code * The response code. * @param array $headers @@ -812,8 +802,8 @@ class EntityResource { * @return \Drupal\jsonapi\ResourceResponse * The response. */ - protected function buildWrappedResponse($data, $response_code = 200, array $headers = [], array $links = [], array $meta = []) { - $links['self'] = $this->linkManager->getRequestLink($this->requestStack->getCurrentRequest()); + protected function buildWrappedResponse($data, Request $request, $response_code = 200, array $headers = [], array $links = [], array $meta = []) { + $links['self'] = $this->linkManager->getRequestLink($request); return new ResourceResponse(new JsonApiDocumentTopLevel($data, $links, $meta), $response_code, $headers); } @@ -824,11 +814,13 @@ class EntityResource { * The collection of entites. * @param \Drupal\jsonapi\ResourceType\ResourceType $resource_type * The base JSON API resource type for the request to be served. + * @param \Symfony\Component\HttpFoundation\Request $request + * The request object. * * @return \Drupal\jsonapi\ResourceResponse * The response. */ - protected function respondWithCollection(EntityCollection $entity_collection, ResourceType $resource_type) { + protected function respondWithCollection(EntityCollection $entity_collection, ResourceType $resource_type, Request $request) { $link_context = [ 'has_next_page' => $entity_collection->hasNextPage(), ]; @@ -837,7 +829,7 @@ class EntityResource { $link_context['total_count'] = $meta['count'] = $entity_collection->getTotalCount(); } $collection_links = $this->linkManager->getPagerLinks(\Drupal::request(), $link_context); - $response = $this->buildWrappedResponse($entity_collection, 200, [], $collection_links, $meta); + $response = $this->buildWrappedResponse($entity_collection, $request, 200, [], $collection_links, $meta); // When a new change to any entity in the resource happens, we cannot ensure // the validity of this cached list. Add the list tag to deal with that.