You are here

public function Routes::routes in JSON:API Schema 8

1 string reference to 'Routes::routes'
jsonapi_schema.routing.yml in ./jsonapi_schema.routing.yml
jsonapi_schema.routing.yml

File

src/Routing/Routes.php, line 51

Class

Routes

Namespace

Drupal\jsonapi_schema\Routing

Code

public function routes() {
  $jsonapi_schema_routes = new RouteCollection();
  $entrypoint_schema_route = new Route($this->jsonApiBasePath . '/schema');
  $entrypoint_schema_route
    ->addDefaults([
    RouteObjectInterface::CONTROLLER_NAME => static::CONTROLLER_NAME . '::getEntrypointSchema',
  ]);
  $jsonapi_schema_routes
    ->add("jsonapi_schema.entrypoint", $entrypoint_schema_route);
  foreach ($this->resourceTypeRepository
    ->all() as $resource_type) {
    if ($resource_type
      ->isInternal()) {
      continue;
    }
    $resource_type_name = $resource_type
      ->getTypeName();
    $base_path = $this->jsonApiBasePath . $resource_type
      ->getPath();
    $individual_document_schema_route = new Route($base_path . '/schema');
    $individual_document_schema_route
      ->addDefaults([
      RouteObjectInterface::CONTROLLER_NAME => static::CONTROLLER_NAME . '::getDocumentSchema',
      static::ROUTE_TYPE_PARAMETER_KEY => 'item',
      static::RESOURCE_TYPE_PARAMETER_KEY => $resource_type_name,
    ]);
    $collection_document_schema_route = new Route($base_path . '/collection/schema');
    $collection_document_schema_route
      ->addDefaults([
      RouteObjectInterface::CONTROLLER_NAME => static::CONTROLLER_NAME . '::getDocumentSchema',
      static::ROUTE_TYPE_PARAMETER_KEY => 'collection',
      static::RESOURCE_TYPE_PARAMETER_KEY => $resource_type_name,
    ]);
    $resource_object_schema_route = new Route($base_path . '/resource/schema');
    $resource_object_schema_route
      ->addDefaults([
      RouteObjectInterface::CONTROLLER_NAME => static::CONTROLLER_NAME . '::getResourceObjectSchema',
      static::ROUTE_TYPE_PARAMETER_KEY => 'type',
      static::RESOURCE_TYPE_PARAMETER_KEY => $resource_type_name,
    ]);
    if ($resource_type
      ->isLocatable()) {
      $jsonapi_schema_routes
        ->add("jsonapi_schema.{$resource_type_name}.collection", $collection_document_schema_route);
    }
    $jsonapi_schema_routes
      ->add("jsonapi_schema.{$resource_type_name}.item", $individual_document_schema_route);
    $jsonapi_schema_routes
      ->add("jsonapi_schema.{$resource_type_name}.type", $resource_object_schema_route);
    foreach ($resource_type
      ->getRelatableResourceTypes() as $public_field_name => $target_resource_types) {
      if ($resource_type
        ->isInternal() || !Routes::hasNonInternalTargetResourceTypes($target_resource_types)) {
        continue;
      }
      $is_to_one_relationship = $resource_type
        ->getFieldByPublicName($public_field_name)
        ->hasOne();
      $public_target_resource_types = array_filter($target_resource_types, function (ResourceType $resource_type) {
        return !$resource_type
          ->isInternal();
      });
      $public_target_resource_type_names = array_map(function (ResourceType $resource_type) {
        return $resource_type
          ->getTypeName();
      }, $public_target_resource_types);
      $related_document_schema_route = new Route($base_path . "/resource/relationships/{$public_field_name}/related/schema");
      $related_document_schema_route
        ->addDefaults([
        RouteObjectInterface::CONTROLLER_NAME => static::CONTROLLER_NAME . '::getDocumentSchema',
        static::ROUTE_TYPE_PARAMETER_KEY => $is_to_one_relationship ? 'item' : 'collection',
        static::RESOURCE_TYPE_PARAMETER_KEY => count($public_target_resource_type_names) > 1 ? $public_target_resource_type_names : reset($public_target_resource_type_names),
      ]);
      $jsonapi_schema_routes
        ->add("jsonapi_schema.{$resource_type_name}.{$public_field_name}.related", $related_document_schema_route);

      //$relationship_document_schema_route = new Route($base_path . "/resource/relationships/$public_field_name/schema");
    }
  }
  $jsonapi_schema_routes
    ->addRequirements([
    '_access' => 'TRUE',
  ]);
  return $jsonapi_schema_routes;
}