Skip to content

Commit

Permalink
ACMS-3507: Added switchSiteContext method to BaseDrush command.
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalkhode1 committed Feb 2, 2024
1 parent 088a195 commit 12c37b6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Config/DefaultDrushConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Acquia\Drupal\RecommendedSettings\Config;

use Consolidation\Config\Config;
use Drush\Config\DrushConfig;
use Consolidation\Config\ConfigInterface;

/**
* The configuration for settings.
Expand All @@ -16,7 +16,7 @@ class DefaultDrushConfig extends Config {
* @param string[] $data
* Data array, if available.
*/
public function __construct(DrushConfig $config) {
public function __construct(ConfigInterface $config) {
$uri = $config->get("options.uri") ?? "default";
$config->set('repo.root', $config->get("runtime.project"));
$config->set('docroot', $config->get("options.root"));
Expand Down
22 changes: 18 additions & 4 deletions src/Drush/Commands/BaseDrushCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ class BaseDrushCommands extends DrushCommands implements ConfigAwareInterface, L
*/
#[CLI\Hook(type: HookManager::INITIALIZE)]
public function init(InputInterface $input, AnnotationData $annotationData): void {
$config = new DefaultDrushConfig($this->getConfig());
$configInitializer = new ConfigInitializer($config);
$config = $configInitializer->loadAllConfig()->processConfig();
$this->setConfig($config);
$this->switchSiteContext("default");
}

/**
Expand Down Expand Up @@ -101,4 +98,21 @@ protected function invokeCommand(string $command_name, array $args = []): void {
});
}

/**
* Sets multisite context by settings site-specific config values.
*
* @param string $site_name
* The name of a multisite, e.g., if docroot/sites/example.com is the site,
* $site_name would be example.com.
*/
public function switchSiteContext(string $site_name) {
$this->logger->debug("Switching site context to <comment>$site_name</comment>.");
$config = new DefaultDrushConfig($this->getConfig());
$configInitializer = new ConfigInitializer($config);
$config = $configInitializer->loadAllConfig()->processConfig();
$config->set('drush.uri', $site_name);
$config->set('site', $site_name);
$this->setConfig($config);
}

}

0 comments on commit 12c37b6

Please sign in to comment.