Skip to content

Commit

Permalink
Prevent duplicate classes in discovery.
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiu-cristea committed Mar 21, 2021
1 parent 9467245 commit e84b128
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
use Composer\Autoload\ClassLoader;
use Consolidation\AnnotatedCommand\AnnotatedCommand;
use Consolidation\AnnotatedCommand\CommandFileDiscovery;
use Consolidation\Filter\Hooks\FilterHooks;
use Consolidation\SiteAlias\SiteAliasManager;
use Drush\Boot\BootstrapManager;
use Drush\Command\RemoteCommandProxy;
use Drush\Commands\DrushCommands;
use Drush\Config\ConfigAwareTrait;
use Drush\Log\LogLevel;
use Drush\Runtime\RedispatchHook;
Expand Down Expand Up @@ -317,16 +319,12 @@ public function configureAndRegisterCommands(InputInterface $input, OutputInterf
// any of the configuration steps we do here.
$this->configureIO($input, $output);

$discovery = $this->commandDiscovery();
$commandClasses = $discovery->discover($commandfileSearchpath, '\Drush');
$commandClasses[] = \Consolidation\Filter\Hooks\FilterHooks::class;
$commandClasses = array_merge(
$this->commandsFromConfiguration(),
$commandClasses = array_unique(array_merge(
$this->discoverCommandsFromConfiguration(),
$this->discoverCommands($commandfileSearchpath, '\Drush'),
$this->discoverPsr4Commands($classLoader),
$commandClasses
);

$this->loadCommandClasses($commandClasses);
[FilterHooks::class]
));

// Uncomment the lines below to use Console's built in help and list commands.
// unset($commandClasses[__DIR__ . '/Commands/help/HelpCommands.php']);
Expand All @@ -339,21 +337,20 @@ public function configureAndRegisterCommands(InputInterface $input, OutputInterf
$runner->registerCommandClasses($this, $commandClasses);
}

protected function commandsFromConfiguration()
protected function discoverCommandsFromConfiguration()
{
$commandList = [];

foreach ($this->config->get('drush.commands', []) as $key => $value) {
$classname = $key;
$path = $value;
if (is_numeric($key)) {
$classname = $value;
$commandList[] = $classname;
} else {
$commandList[$path] = $classname;
$classname = ltrim($key, '\\');
$commandList[$value] = $classname;
}
}
return $commandList;
$this->loadCommandClasses($commandList);
return array_keys($commandList);
}

/**
Expand All @@ -370,9 +367,9 @@ protected function loadCommandClasses($commandClasses)
}

/**
* Create a command file discovery object
* Discovers command classes.
*/
protected function commandDiscovery()
protected function discoverCommands(array $directoryList, string $baseNamespace): array
{
$discovery = new CommandFileDiscovery();
$discovery
Expand All @@ -383,18 +380,29 @@ protected function commandDiscovery()
->ignoreNamespacePart('src')
->setSearchLocations(['Commands', 'Hooks', 'Generators'])
->setSearchPattern('#.*(Command|Hook|Generator)s?.php$#');
return $discovery;
$baseNamespace = ltrim($baseNamespace, '\\');
$commandClasses = $discovery->discover($directoryList, $baseNamespace);
$this->loadCommandClasses($commandClasses);
return array_values($commandClasses);
}

/**
* Discovers commands that are PSR4 auto-loaded.
*/
protected function discoverPsr4Commands(ClassLoader $classLoader): array
{
return (new RelativeNamespaceDiscovery($classLoader))
$classes = (new RelativeNamespaceDiscovery($classLoader))
->setRelativeNamespace('Drush\Commands')
->setSearchPattern('/.*DrushCommands\.php$/')
->getClasses();

return array_filter($classes, function (string $class): bool {
$reflectionClass = new \ReflectionClass($class);
return $reflectionClass->isSubclassOf(DrushCommands::class)
&& !$reflectionClass->isAbstract()
&& !$reflectionClass->isInterface()
&& !$reflectionClass->isTrait();
});
}

/**
Expand Down

0 comments on commit e84b128

Please sign in to comment.