public function DependencyCalculator::calculateDependencies in Dependency Calculation 8
Calculates the dependencies.
Parameters
\Drupal\depcalc\DependentEntityWrapperInterface $wrapper: The dependency wrapper for the entity to calculate dependencies.
\Drupal\depcalc\DependencyStack $stack: An array of pre-calculated dependencies to prevent recalculation.
array $dependencies: (optional) An array of dependencies by reference. Internally used.
Return value
array The list of the dependencies.
File
- src/
DependencyCalculator.php, line 59
Class
- DependencyCalculator
- Calculates all the dependencies of a given entity.
Namespace
Drupal\depcalcCode
public function calculateDependencies(DependentEntityWrapperInterface $wrapper, DependencyStack $stack, array &$dependencies = []) {
if (empty($dependencies['module'])) {
$dependencies['module'] = [];
}
// Prevent handling the same entity more than once.
if (!empty($dependencies[$wrapper
->getUuid()])) {
return $dependencies;
}
// Prevent handling the same entity more than once.
if ($dependency = $stack
->getDependency($wrapper
->getUuid())) {
// Get module dependencies from cache.
$modules = $dependency
->getModuleDependencies();
if ($modules) {
$wrapper
->addModuleDependencies($modules);
$dependencies['module'] = $modules;
}
// Tries to get a list of the dependencies from cache.
try {
$dependencies = $stack
->getDependenciesByUuid(array_keys($dependency
->getDependencies()));
// Add the dependencies from cache to the main wrapper as well as stack.
$wrapper
->addDependencies($stack, ...array_values($dependencies));
$dependencies[$wrapper
->getUuid()] = $dependency;
return $dependencies;
} catch (\Exception $exception) {
$msg = $exception
->getMessage();
$missing_dep_msg = sprintf("Retrieving dependencies from cache failed for entity (%s, %s) as one of the dependency is missing from the cache. Dependencies will be re-calculated." . PHP_EOL, $wrapper
->getEntityTypeId(), $wrapper
->getUuid());
$this->logger
->warning($missing_dep_msg . $msg);
}
}
// We add the dependency to the stack because we
// need it there but the dependency data is WRONG,
// so don't make it permanent in case something breaks before we get to
// resave it later when all dependencies are correctly calculated.
$stack
->addDependency($wrapper, FALSE);
$event = new CalculateEntityDependenciesEvent($wrapper, $stack);
$this->dispatcher
->dispatch(DependencyCalculatorEvents::CALCULATE_DEPENDENCIES, $event);
// Update the stack with the newest $wrapper and the correct dependencies.
$stack
->addDependency($wrapper);
$modules = $event
->getModuleDependencies();
if ($modules) {
$wrapper
->addModuleDependencies($modules);
}
$dependencies = $stack
->getDependenciesByUuid(array_keys($event
->getDependencies()));
$wrapper
->addDependencies($stack, ...array_values($dependencies));
$dependencies[$wrapper
->getUuid()] = $wrapper;
// Extract the name of the module providing this entity type.
$entity_module = $wrapper
->getEntity()
->getEntityType()
->getProvider();
$entity_module = \Drupal::moduleHandler()
->moduleExists($entity_module) ? [
$entity_module,
] : [];
$dependencies['module'] = $event
->getModuleDependencies() + $entity_module;
return $dependencies;
}