DependentEntityWrapper.php in Dependency Calculation 8
File
src/DependentEntityWrapper.php
View source
<?php
namespace Drupal\depcalc;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityInterface;
class DependentEntityWrapper implements DependentEntityWrapperInterface {
protected $id;
protected $entityTypeId;
protected $uuid;
protected $hash;
protected $remoteUuid;
protected $dependencies = [];
protected $childDependencies = [];
protected $modules = [];
protected $additionalProcessing;
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;
}
public function getEntity() {
return \Drupal::service("entity.repository")
->loadEntityByUuid($this
->getEntityTypeId(), $this
->getUuid());
}
public function setRemoteUuid($uuid) {
$this->remoteUuid = $uuid;
}
public function getRemoteUuid() {
if (!empty($this->remoteUuid)) {
return $this->remoteUuid;
}
return $this
->getUuid();
}
public function getId() {
return $this->id;
}
public function getUuid() {
return $this->uuid;
}
public function getEntityTypeId() {
return $this->entityTypeId;
}
public function addDependency(DependentEntityWrapperInterface $dependency, DependencyStack $stack, bool $direct_child = TRUE) {
if ($dependency
->getUuid() === $this
->getUUid()) {
return;
}
if (!array_key_exists($dependency
->getUuid(), $this->dependencies)) {
$this->dependencies[$dependency
->getUuid()] = $dependency
->getHash();
if ($direct_child && !array_key_exists($dependency
->getUuid(), $this->childDependencies)) {
$this->childDependencies[$dependency
->getUuid()] = $dependency
->getUuid();
}
if (!$stack
->hasDependency($dependency
->getUuid())) {
$stack
->addDependency($dependency);
foreach ($stack
->getDependenciesByUuid(array_keys($dependency
->getDependencies())) as $sub_dependency) {
$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);
}
}
}
public function addDependencies(DependencyStack $stack, DependentEntityWrapperInterface ...$dependencies) {
foreach ($dependencies as $dependency) {
$this
->addDependency($dependency, $stack, FALSE);
}
}
public function getDependencies() {
return $this->dependencies;
}
public function addModuleDependencies(array $modules) {
$this->modules = array_values(array_unique(NestedArray::mergeDeep($this->modules, $modules)));
}
public function getModuleDependencies() {
return $this->modules;
}
public function getHash() {
return $this->hash;
}
public function needsAdditionalProcessing() {
return $this->additionalProcessing;
}
public function getChildDependencies() : array {
return $this->childDependencies;
}
}