Skip to content

Commit

Permalink
Merge pull request #49 from acquia/ACMS-3658
Browse files Browse the repository at this point in the history
ACMS-3658: The drs:init:settings command shouldn't be called when DRS or BLT Updated.
  • Loading branch information
vishalkhode1 authored Mar 26, 2024
2 parents b14e273 + 0dea087 commit 17657f4
Show file tree
Hide file tree
Showing 13 changed files with 412 additions and 90 deletions.
62 changes: 4 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,68 +30,14 @@ on when these events are triggered. You can find the examples of such
implementations from [here](examples).

# Quick examples
## Generate settings for a given site
### Generate settings with default credentials for the default site:
```
<?php
/**
* @file
* Include DRS settings.
*/
use Acquia\Drupal\RecommendedSettings\Exceptions\SettingsException;
use Acquia\Drupal\RecommendedSettings\Settings;
// Create settings object.
$siteUri = "site1";
$settings = new Settings(DRUPAL_ROOT, $siteUri);
try {
// Call generate method.
$settings->generate();
}
catch (SettingsException $e) {
echo $e->getMessage();
}
./vendor/bin/drush init:settings
```

## Generate settings for a given site passing database credentials

### Set up a new site with custom credentials for the local environment:
```
<?php
/**
* @file
* Include DRS settings.
*/
use Acquia\Drupal\RecommendedSettings\Exceptions\SettingsException;
use Acquia\Drupal\RecommendedSettings\Settings;
// Create settings object.
$siteUri = "site1";
$settings = new Settings(DRUPAL_ROOT, $siteUri);
// Database details.
$dbSpec = [
'drupal' => [
'db' => [
'database' => 'drupal', // In case of multi-site database name is replaced with the site name.
'username' => 'drupal',
'password' => 'drupal',
'host' => 'localhost',
'port' => '3306',
],
],
];
try {
// Call generate method passing database details.
$settings->generate($dbSpec);
}
catch (SettingsException $e) {
echo $e->getMessage();
}
./vendor/bin/drush init:settings --database=site1 --username=myuser --password=mypass --host=127.0.0.1 --port=1234 --uri=site1
```

