You are here

class EntityLastInstalledSchemaRepository in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/EntityLastInstalledSchemaRepository.php \Drupal\Core\Entity\EntityLastInstalledSchemaRepository

Provides a repository for installed entity definitions.

Hierarchy

Expanded class hierarchy of EntityLastInstalledSchemaRepository

1 string reference to 'EntityLastInstalledSchemaRepository'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses EntityLastInstalledSchemaRepository
entity.last_installed_schema.repository in core/core.services.yml
Drupal\Core\Entity\EntityLastInstalledSchemaRepository

File

core/lib/Drupal/Core/Entity/EntityLastInstalledSchemaRepository.php, line 13

Namespace

Drupal\Core\Entity
View source
class EntityLastInstalledSchemaRepository implements EntityLastInstalledSchemaRepositoryInterface {

  /**
   * The key-value factory.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface
   */
  protected $keyValueFactory;

  /**
   * The cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cacheBackend;

  /**
   * The loaded installed entity type definitions.
   *
   * @var array|null
   */
  protected $entityTypeDefinitions = NULL;

  /**
   * Constructs a new EntityLastInstalledSchemaRepository.
   *
   * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
   *   The key-value factory.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The cache backend.
   */
  public function __construct(KeyValueFactoryInterface $key_value_factory, CacheBackendInterface $cache = NULL) {
    $this->keyValueFactory = $key_value_factory;
    if (!$cache) {
      @trigger_error('The cache.discovery service must be passed to EntityLastInstalledSchemaRepository::__construct(), it is required before drupal:10.0.0.', E_USER_DEPRECATED);
      $cache = \Drupal::cache('discovery');
    }
    $this->cacheBackend = $cache;
  }

