You are here

public function JsonApiSchemaController::getEntrypointSchema in JSON:API Schema 8

File

src/Controller/JsonApiSchemaController.php, line 86

Class

JsonApiSchemaController

Namespace

Drupal\jsonapi_schema\Controller

Code

public function getEntrypointSchema(Request $request) {
  $cacheability = new CacheableMetadata();
  $cacheability
    ->addCacheTags([
    'jsonapi_resource_types',
  ]);
  $collection_links = array_values(array_map(function (ResourceType $resource_type) use ($cacheability) {
    $schema_url = Url::fromRoute("jsonapi_schema.{$resource_type->getTypeName()}.collection")
      ->setAbsolute()
      ->toString(TRUE);
    $cacheability
      ->addCacheableDependency($schema_url);
    return [
      'href' => '{instanceHref}',
      'rel' => 'related',
      'title' => $this
        ->getSchemaTitle($resource_type, 'collection'),
      'targetMediaType' => 'application/vnd.api+json',
      'targetSchema' => $schema_url
        ->getGeneratedUrl(),
      'templatePointers' => [
        'instanceHref' => "/links/{$resource_type->getTypeName()}/href",
      ],
      'templateRequired' => [
        'instanceHref',
      ],
    ];
  }, array_filter($this->resourceTypeRepository
    ->all(), function (ResourceType $resource_type) {
    return !$resource_type
      ->isInternal() && $resource_type
      ->isLocatable();
  })));
  $schema = [
    '$schema' => static::JSON_SCHEMA_DRAFT,
    '$id' => $request
      ->getUri(),
    'allOf' => [
      [
        '$ref' => static::JSONAPI_BASE_SCHEMA_URI . '#/definitions/success',
      ],
      [
        'type' => 'object',
        'links' => $collection_links,
      ],
    ],
  ];
  return CacheableJsonResponse::create($schema)
    ->addCacheableDependency($cacheability);
}