class StorageComparer

Defines a config storage comparer.

Hierarchy

Expanded class hierarchy of StorageComparer

6 files declare their use of StorageComparer
config.admin.inc in drupal/core/modules/config/config.admin.inc
Admin page callbacks for the config module.
config.inc in drupal/core/includes/config.inc
This is the API for configuration storage.
ConfigImporterTest.php in drupal/core/modules/config/lib/Drupal/config/Tests/ConfigImporterTest.php
Contains \Drupal\config\Tests\ConfigImporterTest.
ConfigSnapshotTest.php in drupal/core/modules/config/lib/Drupal/config/Tests/ConfigSnapshotTest.php
Contains \Drupal\config\Tests\ConfigSnapshotTest.
TestBase.php in drupal/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
Definition of Drupal\simpletest\TestBase.

... See full list

File

drupal/core/lib/Drupal/Core/Config/StorageComparer.php, line 13
Contains \Drupal\Core\Config\StorageComparer.

Namespace

Drupal\Core\Config
View source
class StorageComparer implements StorageComparerInterface {

  /**
   * The source storage used to discover configuration changes.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $sourceStorage;

  /**
   * The target storage used to write configuration changes.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $targetStorage;

  /**
   * List of changes to between the source storage and the target storage.
   *
   * @var array
   */
  protected $changelist;

  /**
   * Lists all the configuration object names in the source storage.
   *
   * @see \Drupal\Core\Config\StorageComparer::getSourceNames()
   *
   * @var array
   */
  protected $sourceNames = array();

  /**
   * Lists all the configuration object names in the target storage.
   *
   * @see \Drupal\Core\Config\StorageComparer::getTargetNames()
   *
   * @var array
   */
  protected $targetNames = array();

  /**
   * Constructs the Configuration storage comparer.
   *
   * @param \Drupal\Core\Config\StorageInterface $source_storage
   *   Storage controller object used to read configuration.
   * @param \Drupal\Core\Config\StorageInterface $target_storage
   *   Storage controller object used to write configuration.
   */
  public function __construct(StorageInterface $source_storage, StorageInterface $target_storage) {
    $this->sourceStorage = $source_storage;
    $this->targetStorage = $target_storage;
    $this->changelist = $this
      ->getEmptyChangelist();
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function getEmptyChangelist() {
    return array(
      'create' => array(),
      'update' => array(),
      'delete' => array(),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getChangelist($op = NULL) {
    if ($op) {
      return $this->changelist[$op];
    }
    return $this->changelist;
  }

  /**
   * {@inheritdoc}
   */
  public function addChangeList($op, array $changes) {

    // Only add changes that aren't already listed.
    $changes = array_diff($changes, $this->changelist[$op]);
    $this->changelist[$op] = array_merge($this->changelist[$op], $changes);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function createChangelist() {
    return $this
      ->addChangelistCreate()
      ->addChangelistUpdate()
      ->addChangelistDelete();
  }

  /**
   * {@inheritdoc}
   */
  public function addChangelistDelete() {
    return $this
      ->addChangeList('delete', array_diff($this
      ->getTargetNames(), $this
      ->getSourceNames()));
  }

  /**
   * {@inheritdoc}
   */
  public function addChangelistCreate() {
    return $this
      ->addChangeList('create', array_diff($this
      ->getSourceNames(), $this
      ->getTargetNames()));
  }

  /**
   * {@inheritdoc}
   */
  public function addChangelistUpdate() {
    foreach (array_intersect($this
      ->getSourceNames(), $this
      ->getTargetNames()) as $name) {
      $source_config_data = $this->sourceStorage
        ->read($name);
      $target_config_data = $this->targetStorage
        ->read($name);
      if ($source_config_data !== $target_config_data) {
        $this
          ->addChangeList('update', array(
          $name,
        ));
      }
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function reset() {
    $this->changelist = $this
      ->getEmptyChangelist();
    $this->sourceNames = $this->targetNames = array();
    return $this
      ->createChangelist();
  }

  /**
   * {@inheritdoc}
   */
  public function hasChanges($ops = array(
    'delete',
    'create',
    'update',
  )) {
    foreach ($ops as $op) {
      if (!empty($this->changelist[$op])) {
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * Gets all the configuration names in the source storage.
   *
   * @return array
   *   List of all the configuration names in the source storage.
   */
  protected function getSourceNames() {
    if (empty($this->sourceNames)) {
      $this->sourceNames = $this->sourceStorage
        ->listAll();
    }
    return $this->sourceNames;
  }

  /**
   * Gets all the configuration names in the target storage.
   *
   * @return array
   *   List of all the configuration names in the target storage.
   */
  protected function getTargetNames() {
    if (empty($this->targetNames)) {
      $this->targetNames = $this->targetStorage
        ->listAll();
    }
    return $this->targetNames;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StorageComparer::$changelist protected property List of changes to between the source storage and the target storage.
StorageComparer::$sourceNames protected property Lists all the configuration object names in the source storage.
StorageComparer::$sourceStorage protected property The source storage used to discover configuration changes.
StorageComparer::$targetNames protected property Lists all the configuration object names in the target storage.
StorageComparer::$targetStorage protected property The target storage used to write configuration changes.
StorageComparer::addChangeList public function Adds changes to the changelist. Overrides StorageComparerInterface::addChangeList
StorageComparer::addChangelistCreate public function Creates the create changelist. Overrides StorageComparerInterface::addChangelistCreate
StorageComparer::addChangelistDelete public function Creates the delete changelist. Overrides StorageComparerInterface::addChangelistDelete
StorageComparer::addChangelistUpdate public function Creates the update changelist. Overrides StorageComparerInterface::addChangelistUpdate
StorageComparer::createChangelist public function Add differences between source and target configuration storage to changelist. Overrides StorageComparerInterface::createChangelist
StorageComparer::getChangelist public function Gets the list of differences to import. Overrides StorageComparerInterface::getChangelist
StorageComparer::getEmptyChangelist public function Gets an empty changelist. Overrides StorageComparerInterface::getEmptyChangelist
StorageComparer::getSourceNames protected function Gets all the configuration names in the source storage.
StorageComparer::getSourceStorage public function Gets the configuration source storage. Overrides StorageComparerInterface::getSourceStorage
StorageComparer::getTargetNames protected function Gets all the configuration names in the target storage.
StorageComparer::getTargetStorage public function Gets the configuration target storage. Overrides StorageComparerInterface::getTargetStorage
StorageComparer::hasChanges public function Checks if there are any operations with changes to process. Overrides StorageComparerInterface::hasChanges
StorageComparer::reset public function Recalculates the differences. Overrides StorageComparerInterface::reset
StorageComparer::__construct public function Constructs the Configuration storage comparer.