Skip to content

Commit

Permalink
ACMS-1890: Update code to replace BLT settinsg to DRS settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalkhode1 committed Mar 28, 2024
1 parent 186e0c2 commit b1e032b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 70 deletions.
106 changes: 45 additions & 61 deletions src/Robo/Commands/Blt/MigrateToDrsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,28 @@
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 = <<<WARNING
require DRUPAL_ROOT . "/../vendor/acquia/blt/settings/blt.settings.php";
private array $searchReplacePairs = [
[
'search' => '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' => <<<BLT_REQUIRE
/**
* IMPORTANT.
*
* Do not include additional settings here. Instead, add them to settings
* included by `blt.settings.php`. See BLT's documentation for more detail.
* included by `acquia-recommended.settings.php`. See Acquia's documentation for more detail.
*
* @link https://docs.acquia.com/blt/
* @link https://docs.acquia.com/
*/
WARNING;

/**
* Settings warning.
*
* @var string
* Warning text added to the end of settings.php to point people
* to the Acquia Drupal Recommended Settings
* docs on how to include settings.
*/
private string $drsSettingsWarning = <<<WARNING
require DRUPAL_ROOT . "/../vendor/acquia/drupal-recommended-settings/settings/acquia-recommended.settings.php";
BLT_REQUIRE,
'replace' => <<<DRS_REQUIRE
/**
* IMPORTANT.
*
Expand All @@ -48,27 +42,20 @@ class MigrateToDrsCommand extends BltTasks {
*
* @link https://docs.acquia.com/
*/
WARNING;

/**
* Blt use statement.
*/
private string $bltUseStmt = 'use Acquia\Blt\Robo\Common\EnvironmentDetector;';

/**
* Drs use statement.
*/
private string $drsUseStmt = 'use Acquia\Drupal\RecommendedSettings\Helpers\EnvironmentDetector;';

/**
* Blt config override variable.
*/
private string $bltConfigOverrideVar = 'blt_override_config_directories';

/**
* Drs config override variable.
*/
private string $drsConfigOverrideVar = 'drs_override_config_directories';
DRS_REQUIRE,
'file' => '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.
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 0 additions & 2 deletions src/Robo/Commands/Source/SettingsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Robo/Doctor/AcsfCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Robo/Doctor/SettingsFilesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Robo/Inspector/Inspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/src/DrupalSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")
);

Expand Down

0 comments on commit b1e032b

Please sign in to comment.