  /**
   * {@inheritdoc}
   */
  public function getLastInstalledDefinition($entity_type_id) {
    return $this
      ->getLastInstalledDefinitions()[$entity_type_id] ?? NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getLastInstalledDefinitions() {
    if ($this->entityTypeDefinitions) {
      return $this->entityTypeDefinitions;
    }
    elseif ($cache = $this->cacheBackend
      ->get('entity_type_definitions.installed')) {
      $this->entityTypeDefinitions = $cache->data;
      return $this->entityTypeDefinitions;
    }
    $all_definitions = $this->keyValueFactory
      ->get('entity.definitions.installed')
      ->getAll();

    // Filter out field storage definitions.
    $filtered_keys = array_filter(array_keys($all_definitions), function ($key) {
      return substr($key, -12) === '.entity_type';
    });
    $entity_type_definitions = array_intersect_key($all_definitions, array_flip($filtered_keys));

    // Ensure that the returned array is keyed by the entity type ID.
    $keys = array_keys($entity_type_definitions);
    $keys = array_map(function ($key) {
      $parts = explode('.', $key);
      return $parts[0];
    }, $keys);
    $this->entityTypeDefinitions = array_combine($keys, $entity_type_definitions);
    $this->cacheBackend
      ->set('entity_type_definitions.installed', $this->entityTypeDefinitions, Cache::PERMANENT);
    return $this->entityTypeDefinitions;
  }

  /**
   * {@inheritdoc}
   */
  public function setLastInstalledDefinition(EntityTypeInterface $entity_type) {
    $entity_type_id = $entity_type
      ->id();
    $this->keyValueFactory
      ->get('entity.definitions.installed')
      ->set($entity_type_id . '.entity_type', $entity_type);
    $this->cacheBackend
      ->delete('entity_type_definitions.installed');
    $this->entityTypeDefinitions = NULL;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function deleteLastInstalledDefinition($entity_type_id) {
    $this->keyValueFactory
      ->get('entity.definitions.installed')
      ->delete($entity_type_id . '.entity_type');

    // Clean up field storage definitions as well. Even if the entity type
    // isn't currently fieldable, there might be legacy definitions or an
    // empty array stored from when it was.
    $this->keyValueFactory
      ->get('entity.definitions.installed')
      ->delete($entity_type_id . '.field_storage_definitions');
    $this->cacheBackend
      ->deleteMultiple([
      'entity_type_definitions.installed',
      $entity_type_id . '.field_storage_definitions.installed',
    ]);
    $this->entityTypeDefinitions = NULL;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getLastInstalledFieldStorageDefinitions($entity_type_id) {
    if ($cache = $this->cacheBackend
      ->get($entity_type_id . '.field_storage_definitions.installed')) {
      return $cache->data;
    }
    $definitions = $this->keyValueFactory
      ->get('entity.definitions.installed')
      ->get($entity_type_id . '.field_storage_definitions', []);
    $this->cacheBackend
      ->set($entity_type_id . '.field_storage_definitions.installed', $definitions, Cache::PERMANENT);
    return $definitions;
  }

  /**
   * {@inheritdoc}
   */
  public function setLastInstalledFieldStorageDefinitions($entity_type_id, array $storage_definitions) {
    $this->keyValueFactory
      ->get('entity.definitions.installed')
      ->set($entity_type_id . '.field_storage_definitions', $storage_definitions);
    $this->cacheBackend
      ->delete($entity_type_id . '.field_storage_definitions.installed');
  }

  /**
   * {@inheritdoc}
   */
  public function setLastInstalledFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
    $entity_type_id = $storage_definition
      ->getTargetEntityTypeId();
    $definitions = $this
      ->getLastInstalledFieldStorageDefinitions($entity_type_id);
    $definitions[$storage_definition
      ->getName()] = $storage_definition;
    $this
      ->setLastInstalledFieldStorageDefinitions($entity_type_id, $definitions);
  }

  /**
   * {@inheritdoc}
   */
  public function deleteLastInstalledFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
    $entity_type_id = $storage_definition
      ->getTargetEntityTypeId();
    $definitions = $this
      ->getLastInstalledFieldStorageDefinitions($entity_type_id);
    unset($definitions[$storage_definition
      ->getName()]);
    $this
      ->setLastInstalledFieldStorageDefinitions($entity_type_id, $definitions);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityLastInstalledSchemaRepository::$cacheBackend protected property The cache backend.
EntityLastInstalledSchemaRepository::$entityTypeDefinitions protected property The loaded installed entity type definitions.
EntityLastInstalledSchemaRepository::$keyValueFactory protected property The key-value factory.
EntityLastInstalledSchemaRepository::deleteLastInstalledDefinition public function Deletes the entity type definition from the application state. Overrides EntityLastInstalledSchemaRepositoryInterface::deleteLastInstalledDefinition
EntityLastInstalledSchemaRepository::deleteLastInstalledFieldStorageDefinition public function Deletes the field storage definition from the application state. Overrides EntityLastInstalledSchemaRepositoryInterface::deleteLastInstalledFieldStorageDefinition
EntityLastInstalledSchemaRepository::getLastInstalledDefinition public function Gets the entity type definition in its most recently installed state. Overrides EntityLastInstalledSchemaRepositoryInterface::getLastInstalledDefinition
EntityLastInstalledSchemaRepository::getLastInstalledDefinitions public function Gets the entity type definitions in their most recently installed state. Overrides EntityLastInstalledSchemaRepositoryInterface::getLastInstalledDefinitions
EntityLastInstalledSchemaRepository::getLastInstalledFieldStorageDefinitions public function Gets the entity type's most recently installed field storage definitions. Overrides EntityLastInstalledSchemaRepositoryInterface::getLastInstalledFieldStorageDefinitions
EntityLastInstalledSchemaRepository::setLastInstalledDefinition public function Stores the entity type definition in the application state. Overrides EntityLastInstalledSchemaRepositoryInterface::setLastInstalledDefinition
EntityLastInstalledSchemaRepository::setLastInstalledFieldStorageDefinition public function Stores the field storage definition in the application state. Overrides EntityLastInstalledSchemaRepositoryInterface::setLastInstalledFieldStorageDefinition
EntityLastInstalledSchemaRepository::setLastInstalledFieldStorageDefinitions public function Stores the entity type's field storage definitions in the application state. Overrides EntityLastInstalledSchemaRepositoryInterface::setLastInstalledFieldStorageDefinitions
EntityLastInstalledSchemaRepository::__construct public function Constructs a new EntityLastInstalledSchemaRepository.