diff --git a/.docker/main/composer.json b/.docker/main/composer.json index eba4dca..615684c 100644 --- a/.docker/main/composer.json +++ b/.docker/main/composer.json @@ -40,7 +40,8 @@ "composer/installers": true, "drupal/core-composer-scaffold": true, "drupal/core-project-message": true, - "drupal/core-vendor-hardening": true + "drupal/core-vendor-hardening": true, + "dealerdirect/phpcodesniffer-composer-installer": true } }, "extra": { diff --git a/README.md b/README.md index bf499b4..dc4fcc4 100644 --- a/README.md +++ b/README.md @@ -256,6 +256,32 @@ do done; ``` +### site:keys + +Get a list of all keys in `$sites`. Usually, these are site URIs. + +#### Example: Usage + +```shell +$ drall site:keys +tmnt.com +donatello.com +leonardo.com +michelangelo.com +raphael.com +``` + +The output can then be iterated with scripts. + +#### Example: Iterating + +```shell +for site in $(drall site:keys) +do + echo "Current site: $site"; +done; +``` + ### site:aliases Get a list of site aliases. diff --git a/composer.json b/composer.json index 5ea01ea..cf989ef 100644 --- a/composer.json +++ b/composer.json @@ -53,6 +53,9 @@ }, "sort-packages": true }, + "extra": { + "phpcodesniffer-search-depth": 5 + }, "scripts": { "lint": "composer exec phpcs", "test": "XDEBUG_MODE=coverage composer exec phpunit" diff --git a/src/Command/BaseCommand.php b/src/Command/BaseCommand.php index b67ebb1..2c99ff3 100644 --- a/src/Command/BaseCommand.php +++ b/src/Command/BaseCommand.php @@ -5,9 +5,9 @@ use Drall\Trait\SiteDetectorAwareTrait; use Psr\Log\LoggerAwareTrait; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Logger\ConsoleLogger; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Logger\ConsoleLogger; use Symfony\Component\Console\Output\OutputInterface; abstract class BaseCommand extends Command { diff --git a/src/Command/ExecCommand.php b/src/Command/ExecCommand.php index b140365..b442751 100644 --- a/src/Command/ExecCommand.php +++ b/src/Command/ExecCommand.php @@ -50,9 +50,10 @@ protected function configure() { $this->setAliases(['ex']); $this->setDescription('Execute a command on multiple Drupal sites.'); $this->addUsage('drush core:status'); - $this->addUsage('--drall-group=bluish drush core:status'); - $this->addUsage('--drall-workers=4 drush cache:rebuild'); $this->addUsage('./vendor/bin/drush core:status'); + $this->addUsage('--drall-group=GROUP drush core:status'); + $this->addUsage('--drall-filter=FILTER drush core:status'); + $this->addUsage('--drall-workers=4 drush cache:rebuild'); $this->addUsage('ls web/sites/@@dir/settings.php'); $this->addUsage('echo "Working on @@site" && drush @@site.local core:status'); diff --git a/src/Command/SiteAliasesCommand.php b/src/Command/SiteAliasesCommand.php index bfd09c0..701c820 100644 --- a/src/Command/SiteAliasesCommand.php +++ b/src/Command/SiteAliasesCommand.php @@ -15,9 +15,10 @@ protected function configure() { $this->setName('site:aliases'); $this->setAliases(['sa']); - $this->setDescription('Get a list of site aliases.'); + $this->setDescription('List all Drush site aliases.'); $this->addUsage('site:aliases'); $this->addUsage('--drall-group=GROUP site:aliases'); + $this->addUsage('--drall-filter=FILTER site:aliases'); } protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/src/Command/SiteDirectoriesCommand.php b/src/Command/SiteDirectoriesCommand.php index 0e0b6a5..f993974 100644 --- a/src/Command/SiteDirectoriesCommand.php +++ b/src/Command/SiteDirectoriesCommand.php @@ -6,7 +6,7 @@ use Symfony\Component\Console\Output\OutputInterface; /** - * A command to get a list of site directories in the Drupal installation. + * A command to get a list of values in the $sites array. */ class SiteDirectoriesCommand extends BaseCommand { @@ -15,7 +15,7 @@ protected function configure() { $this->setName('site:directories'); $this->setAliases(['sd']); - $this->setDescription('Get a list of site directories.'); + $this->setDescription('List the values of the $sites array.'); $this->addUsage('site:directories'); $this->addUsage('--drall-group=GROUP site:directories'); } diff --git a/src/Command/SiteKeysCommand.php b/src/Command/SiteKeysCommand.php new file mode 100644 index 0000000..550438c --- /dev/null +++ b/src/Command/SiteKeysCommand.php @@ -0,0 +1,45 @@ +setName('site:keys'); + $this->setAliases(['sk']); + $this->setDescription('List the keys of the $sites array.'); + $this->addUsage('site:keys'); + $this->addUsage('--drall-group=GROUP site:keys'); + $this->addUsage('--drall-filter=FILTER site:keys'); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $this->preExecute($input, $output); + + $keys = $this->siteDetector() + ->getSiteKeys( + $this->getDrallGroup($input), + $this->getDrallFilter($input), + ); + + if (count($keys) === 0) { + $this->logger->warning('No Drupal sites found.'); + return 0; + } + + foreach ($keys as $key) { + $output->writeln($key); + } + + return 0; + } + +} diff --git a/src/Drall.php b/src/Drall.php index 597c642..97f127d 100644 --- a/src/Drall.php +++ b/src/Drall.php @@ -4,13 +4,14 @@ use Consolidation\SiteAlias\SiteAliasManager; use Drall\Command\ExecCommand; -use Drall\Command\SiteDirectoriesCommand; use Drall\Command\SiteAliasesCommand; +use Drall\Command\SiteDirectoriesCommand; +use Drall\Command\SiteKeysCommand; use Drall\Model\EnvironmentId; use Drall\Service\SiteDetector; use Drall\Trait\SiteDetectorAwareTrait; -use Drush\SiteAlias\SiteAliasFileLoader; use DrupalFinder\DrupalFinder; +use Drush\SiteAlias\SiteAliasFileLoader; use Symfony\Component\Console\Application; use Symfony\Component\Console\Exception\CommandNotFoundException; use Symfony\Component\Console\Input\ArgvInput; @@ -52,6 +53,10 @@ public function __construct( $cmd->setSiteDetector($siteDetector); $this->add($cmd); + $cmd = new SiteKeysCommand(); + $cmd->setSiteDetector($siteDetector); + $this->add($cmd); + $cmd = new SiteAliasesCommand(); $cmd->setSiteDetector($siteDetector); $this->add($cmd); diff --git a/src/Service/SiteDetector.php b/src/Service/SiteDetector.php index 55ce102..e0c1cc3 100644 --- a/src/Service/SiteDetector.php +++ b/src/Service/SiteDetector.php @@ -6,9 +6,9 @@ use Consolidation\Filter\LogicalOpFactory; use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; use Consolidation\SiteAlias\SiteAliasManagerInterface; +use Drall\Model\SitesFile; use Drall\Trait\DrupalFinderAwareTrait; use DrupalFinder\DrupalFinder; -use Drall\Model\SitesFile; class SiteDetector { diff --git a/test/Integration/Command/SiteAliasesCommandTest.php b/test/Integration/Command/SiteAliasesCommandTest.php index 3c69555..37cf644 100644 --- a/test/Integration/Command/SiteAliasesCommandTest.php +++ b/test/Integration/Command/SiteAliasesCommandTest.php @@ -34,7 +34,7 @@ public function testExecute(): void { } /** - * Run site:aliases with --filter. + * Run site:aliases with --drall-filter. */ public function testExecuteWithFilter(): void { $output = shell_exec('drall site:aliases --drall-filter="leo||ralph"'); diff --git a/test/Integration/Command/SiteDirectoriesCommandTest.php b/test/Integration/Command/SiteDirectoriesCommandTest.php index 0e0f9af..7e20f00 100644 --- a/test/Integration/Command/SiteDirectoriesCommandTest.php +++ b/test/Integration/Command/SiteDirectoriesCommandTest.php @@ -34,7 +34,7 @@ public function testExecute(): void { } /** - * Run site:directories with --filter. + * Run site:directories with --drall-filter. */ public function testExecuteWithFilter(): void { $output = shell_exec('drall site:directories --drall-filter="leo||ralph"'); diff --git a/test/Integration/Command/SiteKeysCommandTest.php b/test/Integration/Command/SiteKeysCommandTest.php new file mode 100644 index 0000000..1a2434a --- /dev/null +++ b/test/Integration/Command/SiteKeysCommandTest.php @@ -0,0 +1,128 @@ +assertOutputEquals("[warning] No Drupal sites found." . PHP_EOL, $output); + } + + /** + * Run site:keys with a Drupal installation. + */ + public function testExecute(): void { + $output = shell_exec('drall site:keys'); + $this->assertOutputEquals(<<assertOutputEquals(<<assertOutputEquals(<<assertOutputEquals(<<assertEquals(<<assertEquals(<<getMockBuilder(SiteDetector::class) + ->setConstructorArgs([$drupalFinder, $siteAliasManager]) + ->onlyMethods(['getSiteKeys']) + ->getMock(); + $siteDetectorMock + ->expects($this->once()) + ->method('getSiteKeys') + ->willReturn(['donatello.com', 'leonardo.com']); + + $app = new Drall($siteDetectorMock); + $tester = new CommandTester($app->find('site:keys')); + $tester->execute([]); + + $tester->assertCommandIsSuccessful(); + + $this->assertEquals( + <<getDisplay() + ); + } + + public function testExecuteWithGroup() { + $drupalFinder = new DrupalFinder(); + $siteAliasManager = new SiteAliasManager(); + + $siteDetectorMock = $this->getMockBuilder(SiteDetector::class) + ->setConstructorArgs([$drupalFinder, $siteAliasManager]) + ->onlyMethods(['getSiteKeys']) + ->getMock(); + $siteDetectorMock + ->expects($this->once()) + ->method('getSiteKeys') + ->with('bluish') + ->willReturn(['tmnt.com']); + + $app = new Drall($siteDetectorMock); + $tester = new CommandTester($app->find('site:keys')); + $tester->execute(['--drall-group' => 'bluish']); + + $tester->assertCommandIsSuccessful(); + + $this->assertEquals( + <<getDisplay() + ); + } + + public function testExecuteWithNoSiteDirectories() { + $drupalFinder = new DrupalFinder(); + $siteAliasManager = new SiteAliasManager(); + + $siteDetectorMock = $this->getMockBuilder(SiteDetector::class) + ->setConstructorArgs([$drupalFinder, $siteAliasManager]) + ->onlyMethods(['getSiteKeys']) + ->getMock(); + $siteDetectorMock + ->expects($this->once()) + ->method('getSiteKeys') + ->willReturn([]); + + $app = new Drall($siteDetectorMock); + $tester = new CommandTester($app->find('site:keys')); + $tester->execute([]); + + $tester->assertCommandIsSuccessful(); + + $this->assertEquals( + '[warning] No Drupal sites found.' . PHP_EOL, + $tester->getDisplay() + ); + } + +}