From 46b347b2a271bdef6f908fce251bf96cd0508100 Mon Sep 17 00:00:00 2001 From: Vishal Khode Date: Thu, 28 Mar 2024 15:02:00 +0530 Subject: [PATCH] ACMS-1890: Update code to replace BLT settinsg to DRS settings. --- src/Robo/Commands/Blt/MigrateToDrsCommand.php | 102 ++++++++---------- src/Robo/Commands/Source/SettingsCommand.php | 2 - src/Robo/Doctor/AcsfCheck.php | 6 +- src/Robo/Doctor/SettingsFilesCheck.php | 4 +- src/Robo/Inspector/Inspector.php | 2 +- tests/phpunit/src/DrupalSettingsTest.php | 2 +- 6 files changed, 50 insertions(+), 68 deletions(-) diff --git a/src/Robo/Commands/Blt/MigrateToDrsCommand.php b/src/Robo/Commands/Blt/MigrateToDrsCommand.php index b7aed1c04..6d7f16905 100644 --- a/src/Robo/Commands/Blt/MigrateToDrsCommand.php +++ b/src/Robo/Commands/Blt/MigrateToDrsCommand.php @@ -12,14 +12,18 @@ class MigrateToDrsCommand extends BltTasks { /** - * Settings warning. + * An array of search replace pairs. * - * @var string - * Warning text added to the end of settings.php to point people to the BLT - * docs on how to include settings. + * @var array|\string[][] */ - private string $bltSettingsWarning = << 'require DRUPAL_ROOT . "/../vendor/acquia/blt/settings/blt.settings.php";', + 'replace' => 'require DRUPAL_ROOT . "/../vendor/acquia/drupal-recommended-settings/settings/acquia-recommended.settings.php";', + 'file' => 'settings.php', + ], + [ + 'search' => << << 'settings.php', + ], + [ + 'search' => 'use Acquia\Blt\Robo\Common\EnvironmentDetector;', + 'replace' => 'use Acquia\Drupal\RecommendedSettings\Helpers\EnvironmentDetector;', + 'file' => 'settings.php', + ], + [ + 'search' => 'blt_override_config_directories', + 'replace' => 'drs_override_config_directories', + 'file' => 'local.settings.php', + ], + ]; /** * Migrate BLT to use DRS. @@ -80,12 +67,15 @@ class MigrateToDrsCommand extends BltTasks { public function migrateDrs(): void { $multiSites = $this->getConfigValue('multisites'); if (!empty($multiSites)) { - $this->io()->warning('This script will update settings.php and local.settings.php files from site [' . implode(',', $multiSites) . '] with following changes.'); - $this->io()->table(['File', 'Snippet to remove', 'Snippet to add'], [ - ['settings.php', $this->bltSettingsWarning, $this->drsSettingsWarning], - ['settings.php', $this->bltConfigOverrideVar, $this->drsConfigOverrideVar], - ['local.settings.php', $this->bltUseStmt, $this->drsUseStmt], - ]); + $rows = array_map(function ($pair) { + return [ + $pair["file"], + $pair["search"], + $pair["replace"], + ]; + }, $this->searchReplacePairs); + $this->io()->warning('This script will update following files from site [' . implode(',', $multiSites) . '] with following changes.'); + $this->io()->table(['File', 'Snippet to remove', 'Snippet to add'], $rows); } // Don't proceed further if aborted by user. @@ -133,22 +123,16 @@ private function processSettingsFile(string $site): void { private function updateSettingsFile(string $settingFile): void { $fileContent = file_get_contents($settingFile); - // Check whether $blt_override_config_directories variable exists. - if (str_contains($fileContent, $this->bltConfigOverrideVar)) { - $fileContent = str_replace($this->bltConfigOverrideVar, $this->drsConfigOverrideVar, $fileContent); - } - // Check if blt use statement exists. - if (str_contains($fileContent, $this->bltUseStmt)) { - $fileContent = str_replace($this->bltUseStmt, $this->drsUseStmt, $fileContent); - } - // Let remove BLT require section from settings.php. - if (substr_count($fileContent, $this->drsSettingsWarning) < 1) { - $fileContent = str_replace($this->bltSettingsWarning, $this->drsSettingsWarning, $fileContent); - } - else { - $fileContent = str_replace($this->bltSettingsWarning, '', $fileContent); + // Loop through the search/replace pairs. + foreach ($this->searchReplacePairs as $pair) { + // Check if the search string exists in the file content. + if (strpos($fileContent, $pair['search']) !== FALSE) { + // Replace the search string with the replace string. + $fileContent = str_replace($pair['search'], $pair['replace'], $fileContent); + } } + // Write the modified content back to the file. file_put_contents($settingFile, $fileContent); } diff --git a/src/Robo/Commands/Source/SettingsCommand.php b/src/Robo/Commands/Source/SettingsCommand.php index 715f93d08..dbcea6f53 100644 --- a/src/Robo/Commands/Source/SettingsCommand.php +++ b/src/Robo/Commands/Source/SettingsCommand.php @@ -7,8 +7,6 @@ use Acquia\Blt\Robo\Config\ConfigInitializer; use Acquia\Blt\Robo\Exceptions\BltException; use Acquia\Drupal\RecommendedSettings\Drush\Commands\SettingsDrushCommands; -use Acquia\Drupal\RecommendedSettings\Exceptions\SettingsException; -use Acquia\Drupal\RecommendedSettings\Settings; use Robo\Contract\VerbosityThresholdInterface; use Robo\ResultData; use Symfony\Component\Console\Input\InputOption; diff --git a/src/Robo/Doctor/AcsfCheck.php b/src/Robo/Doctor/AcsfCheck.php index 9608e56ae..dd3efa05d 100644 --- a/src/Robo/Doctor/AcsfCheck.php +++ b/src/Robo/Doctor/AcsfCheck.php @@ -21,10 +21,10 @@ protected function checkAcsfConfig() { $file_path = $this->getConfigValue('repo.root') . '/factory-hooks/pre-settings-php/includes.php'; if (file_exists($file_path)) { $file_contents = file_get_contents($file_path); - if (!strstr($file_contents, '/../vendor/acquia/blt/settings/blt.settings.php')) { + if (!strstr($file_contents, '/../vendor/acquia/drupal-recommended-settings/settings/acquia-recommended.settings.php')) { $this->logProblem(__FUNCTION__, [ - "BLT settings are not included in your pre-settings-php include.", - " Add a require statement for \"/../vendor/acquia/blt/settings/blt.settings.php\" to $file_path", + "DRS settings are not included in your pre-settings-php include.", + " Add a require statement for \"/../vendor/acquia/drupal-recommended-settings/settings/acquia-recommended.settings.php\" to $file_path", ], 'error'); } } diff --git a/src/Robo/Doctor/SettingsFilesCheck.php b/src/Robo/Doctor/SettingsFilesCheck.php index b41013a77..8246067b5 100644 --- a/src/Robo/Doctor/SettingsFilesCheck.php +++ b/src/Robo/Doctor/SettingsFilesCheck.php @@ -39,8 +39,8 @@ protected function checkDrupalSettingsFile() { } else { if (!$this->getInspector()->isDrupalSettingsFileValid()) { - $this->logProblem('blt-settings', - "BLT settings are not included in settings file.", 'error'); + $this->logProblem('drs-settings', + "DRS settings are not included in settings file.", 'error'); } if (strstr($this->getConfigValue('drupal.settings_file'), '/sites/default/settings/blt.settings.php')) { diff --git a/src/Robo/Inspector/Inspector.php b/src/Robo/Inspector/Inspector.php index cab8eca6a..e6dcfc43a 100644 --- a/src/Robo/Inspector/Inspector.php +++ b/src/Robo/Inspector/Inspector.php @@ -171,7 +171,7 @@ public function isDrupalLocalSettingsFilePresent() { public function isDrupalSettingsFileValid() { $settings_file_contents = file_get_contents($this->getConfigValue('drupal.settings_file')); if (!strstr($settings_file_contents, - '/../vendor/acquia/blt/settings/blt.settings.php') + '/../vendor/acquia/drupal-recommended-settings/settings/acquia-recommended.settings.php') ) { return FALSE; } diff --git a/tests/phpunit/src/DrupalSettingsTest.php b/tests/phpunit/src/DrupalSettingsTest.php index ada0c0f2c..412464627 100644 --- a/tests/phpunit/src/DrupalSettingsTest.php +++ b/tests/phpunit/src/DrupalSettingsTest.php @@ -27,7 +27,7 @@ public function testSetupDefaultLocalSettings() { $this->assertFileExists("$this->sandboxInstance/docroot/sites/$site/settings.php"); $this->assertStringContainsString( - 'require DRUPAL_ROOT . "/../vendor/acquia/blt/settings/blt.settings.php"', + 'require DRUPAL_ROOT . "/../vendor/acquia/drupal-recommended-settings/settings/acquia-recommended.settings.php"', file_get_contents("$this->sandboxInstance/docroot/sites/$site/settings.php") );