You are here

class DependentEntityWrapper in Dependency Calculation 8

An entity wrapper class for finding and tracking dependencies of an entity.

Hierarchy

Expanded class hierarchy of DependentEntityWrapper

29 files declare their use of DependentEntityWrapper
CalculateDependenciesEventDispatcherTrait.php in tests/src/Kernel/EventSubscriber/DependencyCollector/CalculateDependenciesEventDispatcherTrait.php
ConfigEntityDependencyCollector.php in src/EventSubscriber/DependencyCollector/ConfigEntityDependencyCollector.php
ConfigEntityDependencyCollectorTest.php in tests/src/Kernel/EventSubscriber/DependencyCollector/ConfigEntityDependencyCollectorTest.php
DependencyCalculatorTest.php in tests/src/Kernel/DependencyCalculatorTest.php
DependencyHelperTrait.php in tests/src/Kernel/DependencyHelperTrait.php

... See full list

File

src/DependentEntityWrapper.php, line 11

Namespace

Drupal\depcalc
View source
class DependentEntityWrapper implements DependentEntityWrapperInterface {

  /**
   * The entity id.
   *
   * @var int|null|string
   */
  protected $id;

  /**
   * The entity type id.
   *
   * @var string
   */
  protected $entityTypeId;

  /**
   * The entity uuid.
   *
   * @var null|string
   */
  protected $uuid;

  /**
   * The sha1 hash value of the entity.
   *
   * @var string
   */
  protected $hash;

  /**
   * The remote uuid of the entity if it differs from the ContentHub uuid.
   *
   * @var string
   */
  protected $remoteUuid;

  /**
   * The list of uuid/hash values of dependencies of this entity.
   *
   * @var string[]
   */
  protected $dependencies = [];

  /**
   * The list of uuid/hash values of direct child dependencies of this entity.
   *
   * @var string[]
   */
  protected $childDependencies = [];

  /**
   * The modules this entity requires to operate.
   *
   * @var string[]
   */
  protected $modules = [];

  /**
   * Whether this entity needs additional processing.
   *
   * @var bool
   */
  protected $additionalProcessing;

  /**
   * DependentEntityWrapper constructor.
   *
   * The entity object is thrown away within this constructor and just the bare
   * minimum of details to reconstruct it are kept. This is to reduce memory
   * overhead during the run time of dependency calculation.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity for which we are calculating dependencies.
   * @param bool $additional_processing
   *   Whether or not the entity will require additional processing.
   *
   * @throws \Exception
   */
  public function __construct(EntityInterface $entity, $additional_processing = FALSE) {
    $this->entityTypeId = $entity
      ->getEntityTypeId();
    $this->id = $entity
      ->id();
    $uuid = $entity
      ->uuid();
    $this->hash = sha1(json_encode($entity
      ->toArray()));
    if (empty($uuid)) {
      throw new \Exception(sprintf("The entity of type %s by id %s does not have a UUID. This indicates a larger problem with your application and should be remedied before attempting to calculate dependencies.", $this->entityTypeId, $this->id));
    }
    $this->uuid = $uuid;
    $this->additionalProcessing = $additional_processing;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntity() {
    return \Drupal::service("entity.repository")
      ->loadEntityByUuid($this
      ->getEntityTypeId(), $this
      ->getUuid());
  }

  /**
   * {@inheritdoc}
   */
  public function setRemoteUuid($uuid) {
    $this->remoteUuid = $uuid;
  }

  /**
   * {@inheritdoc}
   */
  public function getRemoteUuid() {
    if (!empty($this->remoteUuid)) {
      return $this->remoteUuid;
    }
    return $this
      ->getUuid();
  }

  /**
   * {@inheritdoc}
   */
  public function getId() {
    return $this->id;
  }

  /**
   * {@inheritdoc}
   */
  public function getUuid() {
    return $this->uuid;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityTypeId() {
    return $this->entityTypeId;
  }

  /**
   * {@inheritdoc}
   */
  public function addDependency(DependentEntityWrapperInterface $dependency, DependencyStack $stack, bool $direct_child = TRUE) {

    // Don't save a thing as a dependency of itself.
    if ($dependency
      ->getUuid() === $this
      ->getUUid()) {
      return;
    }
    if (!array_key_exists($dependency
      ->getUuid(), $this->dependencies)) {
      $this->dependencies[$dependency
        ->getUuid()] = $dependency
        ->getHash();

      // Add this dependency to direct child dependency array.
      if ($direct_child && !array_key_exists($dependency
        ->getUuid(), $this->childDependencies)) {

        // Minimal data needed to load this child entity.
        $this->childDependencies[$dependency
          ->getUuid()] = $dependency
          ->getUuid();
      }
      if (!$stack
        ->hasDependency($dependency
        ->getUuid())) {
        $stack
          ->addDependency($dependency);
        foreach ($stack
          ->getDependenciesByUuid(array_keys($dependency
          ->getDependencies())) as $sub_dependency) {

          // Since these are sub-dependencies, so setting the boolean to false.
          $this
            ->addDependency($sub_dependency, $stack, FALSE);
        }
      }
      else {
        $this
          ->addDependencies($stack, ...array_values($stack
          ->getDependenciesByUuid(array_keys($stack
          ->getDependency($dependency
          ->getUuid())
          ->getDependencies()))));
      }
      $modules = $stack
        ->getDependency($dependency
        ->getUuid())
        ->getModuleDependencies();
      if ($modules) {
        $this
          ->addModuleDependencies($modules);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function addDependencies(DependencyStack $stack, DependentEntityWrapperInterface ...$dependencies) {
    foreach ($dependencies as $dependency) {

      // This can't be added as a child because $dependencies holds
      // all the dependencies for a given entity whether direct or not,
      // so boolean set to false.
      $this
        ->addDependency($dependency, $stack, FALSE);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getDependencies() {
    return $this->dependencies;
  }

  /**
   * {@inheritdoc}
   */
  public function addModuleDependencies(array $modules) {
    $this->modules = array_values(array_unique(NestedArray::mergeDeep($this->modules, $modules)));
  }

  /**
   * {@inheritdoc}
   */
  public function getModuleDependencies() {
    return $this->modules;
  }

  /**
   * {@inheritdoc}
   */
  public function getHash() {
    return $this->hash;
  }

  /**
   * {@inheritdoc}
   */
  public function needsAdditionalProcessing() {
    return $this->additionalProcessing;
  }

  /**
   * {@inheritDoc}
   */
  public function getChildDependencies() : array {
    return $this->childDependencies;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependentEntityWrapper::$additionalProcessing protected property Whether this entity needs additional processing.
DependentEntityWrapper::$childDependencies protected property The list of uuid/hash values of direct child dependencies of this entity.
DependentEntityWrapper::$dependencies protected property The list of uuid/hash values of dependencies of this entity.
DependentEntityWrapper::$entityTypeId protected property The entity type id.
DependentEntityWrapper::$hash protected property The sha1 hash value of the entity.
DependentEntityWrapper::$id protected property The entity id.
DependentEntityWrapper::$modules protected property The modules this entity requires to operate.
DependentEntityWrapper::$remoteUuid protected property The remote uuid of the entity if it differs from the ContentHub uuid.
DependentEntityWrapper::$uuid protected property The entity uuid.
DependentEntityWrapper::addDependencies public function Add dependencies of this entity. Overrides DependentEntityWrapperInterface::addDependencies
DependentEntityWrapper::addDependency public function Document a new dependency for this entity. Overrides DependentEntityWrapperInterface::addDependency
DependentEntityWrapper::addModuleDependencies public function Add new module dependencies. Overrides DependentEntityWrapperInterface::addModuleDependencies
DependentEntityWrapper::getChildDependencies public function Get the uuid/hash values of direct child dependencies of this entity. Overrides DependentEntityWrapperInterface::getChildDependencies
DependentEntityWrapper::getDependencies public function Get the uuid/hash values of dependencies of this entity. Overrides DependentEntityWrapperInterface::getDependencies
DependentEntityWrapper::getEntity public function Get the entity for which we are collecting dependencies. Overrides DependentEntityWrapperInterface::getEntity
DependentEntityWrapper::getEntityTypeId public function The entity type id. Overrides DependentEntityWrapperInterface::getEntityTypeId
DependentEntityWrapper::getHash public function The hash value of the entity. Overrides DependentEntityWrapperInterface::getHash
DependentEntityWrapper::getId public function The id of the entity. Overrides DependentEntityWrapperInterface::getId
DependentEntityWrapper::getModuleDependencies public function The list of module dependencies. Overrides DependentEntityWrapperInterface::getModuleDependencies
DependentEntityWrapper::getRemoteUuid public function Get remote uuid. Overrides DependentEntityWrapperInterface::getRemoteUuid
DependentEntityWrapper::getUuid public function The uuid of the entity. Overrides DependentEntityWrapperInterface::getUuid
DependentEntityWrapper::needsAdditionalProcessing public function Whether any additional processing is needed. Overrides DependentEntityWrapperInterface::needsAdditionalProcessing
DependentEntityWrapper::setRemoteUuid public function Set remote uuid. Overrides DependentEntityWrapperInterface::setRemoteUuid
DependentEntityWrapper::__construct public function DependentEntityWrapper constructor.