Skip to content

Instantly share code, notes, and snippets.

@philipnorton42
Last active December 2, 2018 19:31
Show Gist options
  • Save philipnorton42/d8162c7e232256e35b491bcc3d257a17 to your computer and use it in GitHub Desktop.
Save philipnorton42/d8162c7e232256e35b491bcc3d257a17 to your computer and use it in GitHub Desktop.
A stand alone script that can be used to compare and copy configuration from two different sub-sites into a default configuration.
<?php
require './vendor/autoload.php';
// Set up the directories needed.
$parentDirectory = 'config/siteone';
$siblingDirectory = 'config/sitetwo';
$destinationDirectory = 'config/default';
$parentConfigSplitFile = $destinationDirectory . '/config_split.config_split.siteone.yml';
$siblingConfigSplitFile = $destinationDirectory . '/config_split.config_split.sitetwo.yml';
// Name and clear out the log files.
$updateFile = 'configUpdate.txt';
file_put_contents($updateFile, '');
// Set up the config type lists.
$parentBlacklist = [];
$parentGraylist = [];
$siblingBlacklist = [];
$siblingGraylist = [];
// Load all of the files in the parent directory to check them.
$files = array_diff(scandir($parentDirectory), array('..', '.'));
foreach ($files as $filename) {
// Check to see if we have a yml file.
if (strstr($filename, '.yml') !== FALSE) {
// Extract the configuration name.
$configName = str_replace('.yml', '', $filename);
if (file_exists($siblingDirectory . '/' . $filename)) {
// File also exists in the sibling directory.
// Get the contents of both of the files.
$parentFileContents = file_get_contents($parentDirectory . '/' . $filename);
$parentArray = \Drupal\Core\Serialization\Yaml::decode($parentFileContents);
$siblingFileContents = file_get_contents($siblingDirectory . '/' . $filename);
$siblingArray = \Drupal\Core\Serialization\Yaml::decode($siblingFileContents);
// Calculate the difference between the two files.
$difference = \Drupal\Component\Utility\DiffArray::diffAssocRecursive($parentArray, $siblingArray);
if (count($difference) == 1 && isset($difference['uuid'])) {
// If we have exactly 1 difference and that difference is the uuid then process it.
// Extract the uuid from the parent and write this to a file.
$uuid = $parentArray['uuid'];
$output = "'" . $configName . "' => '" . $uuid . "'," . PHP_EOL;
file_put_contents($updateFile, $output, FILE_APPEND);
// Copy the parent file to the destination directory.
copy($parentDirectory . '/' . $filename, $destinationDirectory . '/' . $filename);
// Delete both of the files.
unlink($parentDirectory . '/' . $filename);
unlink($siblingDirectory . '/' . $filename);
}
elseif (count($difference) > 0 && !isset($difference['uuid'])) {
// This is not a uuid change and as it's different we just need to gray list and copy this version to
// the default config.
$parentGraylist[] = $configName;
$siblingGraylist[] = $configName;
copy($parentDirectory . '/' . $filename, $destinationDirectory . '/' . $filename);
}
elseif (count($difference) == 0) {
// If we have exactly no difference between the two files then just move it.
// Copy the parent file to the destination directory.
copy($parentDirectory . '/' . $filename, $destinationDirectory . '/' . $filename);
// Delete both of the files.
unlink($parentDirectory . '/' . $filename);
unlink($siblingDirectory . '/' . $filename);
}
}
else {
// Config doesn't exist in sibling, make sure it doesn't also exist in the default configuration.
if (!file_exists($destinationDirectory . '/' . $filename)) {
// Add it to blacklist.
$parentBlacklist[] = $configName;
}
}
}
}
// Run the same checks on the sibling directory.
$files = array_diff(scandir($siblingDirectory), array('..', '.'));
foreach ($files as $filename) {
// Check to see if we have a yml file.
if (strstr($filename, '.yml') !== FALSE) {
// Extract the configuration name.
$configName = str_replace('.yml', '', $filename);
if (!file_exists($parentDirectory . '/' . $filename)) {
if (!file_exists($destinationDirectory . '/' . $filename)) {
$siblingBlacklist[] = $configName;
}
}
}
}
// Export the data to our config split files.
if (count($parentBlacklist) > 0 || count($parentGraylist) > 0) {
$parentConfigSplitFileContents = file_get_contents($parentConfigSplitFile);
$parentConfigSplit = \Drupal\Core\Serialization\Yaml::decode($parentConfigSplitFileContents);
if (count($parentBlacklist) > 0) {
$parentConfigSplit['blacklist'] = array_unique(array_merge($parentConfigSplit['blacklist'], $parentBlacklist));
sort($parentConfigSplit['blacklist']);
}
if (count($parentGraylist) > 0) {
$parentConfigSplit['graylist'] = array_unique(array_merge($parentConfigSplit['graylist'], $parentGraylist));
sort($parentConfigSplit['graylist']);
}
$parentConfigSplitFileContents = \Drupal\Core\Serialization\Yaml::encode($parentConfigSplit);
file_put_contents($parentConfigSplitFile, $parentConfigSplitFileContents);
}
if (count($siblingBlacklist) > 0 || count($siblingGraylist) > 0) {
$siblingConfigSplitFileContents = file_get_contents($siblingConfigSplitFile);
$siblingConfigSplit = \Drupal\Core\Serialization\Yaml::decode($siblingConfigSplitFileContents);
if (count($parentBlacklist) > 0) {
$siblingConfigSplit['blacklist'] = array_unique(array_merge($siblingConfigSplit['blacklist'], $siblingBlacklist));
sort($siblingConfigSplit['blacklist']);
}
if (count($parentGraylist) > 0) {
$siblingConfigSplit['graylist'] = array_unique(array_merge($siblingConfigSplit['graylist'], $siblingGraylist));
sort($siblingConfigSplit['graylist']);
}
$siblingConfigSplitFileContents = \Drupal\Core\Serialization\Yaml::encode($siblingConfigSplit);
file_put_contents($siblingConfigSplitFile, $siblingConfigSplitFileContents);
}
echo 'Done.' . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment