You are here

class Routes in JSON:API Schema 8

Hierarchy

Expanded class hierarchy of Routes

1 file declares its use of Routes
JsonApiSchemaController.php in src/Controller/JsonApiSchemaController.php

File

src/Routing/Routes.php, line 16

Namespace

Drupal\jsonapi_schema\Routing
View source
class Routes implements ContainerInjectionInterface {
  const CONTROLLER_NAME = JsonApiSchemaController::class;
  const RESOURCE_TYPE_PARAMETER_KEY = 'resource_type';
  const ROUTE_TYPE_PARAMETER_KEY = 'route_type';

  /**
   * @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface
   */
  protected $resourceTypeRepository;
  protected $jsonApiRoutes;
  protected $jsonApiBasePath;
  protected $entityFieldManager;
  public function __construct(ResourceTypeRepositoryInterface $resource_type_repository, EntityFieldManagerInterface $entity_field_manager, JsonApiRoutes $jsonapi_routes, $jsonapi_base_path) {
    $this->resourceTypeRepository = $resource_type_repository;
    $this->entityFieldManager = $entity_field_manager;
    $this->jsonApiRoutes = $jsonapi_routes;
    $this->jsonApiBasePath = $jsonapi_base_path;
  }
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('jsonapi.resource_type.repository'), $container
      ->get('entity_field.manager'), JsonApiRoutes::create($container), $container
      ->getParameter('jsonapi.base_path'));
  }
  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;
  }

  /**
   * Determines if an array of resource types has any non-internal ones.
   *
   * @param \Drupal\jsonapi\ResourceType\ResourceType[] $resource_types
   *   The resource types to check.
   *
   * @return bool
   *   TRUE if there is at least one non-internal resource type in the given
   *   array; FALSE otherwise.
   */
  public static function hasNonInternalTargetResourceTypes(array $resource_types) {
    return array_reduce($resource_types, function ($carry, ResourceType $target) {
      return $carry || !$target
        ->isInternal();
    }, FALSE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Routes::$entityFieldManager protected property
Routes::$jsonApiBasePath protected property
Routes::$jsonApiRoutes protected property
Routes::$resourceTypeRepository protected property
Routes::CONTROLLER_NAME constant
Routes::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
Routes::hasNonInternalTargetResourceTypes public static function Determines if an array of resource types has any non-internal ones.
Routes::RESOURCE_TYPE_PARAMETER_KEY constant
Routes::routes public function
Routes::ROUTE_TYPE_PARAMETER_KEY constant
Routes::__construct public function