Chris@18: resourceTypeRepository = $resource_type_repository; Chris@18: $this->user = $user; Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public static function create(ContainerInterface $container) { Chris@18: return new static( Chris@18: $container->get('jsonapi.resource_type.repository'), Chris@18: $container->get('current_user') Chris@18: ); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Controller to list all the resources. Chris@18: * Chris@18: * @return \Drupal\jsonapi\ResourceResponse Chris@18: * The response object. Chris@18: */ Chris@18: public function index() { Chris@18: $cacheability = (new CacheableMetadata()) Chris@18: ->addCacheContexts(['user.roles:authenticated']) Chris@18: ->addCacheTags(['jsonapi_resource_types']); Chris@18: Chris@18: // Only build URLs for exposed resources. Chris@18: $resources = array_filter($this->resourceTypeRepository->all(), function ($resource) { Chris@18: return !$resource->isInternal(); Chris@18: }); Chris@18: Chris@18: $self_link = new Link(new CacheableMetadata(), Url::fromRoute('jsonapi.resource_list'), ['self']); Chris@18: $urls = array_reduce($resources, function (LinkCollection $carry, ResourceType $resource_type) { Chris@18: if ($resource_type->isLocatable() || $resource_type->isMutable()) { Chris@18: $route_suffix = $resource_type->isLocatable() ? 'collection' : 'collection.post'; Chris@18: $url = Url::fromRoute(sprintf('jsonapi.%s.%s', $resource_type->getTypeName(), $route_suffix))->setAbsolute(); Chris@18: // @todo: implement an extension relation type to signal that this is a primary collection resource. Chris@18: $link_relation_types = []; Chris@18: return $carry->withLink($resource_type->getTypeName(), new Link(new CacheableMetadata(), $url, $link_relation_types)); Chris@18: } Chris@18: return $carry; Chris@18: }, new LinkCollection(['self' => $self_link])); Chris@18: Chris@18: $meta = []; Chris@18: if ($this->user->isAuthenticated()) { Chris@18: $current_user_uuid = User::load($this->user->id())->uuid(); Chris@18: $meta['links']['me'] = ['meta' => ['id' => $current_user_uuid]]; Chris@18: $cacheability->addCacheContexts(['user']); Chris@18: try { Chris@18: $me_url = Url::fromRoute( Chris@18: 'jsonapi.user--user.individual', Chris@18: ['entity' => $current_user_uuid] Chris@18: ) Chris@18: ->setAbsolute() Chris@18: ->toString(TRUE); Chris@18: $meta['links']['me']['href'] = $me_url->getGeneratedUrl(); Chris@18: // The cacheability of the `me` URL is the cacheability of that URL Chris@18: // itself and the currently authenticated user. Chris@18: $cacheability = $cacheability->merge($me_url); Chris@18: } Chris@18: catch (RouteNotFoundException $e) { Chris@18: // Do not add the link if the route is disabled or marked as internal. Chris@18: } Chris@18: } Chris@18: Chris@18: $response = new ResourceResponse(new JsonApiDocumentTopLevel(new ResourceObjectData([]), new NullIncludedData(), $urls, $meta)); Chris@18: return $response->addCacheableDependency($cacheability); Chris@18: } Chris@18: Chris@18: }