comparison core/modules/jsonapi/src/Controller/EntryPoint.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php
2
3 namespace Drupal\jsonapi\Controller;
4
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\Controller\ControllerBase;
7 use Drupal\Core\Session\AccountInterface;
8 use Drupal\Core\Url;
9 use Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel;
10 use Drupal\jsonapi\JsonApiResource\LinkCollection;
11 use Drupal\jsonapi\JsonApiResource\NullIncludedData;
12 use Drupal\jsonapi\JsonApiResource\Link;
13 use Drupal\jsonapi\JsonApiResource\ResourceObjectData;
14 use Drupal\jsonapi\ResourceResponse;
15 use Drupal\jsonapi\ResourceType\ResourceType;
16 use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface;
17 use Drupal\user\Entity\User;
18 use Symfony\Component\DependencyInjection\ContainerInterface;
19 use Symfony\Component\Routing\Exception\RouteNotFoundException;
20
21 /**
22 * Controller for the API entry point.
23 *
24 * @internal JSON:API maintains no PHP API. The API is the HTTP API. This class
25 * may change at any time and could break any dependencies on it.
26 *
27 * @see https://www.drupal.org/project/jsonapi/issues/3032787
28 * @see jsonapi.api.php
29 */
30 class EntryPoint extends ControllerBase {
31
32 /**
33 * The JSON:API resource type repository.
34 *
35 * @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface
36 */
37 protected $resourceTypeRepository;
38
39 /**
40 * The account object.
41 *
42 * @var \Drupal\Core\Session\AccountInterface
43 */
44 protected $user;
45
46 /**
47 * EntryPoint constructor.
48 *
49 * @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository
50 * The resource type repository.
51 * @param \Drupal\Core\Session\AccountInterface $user
52 * The current user.
53 */
54 public function __construct(ResourceTypeRepositoryInterface $resource_type_repository, AccountInterface $user) {
55 $this->resourceTypeRepository = $resource_type_repository;
56 $this->user = $user;
57 }
58
59 /**
60 * {@inheritdoc}
61 */
62 public static function create(ContainerInterface $container) {
63 return new static(
64 $container->get('jsonapi.resource_type.repository'),
65 $container->get('current_user')
66 );
67 }
68
69 /**
70 * Controller to list all the resources.
71 *
72 * @return \Drupal\jsonapi\ResourceResponse
73 * The response object.
74 */
75 public function index() {
76 $cacheability = (new CacheableMetadata())
77 ->addCacheContexts(['user.roles:authenticated'])
78 ->addCacheTags(['jsonapi_resource_types']);
79
80 // Only build URLs for exposed resources.
81 $resources = array_filter($this->resourceTypeRepository->all(), function ($resource) {
82 return !$resource->isInternal();
83 });
84
85 $self_link = new Link(new CacheableMetadata(), Url::fromRoute('jsonapi.resource_list'), ['self']);
86 $urls = array_reduce($resources, function (LinkCollection $carry, ResourceType $resource_type) {
87 if ($resource_type->isLocatable() || $resource_type->isMutable()) {
88 $route_suffix = $resource_type->isLocatable() ? 'collection' : 'collection.post';
89 $url = Url::fromRoute(sprintf('jsonapi.%s.%s', $resource_type->getTypeName(), $route_suffix))->setAbsolute();
90 // @todo: implement an extension relation type to signal that this is a primary collection resource.
91 $link_relation_types = [];
92 return $carry->withLink($resource_type->getTypeName(), new Link(new CacheableMetadata(), $url, $link_relation_types));
93 }
94 return $carry;
95 }, new LinkCollection(['self' => $self_link]));
96
97 $meta = [];
98 if ($this->user->isAuthenticated()) {
99 $current_user_uuid = User::load($this->user->id())->uuid();
100 $meta['links']['me'] = ['meta' => ['id' => $current_user_uuid]];
101 $cacheability->addCacheContexts(['user']);
102 try {
103 $me_url = Url::fromRoute(
104 'jsonapi.user--user.individual',
105 ['entity' => $current_user_uuid]
106 )
107 ->setAbsolute()
108 ->toString(TRUE);
109 $meta['links']['me']['href'] = $me_url->getGeneratedUrl();
110 // The cacheability of the `me` URL is the cacheability of that URL
111 // itself and the currently authenticated user.
112 $cacheability = $cacheability->merge($me_url);
113 }
114 catch (RouteNotFoundException $e) {
115 // Do not add the link if the route is disabled or marked as internal.
116 }
117 }
118
119 $response = new ResourceResponse(new JsonApiDocumentTopLevel(new ResourceObjectData([]), new NullIncludedData(), $urls, $meta));
120 return $response->addCacheableDependency($cacheability);
121 }
122
123 }