You are here

class ConfigurableResourceTypeRepository in JSON:API Extras 8

Same name and namespace in other branches
  1. 8.3 src/ResourceType/ConfigurableResourceTypeRepository.php \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceTypeRepository
  2. 8.2 src/ResourceType/ConfigurableResourceTypeRepository.php \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceTypeRepository

Provides a repository of JSON API configurable resource types.

Hierarchy

Expanded class hierarchy of ConfigurableResourceTypeRepository

2 files declare their use of ConfigurableResourceTypeRepository
JsonapiExtrasServiceProvider.php in src/JsonapiExtrasServiceProvider.php
JsonapiResourceConfigListBuilder.php in src/JsonapiResourceConfigListBuilder.php

File

src/ResourceType/ConfigurableResourceTypeRepository.php, line 16

Namespace

Drupal\jsonapi_extras\ResourceType
View source
class ConfigurableResourceTypeRepository extends ResourceTypeRepository {

  /**
   * The entity repository.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * Plugin manager for enhancers.
   *
   * @var \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager
   */
  protected $enhancerManager;

  /**
   * The bundle manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $bundleManager;

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * A list of all resource types.
   *
   * @var \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType[]
   */
  protected $resourceTypes;

  /**
   * A list of only enabled resource types.
   *
   * @var \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType[]
   */
  protected $enabledResourceTypes;

  /**
   * A list of all resource configuration entities.
   *
   * @var \Drupal\jsonapi_extras\Entity\JsonapiResourceConfig[]
   */
  protected $resourceConfigs;

  /**
   * {@inheritdoc}
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_manager, EntityFieldManagerInterface $entity_field_manager, EntityRepositoryInterface $entity_repository, ResourceFieldEnhancerManager $enhancer_manager, ConfigFactoryInterface $config_factory) {
    parent::__construct($entity_type_manager, $bundle_manager, $entity_field_manager);
    $this->entityRepository = $entity_repository;
    $this->enhancerManager = $enhancer_manager;
    $this->configFactory = $config_factory;
    $this->entityFieldManager = $entity_field_manager;
    $this->bundleManager = $bundle_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function all() {
    if (!$this->all) {
      foreach ($this
        ->getEntityTypeBundleTuples() as $tuple) {
        list($entity_type_id, $bundle) = $tuple;
        $resource_config_id = sprintf('%s--%s', $entity_type_id, $bundle);
        $this->all[] = new ConfigurableResourceType($entity_type_id, $bundle, $this->entityTypeManager
          ->getDefinition($entity_type_id)
          ->getClass(), $this
          ->getResourceConfig($resource_config_id), $this->enhancerManager, $this->configFactory);
      }
      foreach ($this->all as $resource_type) {
        $relatable_resource_types = $this
          ->calculateRelatableResourceTypes($resource_type);
        $resource_type
          ->setRelatableResourceTypes($relatable_resource_types);
      }
    }
    return $this->all;
  }

  /**
   * Get a single resource configuration entity by its ID.
   *
   * @param string $resource_config_id
   *   The configuration entity ID.
   *
   * @return \Drupal\jsonapi_extras\Entity\JsonapiResourceConfig
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   */
  protected function getResourceConfig($resource_config_id) {
    $resource_configs = $this
      ->getResourceConfigs();
    return isset($resource_configs[$resource_config_id]) ? $resource_configs[$resource_config_id] : new NullJsonapiResourceConfig([], '');
  }

  /**
   * Load all resource configuration entities.
   *
   * @return \Drupal\jsonapi_extras\Entity\JsonapiResourceConfig[]
   *   The resource config entities.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   */
  function getResourceConfigs() {
    if (!$this->resourceConfigs) {
      $resource_config_ids = [];
      foreach ($this
        ->getEntityTypeBundleTuples() as $tuple) {
        list($entity_type_id, $bundle) = $tuple;
        $resource_config_ids[] = sprintf('%s--%s', $entity_type_id, $bundle);
      }
      $this->resourceConfigs = $this->entityTypeManager
        ->getStorage('jsonapi_resource_config')
        ->loadMultiple($resource_config_ids);
    }
    return $this->resourceConfigs;
  }

