You are here

class DependencyStack in Dependency Calculation 8

The dependencies stack.

Hierarchy

Expanded class hierarchy of DependencyStack

14 files declare their use of DependencyStack
BaseDependencyCollector.php in src/EventSubscriber/DependencyCollector/BaseDependencyCollector.php
CalculateDependenciesEventDispatcherTrait.php in tests/src/Kernel/EventSubscriber/DependencyCollector/CalculateDependenciesEventDispatcherTrait.php
CalculateEntityDependenciesEvent.php in src/Event/CalculateEntityDependenciesEvent.php
ConfigEntityDependencyCollectorTest.php in tests/src/Kernel/EventSubscriber/DependencyCollector/ConfigEntityDependencyCollectorTest.php
DependencyCalculatorTest.php in tests/src/Kernel/DependencyCalculatorTest.php

... See full list

File

src/DependencyStack.php, line 10

Namespace

Drupal\depcalc
View source
class DependencyStack {

  /**
   * The dependencies list.
   *
   * @var \Drupal\depcalc\DependentEntityWrapperInterface[]
   */
  protected $dependencies = [];

  /**
   * The list of dependencies requiring additional processing.
   *
   * @var array
   */
  protected $additional_processing = [];

  /**
   * An indicator whether this stack instances should not load cached values.
   *
   * @var bool
   */
  protected $ignoreCache = FALSE;

  /**
   * DependencyStack constructor.
   *
   * @param \Drupal\depcalc\DependentEntityWrapperInterface ...$dependencies
   *   Any previously built dependency to prevent recalculation.
   */
  public function __construct(DependentEntityWrapperInterface ...$dependencies) {
    foreach ($dependencies as $dependency) {
      $this->dependencies[$dependency
        ->getRemoteUuid()] = $dependency;
    }
  }

  /**
   * Set whether this stack should be able to load cached values.
   *
   * @param bool $value
   */
  public function ignoreCache(bool $value = TRUE) {
    $this->ignoreCache = $value;
  }

  /**
   * Add a dependency to the stack.
   *
   * @param \Drupal\depcalc\DependentEntityWrapperInterface $dependency
   *   The dependency to add to the stack.
   * @param bool $cache
   *   TRUE if to add to cache (Default), FALSE otherwise.
   */
  public function addDependency(DependentEntityWrapperInterface $dependency, $cache = true) {
    if ($cache) {
      \Drupal::cache('depcalc')
        ->set($dependency
        ->getUuid(), $dependency, Cache::PERMANENT, array_keys($dependency
        ->getDependencies()));
    }
    $this->dependencies[$dependency
      ->getRemoteUuid()] = $dependency;
    if ($dependency
      ->needsAdditionalProcessing()) {
      $this->additional_processing[$dependency
        ->getRemoteUuid()] = '';
    }
    else {
      unset($this->additional_processing[$dependency
        ->getRemoteUuid()]);
    }
  }

  /**
   * Get a specific dependency from the stack.
   *
   * @param string $uuid
   *   The uuid of the dependency to retrieve.
   *
   * @return \Drupal\depcalc\DependentEntityWrapperInterface|null
   *   The dependent entity wrapper.
   */
  public function getDependency($uuid) {
    if (!empty($this->dependencies[$uuid])) {
      return $this->dependencies[$uuid];
    }
    if (!$this->ignoreCache) {

      // Check cached dependencies.
      $cache = \Drupal::cache('depcalc')
        ->get($uuid);
      if ($cache && $cache->expire === "-1" && $cache->data instanceof DependentEntityWrapperInterface) {
        $this->dependencies[$uuid] = $cache->data;
        return $this->dependencies[$uuid];
      }
    }
    return NULL;
  }

  /**
   * Checks if a particular dependency exists in the stack.
   *
   * @param string $uuid
   *   The uuid of the dependency to check.
   *
   * @return bool
   */
  public function hasDependency($uuid) {
    return !empty($this->dependencies[$uuid]);
  }

  /**
   * Checks if a list of dependencies exist within the stack and are processed.
   *
   * @param array $targets
   *   An array of UUIDs.
   *
   * @return bool
   */
  public function hasDependencies(array $targets) {
    $count = count($targets);
    $targets = array_combine($targets, $targets);
    $intersection = array_intersect_key($targets, $this->dependencies);
    $equal = count($intersection) === $count;
    if ($equal) {
      $intersection = array_intersect_key($targets, $this->additional_processing);
      if ($intersection) {
        return FALSE;
      }
    }
    return $equal;
  }

  /**
   * Get a specific set of dependencies.
   *
   * @param string[] $dependencies
   *   The list of dependencies, by uuid, to retrieve.
   *
   * @return \Drupal\depcalc\DependentEntityWrapperInterface[]
   *   The dependencies.
   *
   * @throws \Exception
   */
  public function getDependenciesByUuid(array $dependencies) {
    $results = [];
    foreach ($dependencies as $uuid) {
      $dependency = $this
        ->getDependency($uuid);
      if (!$dependency) {
        throw new \Exception(sprintf("Missing Dependency requested: %s.", $uuid));
      }
      $results[$uuid] = $dependency;
    }
    return $results;
  }

  /**
   * * Get a list of dependencies within the stack.
   *
   * @return \Drupal\depcalc\DependentEntityWrapperInterface[]
   *   The dependencies.
   */
  public function getDependencies() {
    return $this->dependencies;
  }

  /**
   * Get a list of processed dependencies within the stack.
   *
   * This will exclude dependencies that have been created but which still
   * require additional processing.
   *
   * @return \Drupal\depcalc\DependentEntityWrapperInterface[]
   *   The processed dependencies.
   */
  public function getProcessedDependencies() {
    return array_diff_key($this->dependencies, $this->additional_processing);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencyStack::$additional_processing protected property The list of dependencies requiring additional processing.
DependencyStack::$dependencies protected property The dependencies list.
DependencyStack::$ignoreCache protected property An indicator whether this stack instances should not load cached values.
DependencyStack::addDependency public function Add a dependency to the stack.
DependencyStack::getDependencies public function Get a list of dependencies within the stack.
DependencyStack::getDependenciesByUuid public function Get a specific set of dependencies.
DependencyStack::getDependency public function Get a specific dependency from the stack.
DependencyStack::getProcessedDependencies public function Get a list of processed dependencies within the stack.
DependencyStack::hasDependencies public function Checks if a list of dependencies exist within the stack and are processed.
DependencyStack::hasDependency public function Checks if a particular dependency exists in the stack.
DependencyStack::ignoreCache public function Set whether this stack should be able to load cached values.
DependencyStack::__construct public function DependencyStack constructor.