View source
<?php
namespace Drupal\Core\Config;
use Drupal\Core\Cache\MemoryBackend;
use Drupal\Core\Config\Entity\ConfigDependencyManager;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
class StorageComparer implements StorageComparerInterface {
use DependencySerializationTrait;
protected $sourceStorage;
protected $sourceStorages;
protected $targetStorage;
protected $targetStorages;
protected $changelist;
protected $sourceNames = [];
protected $targetNames = [];
protected $sourceCacheStorage;
protected $targetCacheStorage;
public function __construct(StorageInterface $source_storage, StorageInterface $target_storage, ConfigManagerInterface $config_manager = NULL) {
$this->sourceCacheStorage = new MemoryBackend();
$this->sourceStorage = new CachedStorage($source_storage, $this->sourceCacheStorage);
$this->targetCacheStorage = new MemoryBackend();
$this->targetStorage = new CachedStorage($target_storage, $this->targetCacheStorage);
$this->changelist[StorageInterface::DEFAULT_COLLECTION] = $this
->getEmptyChangelist();
if ($config_manager !== NULL) {
@trigger_error('The storage comparer does not need a config manager. The parameter is deprecated since version 8.7.0 and will be removed in 9.0.0. Omit the third parameter. See https://www.drupal.org/node/2993271.', E_USER_DEPRECATED);
}
}
public function getSourceStorage($collection = StorageInterface::DEFAULT_COLLECTION) {
if (!isset($this->sourceStorages[$collection])) {
if ($collection == StorageInterface::DEFAULT_COLLECTION) {
$this->sourceStorages[$collection] = $this->sourceStorage;
}
else {
$this->sourceStorages[$collection] = $this->sourceStorage
->createCollection($collection);
}
}
return $this->sourceStorages[$collection];
}
public function getTargetStorage($collection = StorageInterface::DEFAULT_COLLECTION) {
if (!isset($this->targetStorages[$collection])) {
if ($collection == StorageInterface::DEFAULT_COLLECTION) {
$this->targetStorages[$collection] = $this->targetStorage;
}
else {
$this->targetStorages[$collection] = $this->targetStorage
->createCollection($collection);
}
}
return $this->targetStorages[$collection];
}
public function getEmptyChangelist() {
return [
'create' => [],
'update' => [],
'delete' => [],
'rename' => [],
];
}
public function getChangelist($op = NULL, $collection = StorageInterface::DEFAULT_COLLECTION) {
if ($op) {
return $this->changelist[$collection][$op];
}
return $this->changelist[$collection];
}
protected function addChangeList($collection, $op, array $changes, array $sort_order = NULL) {
$changes = array_diff($changes, $this->changelist[$collection][$op]);
$this->changelist[$collection][$op] = array_merge($this->changelist[$collection][$op], $changes);
if (isset($sort_order)) {
$count = count($this->changelist[$collection][$op]);
$this->changelist[$collection][$op] = array_values(array_intersect($sort_order, $this->changelist[$collection][$op]));
if ($count != count($this->changelist[$collection][$op])) {
throw new \InvalidArgumentException("Sorting the {$op} changelist should not change its length.");
}
}
}
public function createChangelist() {
foreach ($this
->getAllCollectionNames() as $collection) {
$this->changelist[$collection] = $this
->getEmptyChangelist();
$this
->getAndSortConfigData($collection);
$this
->addChangelistCreate($collection);
$this
->addChangelistUpdate($collection);
$this
->addChangelistDelete($collection);
if ($collection == StorageInterface::DEFAULT_COLLECTION) {
$this
->addChangelistRename($collection);
}
}
return $this;
}
protected function addChangelistDelete($collection) {
$deletes = array_diff(array_reverse($this->targetNames[$collection]), $this->sourceNames[$collection]);
$this
->addChangeList($collection, 'delete', $deletes);
}
protected function addChangelistCreate($collection) {
$creates = array_diff($this->sourceNames[$collection], $this->targetNames[$collection]);
$this
->addChangeList($collection, 'create', $creates);
}
protected function addChangelistUpdate($collection) {
$recreates = [];
foreach (array_intersect($this->sourceNames[$collection], $this->targetNames[$collection]) as $name) {
$source_data = $this
->getSourceStorage($collection)
->read($name);
$target_data = $this
->getTargetStorage($collection)
->read($name);
if ($source_data !== $target_data) {
if (isset($source_data['uuid']) && $source_data['uuid'] !== $target_data['uuid']) {
$recreates[] = $name;
}
else {
$this
->addChangeList($collection, 'update', [
$name,
]);
}
}
}
if (!empty($recreates)) {
$this
->addChangeList($collection, 'create', $recreates, $this->sourceNames[$collection]);
$this
->addChangeList($collection, 'delete', $recreates, array_reverse($this->targetNames[$collection]));
}
}
protected function addChangelistRename($collection) {
$create_list = $this
->getChangelist('create', $collection);
$delete_list = $this
->getChangelist('delete', $collection);
if (empty($create_list) || empty($delete_list)) {
return;
}
$create_uuids = [];
foreach ($this->sourceNames[$collection] as $name) {
$data = $this
->getSourceStorage($collection)
->read($name);
if (isset($data['uuid']) && in_array($name, $create_list)) {
$create_uuids[$data['uuid']] = $name;
}
}
if (empty($create_uuids)) {
return;
}
$renames = [];
foreach ($this->targetNames[$collection] as $name) {
$data = $this
->getTargetStorage($collection)
->read($name);
if (isset($data['uuid']) && isset($create_uuids[$data['uuid']])) {
$this
->removeFromChangelist($collection, 'create', $create_uuids[$data['uuid']]);
$this
->removeFromChangelist($collection, 'delete', $name);
$renames[] = $this
->createRenameName($name, $create_uuids[$data['uuid']]);
}
}
$this
->addChangeList($collection, 'rename', $renames);
}
protected function removeFromChangelist($collection, $op, $name) {
$key = array_search($name, $this->changelist[$collection][$op]);
if ($key !== FALSE) {
unset($this->changelist[$collection][$op][$key]);
}
}
public function moveRenameToUpdate($rename, $collection = StorageInterface::DEFAULT_COLLECTION) {
$names = $this
->extractRenameNames($rename);
$this
->removeFromChangelist($collection, 'rename', $rename);
$this
->addChangeList($collection, 'update', [
$names['new_name'],
], $this->sourceNames[$collection]);
}
public function reset() {
$this->changelist = [
StorageInterface::DEFAULT_COLLECTION => $this
->getEmptyChangelist(),
];
$this->sourceNames = $this->targetNames = [];
$this->sourceCacheStorage
->deleteAll();
$this->targetCacheStorage
->deleteAll();
return $this
->createChangelist();
}
public function hasChanges() {
foreach ($this
->getAllCollectionNames() as $collection) {
foreach ([
'delete',
'create',
'update',
'rename',
] as $op) {
if (!empty($this->changelist[$collection][$op])) {
return TRUE;
}
}
}
return FALSE;
}
public function validateSiteUuid() {
$source = $this->sourceStorage
->read('system.site');
$target = $this->targetStorage
->read('system.site');
return $source && $target && $source['uuid'] === $target['uuid'];
}
protected function getAndSortConfigData($collection) {
$source_storage = $this
->getSourceStorage($collection);
$target_storage = $this
->getTargetStorage($collection);
$target_names = $target_storage
->listAll();
$source_names = $source_storage
->listAll();
$target_data = $target_storage
->readMultiple($target_names);
$source_data = $source_storage
->readMultiple($source_names);
if ($collection == StorageInterface::DEFAULT_COLLECTION) {
$dependency_manager = new ConfigDependencyManager();
$this->targetNames[$collection] = $dependency_manager
->setData($target_data)
->sortAll();
$this->sourceNames[$collection] = $dependency_manager
->setData($source_data)
->sortAll();
}
else {
$this->targetNames[$collection] = $target_names;
$this->sourceNames[$collection] = $source_names;
}
}
protected function createRenameName($old_name, $new_name) {
return $old_name . '::' . $new_name;
}
public function extractRenameNames($name) {
$names = explode('::', $name, 2);
return [
'old_name' => $names[0],
'new_name' => $names[1],
];
}
public function getAllCollectionNames($include_default = TRUE) {
$collections = array_unique(array_merge($this->sourceStorage
->getAllCollectionNames(), $this->targetStorage
->getAllCollectionNames()));
if ($include_default) {
array_unshift($collections, StorageInterface::DEFAULT_COLLECTION);
}
return $collections;
}
}