Skip to content

Commit

Permalink
Create SiteDetector in BaseCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
jigarius committed Dec 13, 2023
1 parent f203a8e commit bba6a4f
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 72 deletions.
7 changes: 7 additions & 0 deletions src/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drall\Command;

use Drall\Service\SiteDetector;
use Drall\Trait\SiteDetectorAwareTrait;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -68,6 +69,12 @@ protected function preExecute(InputInterface $input, OutputInterface $output) {
$this->logger = new ConsoleLogger($output);
}

if (!$this->hasSiteDetector()) {
$root = $input->getParameterOption('--root') ?: getcwd();
$siteDetector = SiteDetector::create($root);
$this->setSiteDetector($siteDetector);
}

if ($group = $this->getDrallGroup($input)) {
$this->logger->debug('Detected group: {group}', ['group' => $group]);
}
Expand Down
48 changes: 5 additions & 43 deletions src/Drall.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@

namespace Drall;

use Consolidation\SiteAlias\SiteAliasManager;
use Drall\Command\ExecCommand;
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 DrupalFinder\DrupalFinder;
use Drush\SiteAlias\SiteAliasFileLoader;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -31,39 +26,16 @@ final class Drall extends Application {
/**
* Creates a Phpake Application instance.
*/
public function __construct(
SiteDetector $siteDetector = NULL,
?InputInterface $input = NULL
) {
public function __construct() {
parent::__construct();
$this->setName(self::NAME);
$this->setVersion(self::VERSION);
$this->setAutoExit(FALSE);

// @todo Instead of using $input to create a SiteDetector here, we can
// create the SiteDetector in BaseCommand::preExecute(). That way,
// we won't need this extra dependency injection, thereby simplifying
// the code and the tests.
$input = $input ?? new ArgvInput();
$root = $input->getParameterOption('--root') ?: getcwd();
$siteDetector ??= $this->createDefaultSiteDetector($root);
$this->setSiteDetector($siteDetector);

$cmd = new SiteDirectoriesCommand();
$cmd->setSiteDetector($siteDetector);
$this->add($cmd);

$cmd = new SiteKeysCommand();
$cmd->setSiteDetector($siteDetector);
$this->add($cmd);

$cmd = new SiteAliasesCommand();
$cmd->setSiteDetector($siteDetector);
$this->add($cmd);

$cmd = new ExecCommand();
$cmd->setSiteDetector($siteDetector);
$this->add($cmd);
$this->add(new SiteDirectoriesCommand());
$this->add(new SiteKeysCommand());
$this->add(new SiteAliasesCommand());
$this->add(new ExecCommand());
}

protected function configureIO(InputInterface $input, OutputInterface $output) {
Expand Down Expand Up @@ -110,16 +82,6 @@ protected function getDefaultInputDefinition(): InputDefinition {
return $definition;
}

private function createDefaultSiteDetector(string $root): SiteDetector {
$drupalFinder = new DrupalFinder();
$drupalFinder->locateRoot($root);

$siteAliasManager = new SiteAliasManager(new SiteAliasFileLoader());
$siteAliasManager->addSearchLocation($drupalFinder->getComposerRoot() . '/drush/sites');

return new SiteDetector($drupalFinder, $siteAliasManager);
}

public function find($name) {
try {
return parent::find($name);
Expand Down
21 changes: 21 additions & 0 deletions src/Service/SiteDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

use Consolidation\Filter\FilterOutputData;
use Consolidation\Filter\LogicalOpFactory;
use Consolidation\SiteAlias\SiteAliasManager;
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
use Consolidation\SiteAlias\SiteAliasManagerInterface;
use Drall\Model\SitesFile;
use Drall\Trait\DrupalFinderAwareTrait;
use DrupalFinder\DrupalFinder;
use Drush\SiteAlias\SiteAliasFileLoader;

class SiteDetector {

Expand Down Expand Up @@ -211,6 +213,25 @@ private function filter(
return $result;
}

/**
* Create a SiteDetector given a Drupal project root.
*
* @param string $root
* Composer project root directory.
*
* @return static
* A SiteDetector.
*/
public static function create(string $root): static {
$drupalFinder = new DrupalFinder();
$drupalFinder->locateRoot($root);

$siteAliasManager = new SiteAliasManager(new SiteAliasFileLoader());
$siteAliasManager->addSearchLocation($drupalFinder->getComposerRoot() . '/drush/sites');

return new SiteDetector($drupalFinder, $siteAliasManager);
}

/**
* Detects sites in a "sites" directory.
*
Expand Down
17 changes: 10 additions & 7 deletions test/Unit/Command/ExecCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ public function testNoSitesFound() {
->method('getSiteDirNames')
->willReturn([]);

$app = new Drall($siteDetectorMock);
$app = new Drall();
$input = ['cmd' => 'cat @@dir'];
$command = $app->find('exec')
->setArgv(self::arrayInputAsArgv($input));
/** @var \Drall\Command\ExecCommand $command */
$command = $app->find('exec');
$command->setSiteDetector($siteDetectorMock);
$command->setArgv(self::arrayInputAsArgv($input));
$tester = new CommandTester($command);
$tester->execute($input);

Expand All @@ -54,11 +56,12 @@ public function testNonZeroExitCode() {
->method('getSiteAliasNames')
->willReturn(['@splinter', '@shredder']);

$app = new Drall($siteDetectorMock);
$app = new Drall();
$input = ['cmd' => 'drush @@site.dev core:rebuild'];
/** @var ExecCommand $command */
$command = $app->find('exec')
->setArgv(self::arrayInputAsArgv($input));
/** @var \Drall\Command\ExecCommand $command */
$command = $app->find('exec');
$command->setSiteDetector($siteDetectorMock);
$command->setArgv(self::arrayInputAsArgv($input));
$tester = new CommandTester($command);

$this->assertEquals(1, $tester->execute($input));
Expand Down
24 changes: 15 additions & 9 deletions test/Unit/Command/SiteAliasesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Drall\Service\SiteDetector;
use Drall\TestCase;
use DrupalFinder\DrupalFinder;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Tester\CommandTester;

/**
Expand All @@ -27,8 +26,11 @@ public function testExecute() {
->method('getSiteAliases')
->willReturn(['@leo.local', '@ralph.local']);

$app = new Drall($siteDetectorMock);
$tester = new CommandTester($app->find('site:aliases'));
$app = new Drall();
/** @var \Drall\Command\SiteAliasesCommand $command */
$command = $app->find('site:aliases');
$command->setSiteDetector($siteDetectorMock);
$tester = new CommandTester($command);
$tester->execute([]);

$tester->assertCommandIsSuccessful();
Expand Down Expand Up @@ -58,8 +60,11 @@ public function testExecuteWithGroup() {
->with('bluish')
->willReturn(['@tmnt.local']);

$app = new Drall($siteDetectorMock);
$tester = new CommandTester($app->find('site:aliases'));
$app = new Drall();
/** @var \Drall\Command\SiteAliasesCommand $command */
$command = $app->find('site:aliases');
$command->setSiteDetector($siteDetectorMock);
$tester = new CommandTester($command);
$tester->execute(['--drall-group' => 'bluish']);

$tester->assertCommandIsSuccessful();
Expand All @@ -78,10 +83,11 @@ public function testExecuteWithNoSiteAliases() {
->method('getSiteAliases')
->willReturn([]);

$output = new BufferedOutput();

$app = new Drall($siteDetectorMock);
$tester = new CommandTester($app->find('site:aliases'));
$app = new Drall();
/** @var \Drall\Command\SiteAliasesCommand $command */
$command = $app->find('site:aliases');
$command->setSiteDetector($siteDetectorMock);
$tester = new CommandTester($command);
$tester->execute([]);

$tester->assertCommandIsSuccessful();
Expand Down
19 changes: 14 additions & 5 deletions test/Unit/Command/SiteDirectoriesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public function testExecute() {
->method('getSiteDirNames')
->willReturn(['donnie', 'leo']);

$app = new Drall($siteDetectorMock);
$app = new Drall();
/** @var \Drall\Command\SiteDirectoriesCommand $command */
$command = $app->find('site:directories');
$command->setSiteDetector($siteDetectorMock);
$tester = new CommandTester($app->find('site:directories'));
$tester->execute([]);

Expand Down Expand Up @@ -57,8 +60,11 @@ public function testExecuteWithGroup() {
->with('bluish')
->willReturn(['default']);

$app = new Drall($siteDetectorMock);
$tester = new CommandTester($app->find('site:directories'));
$app = new Drall();
/** @var \Drall\Command\SiteDirectoriesCommand $command */
$command = $app->find('site:directories');
$command->setSiteDetector($siteDetectorMock);
$tester = new CommandTester($command);
$tester->execute(['--drall-group' => 'bluish']);

$tester->assertCommandIsSuccessful();
Expand All @@ -77,8 +83,11 @@ public function testExecuteWithNoSiteDirectories() {
->method('getSiteDirNames')
->willReturn([]);

$app = new Drall($siteDetectorMock);
$tester = new CommandTester($app->find('site:directories'));
$app = new Drall();
/** @var \Drall\Command\SiteDirectoriesCommand $command */
$command = $app->find('site:directories');
$command->setSiteDetector($siteDetectorMock);
$tester = new CommandTester($command);
$tester->execute([]);

$tester->assertCommandIsSuccessful();
Expand Down
29 changes: 21 additions & 8 deletions test/Unit/Command/SiteKeysCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ public function testExecute() {
->method('getSiteKeys')
->willReturn(['donatello.com', 'leonardo.com']);

$app = new Drall($siteDetectorMock);
$tester = new CommandTester($app->find('site:keys'));
$app = new Drall();
/** @var \Drall\Command\SiteKeysCommand $command */
$command = $app->find('site:keys');
$command->setSiteDetector($siteDetectorMock);

$tester = new CommandTester($command);
$tester->execute([]);

$tester->assertCommandIsSuccessful();
Expand All @@ -55,12 +59,17 @@ public function testExecuteWithGroup() {
$siteDetectorMock
->expects($this->once())
->method('getSiteKeys')
->with('bluish')
->with('none')
->willReturn(['tmnt.com']);

$app = new Drall($siteDetectorMock);
$tester = new CommandTester($app->find('site:keys'));
$tester->execute(['--drall-group' => 'bluish']);
$app = new Drall();

/** @var \Drall\Command\SiteKeysCommand $command */
$command = $app->find('site:keys');
$command->setSiteDetector($siteDetectorMock);

$tester = new CommandTester($command);
$tester->execute(['--drall-group' => 'none']);

$tester->assertCommandIsSuccessful();

Expand All @@ -86,8 +95,12 @@ public function testExecuteWithNoSiteDirectories() {
->method('getSiteKeys')
->willReturn([]);

$app = new Drall($siteDetectorMock);
$tester = new CommandTester($app->find('site:keys'));
$app = new Drall();
/** @var \Drall\Command\SiteKeysCommand $command */
$command = $app->find('site:keys');
$command->setSiteDetector($siteDetectorMock);

$tester = new CommandTester($command);
$tester->execute([]);

$tester->assertCommandIsSuccessful();
Expand Down

0 comments on commit bba6a4f

Please sign in to comment.