jsonapi.services.yml | 2 +- src/EntityToJsonApi.php | 74 ++++++++++--------------------------------------- 2 files changed, 15 insertions(+), 61 deletions(-) diff --git a/jsonapi.services.yml b/jsonapi.services.yml index e66e5b5..b1b773a 100644 --- a/jsonapi.services.yml +++ b/jsonapi.services.yml @@ -122,7 +122,7 @@ services: jsonapi.entity.to_jsonapi: class: Drupal\jsonapi\EntityToJsonApi - arguments: ['@serializer', '@jsonapi.resource_type.repository', '@current_user'] + arguments: ['@http_kernel'] logger.channel.jsonapi: parent: logger.channel_base diff --git a/src/EntityToJsonApi.php b/src/EntityToJsonApi.php index 25b593f..46f6f94 100644 --- a/src/EntityToJsonApi.php +++ b/src/EntityToJsonApi.php @@ -2,13 +2,10 @@ namespace Drupal\jsonapi; +use Drupal\Component\Serialization\Json; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Session\AccountProxyInterface; -use Drupal\Core\Cache\CacheableMetadata; -use Drupal\jsonapi\Resource\JsonApiDocumentTopLevel; -use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\Serializer\Serializer; +use Symfony\Component\HttpKernel\HttpKernelInterface; /** * Simplifies the process of generating a JSON API version of an entity. @@ -16,36 +13,20 @@ use Symfony\Component\Serializer\Serializer; class EntityToJsonApi { /** - * The currently logged in user. + * The HTTP kernel. * - * @var \Drupal\Core\Session\AccountProxyInterface + * @var \Symfony\Component\HttpKernel\HttpKernelInterface */ - protected $currentUser; - - /** - * Serializer object. - * - * @var \Symfony\Component\Serializer\Serializer - */ - protected $serializer; - - /** - * @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface - */ - protected $resourceTypeRepository; + protected $httpKernel; /** * EntityToJsonApi constructor. * - * @param \Symfony\Component\Serializer\Serializer $serializer - * The serializer. - * @param \Drupal\Core\Session\AccountProxyInterface $current_user - * The currently logged in user. + * @param \Symfony\Component\HttpKernel\HttpKernelInterface + * The HTTP kernel. */ - public function __construct(Serializer $serializer, ResourceTypeRepositoryInterface $resource_type_repository, AccountProxyInterface $current_user) { - $this->serializer = $serializer; - $this->resourceTypeRepository = $resource_type_repository; - $this->currentUser = $current_user; + public function __construct(HttpKernelInterface $http_kernel) { + $this->httpKernel = $http_kernel; } /** @@ -58,11 +39,10 @@ class EntityToJsonApi { * The raw JSON string of the requested resource. */ public function serialize(EntityInterface $entity) { - // TODO: Supporting includes requires adding the 'include' query string. - return $this->serializer->serialize(new JsonApiDocumentTopLevel($entity), - 'api_json', - $this->calculateContext($entity) - ); + $path = sprintf('/jsonapi/%s/%s/%s', $entity->getEntityTypeId(), $entity->bundle(), $entity->uuid()); + $request = Request::create($path, 'GET', ['_format' => 'api_json']); + $response = $this->httpKernel->handle($request, HttpKernelInterface::SUB_REQUEST, TRUE); + return $response->getContent(); } /** @@ -75,33 +55,7 @@ class EntityToJsonApi { * The JSON structure of the requested resource. */ public function normalize(EntityInterface $entity) { - return $this->serializer->normalize(new JsonApiDocumentTopLevel($entity), - 'api_json', - $this->calculateContext($entity) - ); - } - - /** - * Calculate the context for the serialize/normalize operation. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity to generate the JSON from. - * - * @return array - * The context. - */ - protected function calculateContext(EntityInterface $entity) { - // TODO: Supporting includes requires adding the 'include' query string. - $request = new Request(); - return [ - 'account' => $this->currentUser, - 'cacheable_metadata' => new CacheableMetadata(), - 'resource_type' => $this->resourceTypeRepository->get( - $entity->getEntityTypeId(), - $entity->bundle() - ), - 'request' => $request, - ]; + return Json::decode($this->serialize($entity)); } }