"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/jsonapi/src/Controller/EntryPoint.php" (26 Nov 2020, 5084 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "EntryPoint.php" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 9.0.8_vs_9.1.0-rc1.

    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 Symfony\Component\DependencyInjection\ContainerInterface;
   18 use Symfony\Component\Routing\Exception\RouteNotFoundException;
   19 
   20 /**
   21  * Controller for the API entry point.
   22  *
   23  * @internal JSON:API maintains no PHP API. The API is the HTTP API. This class
   24  *   may change at any time and could break any dependencies on it.
   25  *
   26  * @see https://www.drupal.org/project/drupal/issues/3032787
   27  * @see jsonapi.api.php
   28  */
   29 class EntryPoint extends ControllerBase {
   30 
   31   /**
   32    * The JSON:API resource type repository.
   33    *
   34    * @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface
   35    */
   36   protected $resourceTypeRepository;
   37 
   38   /**
   39    * The account object.
   40    *
   41    * @var \Drupal\Core\Session\AccountInterface
   42    */
   43   protected $user;
   44 
   45   /**
   46    * EntryPoint constructor.
   47    *
   48    * @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository
   49    *   The resource type repository.
   50    * @param \Drupal\Core\Session\AccountInterface $user
   51    *   The current user.
   52    */
   53   public function __construct(ResourceTypeRepositoryInterface $resource_type_repository, AccountInterface $user) {
   54     $this->resourceTypeRepository = $resource_type_repository;
   55     $this->user = $user;
   56   }
   57 
   58   /**
   59    * {@inheritdoc}
   60    */
   61   public static function create(ContainerInterface $container) {
   62     return new static(
   63       $container->get('jsonapi.resource_type.repository'),
   64       $container->get('current_user')
   65     );
   66   }
   67 
   68   /**
   69    * Controller to list all the resources.
   70    *
   71    * @return \Drupal\jsonapi\ResourceResponse
   72    *   The response object.
   73    */
   74   public function index() {
   75     $cacheability = (new CacheableMetadata())
   76       ->addCacheContexts(['user.roles:authenticated'])
   77       ->addCacheTags(['jsonapi_resource_types']);
   78 
   79     // Only build URLs for exposed resources.
   80     $resources = array_filter($this->resourceTypeRepository->all(), function ($resource) {
   81       return !$resource->isInternal();
   82     });
   83 
   84     $self_link = new Link(new CacheableMetadata(), Url::fromRoute('jsonapi.resource_list'), 'self');
   85     $urls = array_reduce($resources, function (LinkCollection $carry, ResourceType $resource_type) {
   86       if ($resource_type->isLocatable() || $resource_type->isMutable()) {
   87         $route_suffix = $resource_type->isLocatable() ? 'collection' : 'collection.post';
   88         $url = Url::fromRoute(sprintf('jsonapi.%s.%s', $resource_type->getTypeName(), $route_suffix))->setAbsolute();
   89         // Using a resource type name in place of a link relation type is not
   90         // technically valid. However, since it matches the link key, it will
   91         // not actually be serialized since the rel is omitted if it matches the
   92         // link key; because of that no client can rely on it. Once an extension
   93         // relation type is implemented for links to a collection, that should
   94         // be used instead. Unfortunately, the `collection` link relation type
   95         // would not be semantically correct since it would imply that the
   96         // entrypoint is a *member* of the link target.
   97         // @todo: implement an extension relation type to signal that this is a primary collection resource.
   98         $link_relation_type = $resource_type->getTypeName();
   99         return $carry->withLink($resource_type->getTypeName(), new Link(new CacheableMetadata(), $url, $link_relation_type));
  100       }
  101       return $carry;
  102     }, new LinkCollection(['self' => $self_link]));
  103 
  104     $meta = [];
  105     if ($this->user->isAuthenticated()) {
  106       $current_user_uuid = $this->entityTypeManager()->getStorage('user')->load($this->user->id())->uuid();
  107       $meta['links']['me'] = ['meta' => ['id' => $current_user_uuid]];
  108       $cacheability->addCacheContexts(['user']);
  109       try {
  110         $me_url = Url::fromRoute(
  111           'jsonapi.user--user.individual',
  112           ['entity' => $current_user_uuid]
  113         )
  114           ->setAbsolute()
  115           ->toString(TRUE);
  116         $meta['links']['me']['href'] = $me_url->getGeneratedUrl();
  117         // The cacheability of the `me` URL is the cacheability of that URL
  118         // itself and the currently authenticated user.
  119         $cacheability = $cacheability->merge($me_url);
  120       }
  121       catch (RouteNotFoundException $e) {
  122         // Do not add the link if the route is disabled or marked as internal.
  123       }
  124     }
  125 
  126     $response = new ResourceResponse(new JsonApiDocumentTopLevel(new ResourceObjectData([]), new NullIncludedData(), $urls, $meta));
  127     return $response->addCacheableDependency($cacheability);
  128   }
  129 
  130 }