# License
Expand Down
4 changes: 0 additions & 4 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Composer\Composer;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\OperationInterface;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
Expand Down Expand Up @@ -119,9 +118,6 @@ protected function getSettingsPackage(OperationInterface $operation): mixed {
if ($operation instanceof InstallOperation) {
$package = $operation->getPackage();
}
elseif ($operation instanceof UpdateOperation) {
$package = $operation->getTargetPackage();
}
if (isset($package) && $package instanceof PackageInterface && $package->getName() == "acquia/drupal-recommended-settings") {
return $package;
}
Expand Down
45 changes: 21 additions & 24 deletions src/Robo/Tasks/DrushTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DrushTask extends CommandStack {
/**
* Indicates if the command output should be verbose.
*/
protected bool $verbose = FALSE;
protected bool|int $verbose = FALSE;

/**
* Indicates if the command output should be very verbose.
Expand All @@ -71,7 +71,7 @@ class DrushTask extends CommandStack {
/**
* Add or not the --ansi option.
*/
protected bool $ansi = FALSE;
protected bool|NULL $ansi = NULL;

/**
* Drush commands to execute when task is run.
Expand All @@ -90,24 +90,24 @@ class DrushTask extends CommandStack {
/**
* Adds the given drush command to a stack.
*
* @param string $command
* @param string|array<string> $commands
* The drush command to execute. Do NOT include "drush" prefix.
*
* @return $this
*/
public function drush(string $command) {
public function drush(string|array $commands) {
// Clear out options associated with previous drush command.
$this->setOptionsForLastCommand();

if (!$this->defaultsInitialized) {
$this->init();
}

if (is_array($command)) {
$command = implode(' ', array_filter($command));
if (is_array($commands)) {
$commands = implode(' ', array_filter($commands));
}

$this->commands[] = trim($command);
$this->commands[] = trim($commands);
return $this;
}

Expand Down Expand Up @@ -156,7 +156,7 @@ public function dir($dir) {
*
* @return $this
*/
public function verbose(bool $verbose) {
public function verbose(string|bool|int $verbose) {
$this->verbose = $this->mixedToBool($verbose);
return $this;
}
Expand All @@ -169,7 +169,7 @@ public function verbose(bool $verbose) {
*
* @return $this
*/
public function veryVerbose(bool $verbose) {
public function veryVerbose(string|bool $verbose) {
$this->veryVerbose = $this->mixedToBool($verbose);
return $this;
}
Expand All @@ -182,7 +182,7 @@ public function veryVerbose(bool $verbose) {
*
* @return $this
*/
public function debug(bool $verbose) {
public function debug(string|bool $verbose) {
$this->debug = $this->mixedToBool($verbose);
return $this;
}
Expand All @@ -203,12 +203,12 @@ public function includePath(string $path) {
/**
* Include or not the --ansi option for drush commands.
*
* @param bool $ansi
* @param string|bool $ansi
* The flag for including --ansi option.
*
* @return $this
*/
public function ansi(bool $ansi) {
public function ansi(string|bool $ansi) {
$this->ansi = $ansi;
return $this;
}
Expand Down Expand Up @@ -237,7 +237,7 @@ protected function init(): void {
$this->interactive(FALSE);
}
if (!isset($this->ansi)) {
$this->ansi($this->getConfig()->get('drush.ansi'));
$this->ansi($this->getConfig()->get('drush.ansi', FALSE));
}

$this->defaultsInitialized = TRUE;
Expand All @@ -249,14 +249,17 @@ protected function init(): void {
* @param mixed $mixedVar
* Mixed.
*
* @return bool
* @return int|bool
* TRUE/FALSE as per PHP's cast to boolean ruleset, with the exception that
* a string value not equal to 'yes' or 'true' will evaluate to FALSE.
*/
protected function mixedToBool(mixed $mixedVar): bool {
protected function mixedToBool(mixed $mixedVar): int|bool {
if (is_string($mixedVar)) {
$boolVar = ($mixedVar === 'yes' || $mixedVar === 'true');
}
elseif (is_int($mixedVar)) {
$boolVar = $mixedVar;
}
else {
$boolVar = (bool) $mixedVar;
}
Expand Down Expand Up @@ -290,8 +293,8 @@ protected function setGlobalOptions(): void {
$this->option('no-interaction');
}

if ($this->verbose !== FALSE) {
$verbosity_threshold = $this->verbosityThreshold();
if ($this->verbose !== FALSE || ($this->verbosityThreshold() >= OutputInterface::VERBOSITY_VERBOSE)) {
$verbosity_threshold = ($this->verbose !== FALSE) ? $this->verbose : $this->verbosityThreshold();
switch ($verbosity_threshold) {
case OutputInterface::VERBOSITY_VERBOSE:
$this->verbose(TRUE);
Expand All @@ -304,12 +307,9 @@ protected function setGlobalOptions(): void {
case OutputInterface::VERBOSITY_DEBUG:
$this->debug(TRUE);
break;

}
}
if ($this->verbosityThreshold() >= OutputInterface::VERBOSITY_VERBOSE
&& $this->verbose !== FALSE) {
$this->verbose(TRUE);
}

if (($this->debug || $this->getConfig()->get('drush.debug'))
&& $this->getConfig()->get('drush.debug') !== FALSE) {
Expand Down Expand Up @@ -359,9 +359,6 @@ public function option(string $option, string $value = NULL, string $separator =
*/
public function run() {
$this->setupExecution();
if (empty($this->exec)) {
throw new TaskException($this, 'You must add at least one command');
}

// If 'stopOnFail' is not set, or if there is only one command to run,
// then execute the single command to run.
Expand Down
7 changes: 6 additions & 1 deletion src/Robo/Tasks/LoadTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
trait LoadTasks {

/**
* An instance of drush task class.
*/
protected string $drushTaskClass = DrushTask::class;

/**
* Task drush.
*
Expand All @@ -17,7 +22,7 @@ trait LoadTasks {
*/
protected function taskDrush(): CollectionBuilder {
/** @var \Acquia\Drupal\RecommendedSettings\Robo\Tasks\DrushTask $task */
$task = $this->task(DrushTask::class);
$task = $this->task($this->drushTaskClass);
/** @var \Symfony\Component\Console\Output\OutputInterface $output */
$output = $this->output();
$task->setVerbosityThreshold($output->getVerbosity());
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/project/drs/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ drupal:
db:
database: mydatabase
multisites:
- acms
- acms
77 changes: 77 additions & 0 deletions tests/src/CommandsTestBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Acquia\Drupal\RecommendedSettings\Tests;

use Acquia\Drupal\RecommendedSettings\Robo\Config\ConfigAwareTrait;
use Acquia\Drupal\RecommendedSettings\Tests\Helpers\NullLogOutputStylers;
use Consolidation\Log\Logger;
use League\Container\Container;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Robo\Collection\CollectionBuilder;
use Robo\Common\BuilderAwareTrait;
use Robo\Common\OutputAwareTrait;
use Robo\Config\Config;
use Robo\Robo;
use Robo\Tasks;
use Symfony\Component\Console\Output\NullOutput;

/**
* Base commands to test drush commands/tasks.
*/
abstract class CommandsTestBase extends TestCase {
use BuilderAwareTrait;
use OutputAwareTrait;
use ConfigAwareTrait;

/**
* Stores an instance of container object.
*/
protected ContainerInterface $container;

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->createContainer();
}

/**
* Initialize the Container.
*
* @param ContainerInterface|null $container
* An instance of container object or NULL.
*/
protected function createContainer(?ContainerInterface $container = NULL): void {
if (!$container) {
$container = new Container();
$output = new NullOutput();
$this->setOutput($output);

$config = new Config();
$this->setConfig($config);
$logger = new Logger($this->output());
$null_log_output = new NullLogOutputStylers;
$logger->setLogOutputStyler($null_log_output);
$container->add("logger", $logger);

$app = Robo::createDefaultApplication("acquia/drupal-recommended-settings", "1.0.0");
Robo::configureContainer($container, $app, $this->getConfig());

$tasks = new Tasks();
$builder = CollectionBuilder::create($container, $tasks);
$this->setBuilder($builder);
$container->add("builder", $builder);
}
$this->container = $container;
}

/**
* Returns an instance of container object.
*/
protected function getContainer(): ContainerInterface {
return $this->container;
}

}
1 change: 0 additions & 1 deletion tests/src/Functional/Drush/Traits/SiteUriTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class SiteUriTraitTest extends FunctionalTestBase {
protected function setUp(): void {
parent::setUp();
$this->fileSystem = new Filesystem();

$this->setupSite();
}

Expand Down
19 changes: 19 additions & 0 deletions tests/src/Helpers/NullLogOutputStylers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Acquia\Drupal\RecommendedSettings\Tests\Helpers;

use Consolidation\Log\SymfonyLogOutputStyler;

/**
* Class for not logging any error.
*/
class NullLogOutputStylers extends SymfonyLogOutputStyler {

/**
* @inheritdoc
*/
#[Override]
public function error($symfonyStyle, $level, $message, $context): void {
}

}
Loading

0 comments on commit 17657f4

Please sign in to comment.