  /**
   * Entity type ID and bundle iterator.
   *
   * @return array
   *   A list of entity type ID and bundle tuples.
   */
  protected function getEntityTypeBundleTuples() {
    $entity_type_ids = array_keys($this->entityTypeManager
      ->getDefinitions());

    // For each entity type return as many tuples as bundles.
    return array_reduce($entity_type_ids, function ($carry, $entity_type_id) {
      $bundles = array_keys($this->bundleManager
        ->getBundleInfo($entity_type_id));

      // Get all the tuples for the current entity type.
      $tuples = array_map(function ($bundle) use ($entity_type_id) {
        return [
          $entity_type_id,
          $bundle,
        ];
      }, $bundles);

      // Append the tuples to the aggregated list.
      return array_merge($carry, $tuples);
    }, []);
  }

  /**
   * {@inheritdoc}
   */
  public function getPathPrefix() {
    return $this->configFactory
      ->get('jsonapi_extras.settings')
      ->get('path_prefix');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurableResourceTypeRepository::$bundleManager protected property The bundle manager.
ConfigurableResourceTypeRepository::$configFactory protected property The configuration factory.
ConfigurableResourceTypeRepository::$enabledResourceTypes protected property A list of only enabled resource types.
ConfigurableResourceTypeRepository::$enhancerManager protected property Plugin manager for enhancers.
ConfigurableResourceTypeRepository::$entityRepository protected property The entity repository.
ConfigurableResourceTypeRepository::$resourceConfigs protected property A list of all resource configuration entities.
ConfigurableResourceTypeRepository::$resourceTypes protected property A list of all resource types.
ConfigurableResourceTypeRepository::all public function Gets all JSON:API resource types. Overrides ResourceTypeRepository::all
ConfigurableResourceTypeRepository::getEntityTypeBundleTuples protected function Entity type ID and bundle iterator.
ConfigurableResourceTypeRepository::getPathPrefix public function
ConfigurableResourceTypeRepository::getResourceConfig protected function Get a single resource configuration entity by its ID.
ConfigurableResourceTypeRepository::getResourceConfigs function Load all resource configuration entities.
ConfigurableResourceTypeRepository::__construct public function Instantiates a ResourceTypeRepository object. Overrides ResourceTypeRepository::__construct
ResourceTypeRepository::$cache protected property The cache backend.
ResourceTypeRepository::$cacheTags protected property Cache tags used for caching the repository.
ResourceTypeRepository::$entityFieldManager protected property The entity field manager.
ResourceTypeRepository::$entityTypeBundleInfo protected property The bundle manager.
ResourceTypeRepository::$entityTypeManager protected property The entity type manager.
ResourceTypeRepository::$eventDispatcher protected property The event dispatcher.
ResourceTypeRepository::calculateRelatableResourceTypes protected function Calculates relatable JSON:API resource types for a given resource type.
ResourceTypeRepository::createResourceType protected function Creates a ResourceType value object for the given entity type + bundle. 1
ResourceTypeRepository::get public function Gets a specific JSON:API resource type based on entity type ID and bundle. Overrides ResourceTypeRepositoryInterface::get
ResourceTypeRepository::getAllBundlesForEntityType protected function Gets all bundle IDs for a given entity type.
ResourceTypeRepository::getAllFieldNames protected function Gets all field names for a given entity type and bundle.
ResourceTypeRepository::getByTypeName public function Gets a specific JSON:API resource type based on a supplied typename. Overrides ResourceTypeRepositoryInterface::getByTypeName
ResourceTypeRepository::getFieldMapping Deprecated protected function Gets the field mapping for the given field names and entity type + bundle.
ResourceTypeRepository::getFields protected function Gets the field mapping for the given field names and entity type + bundle. 1
ResourceTypeRepository::getRelatableResourceTypesFromFieldDefinition protected function Get relatable resource types from a field definition.
ResourceTypeRepository::isLocatableResourceType protected static function Whether an entity type + bundle maps to a locatable resource type.
ResourceTypeRepository::isMutableResourceType protected static function Whether an entity type + bundle maps to a mutable resource type.
ResourceTypeRepository::isReferenceFieldDefinition protected function Determines if a given field definition is a reference field.
ResourceTypeRepository::isVersionableResourceType protected static function Whether an entity type is a versionable resource type.