diff --git a/src/CliProgram/AutoDiscovery/AutoDiscoveryDirectory.php b/src/CliProgram/AutoDiscovery/AutoDiscoveryDirectory.php index 38647eb..74c88cc 100644 --- a/src/CliProgram/AutoDiscovery/AutoDiscoveryDirectory.php +++ b/src/CliProgram/AutoDiscovery/AutoDiscoveryDirectory.php @@ -127,7 +127,7 @@ class AutoDiscoveryDirectory implements IAutoDiscoverySpot { $classes[$fileInfo->getRealPath()] = $this->applyAutoPrefixOnCommand($class->newInstance()); } - catch (ReflectionException $exception) { + catch (ReflectionException) { continue; } } diff --git a/src/CliProgram/AutoDiscovery/AutoDiscoverySpotsList.php b/src/CliProgram/AutoDiscovery/AutoDiscoverySpotsList.php index 2556d25..fb40bc6 100644 --- a/src/CliProgram/AutoDiscovery/AutoDiscoverySpotsList.php +++ b/src/CliProgram/AutoDiscovery/AutoDiscoverySpotsList.php @@ -7,7 +7,7 @@ use Arrayy\Collection\AbstractCollection; /** * A list of command auto discovery spots * - * @extends AbstractCollection + * @extends AbstractCollection */ class AutoDiscoverySpotsList extends AbstractCollection { /** diff --git a/src/CliProgram/AutoDiscovery/TAutoDiscoveryApplication.php b/src/CliProgram/AutoDiscovery/TAutoDiscoveryApplication.php index 608088d..5d5bc27 100644 --- a/src/CliProgram/AutoDiscovery/TAutoDiscoveryApplication.php +++ b/src/CliProgram/AutoDiscovery/TAutoDiscoveryApplication.php @@ -2,6 +2,8 @@ namespace jrosset\CliProgram\AutoDiscovery; +use Symfony\Component\Console\Command\Command; + /** * An application with command auto discovery */ @@ -29,6 +31,7 @@ trait TAutoDiscoveryApplication { * @return $this */ public function addAutoDiscoveredCommands (): static { + /** @var IAutoDiscoverySpot $autoDiscoverySpot */ foreach ($this->getAutoDiscoverySpots() as $autoDiscoverySpot) { foreach ($autoDiscoverySpot->getCommands() as $command) { $this->add($command); @@ -36,4 +39,18 @@ trait TAutoDiscoveryApplication { } return $this; } + /** + * Normalize command's name and aliases with auto prefixes (if any) + * + * @param Command $command The commande + * + * @return Command The command + */ + public function normalizeCommandNameAndAliases (Command $command): Command { + /** @var IAutoDiscoverySpot $autoDiscoverySpot */ + foreach ($this->getAutoDiscoverySpots() as $autoDiscoverySpot) { + $command = $autoDiscoverySpot->applyAutoPrefixOnCommand($command); + } + return $command; + } } \ No newline at end of file diff --git a/src/CliProgram/AutoPrefix/TAutoPrefixManagement.php b/src/CliProgram/AutoPrefix/TAutoPrefixManagement.php index c749c43..4b73405 100644 --- a/src/CliProgram/AutoPrefix/TAutoPrefixManagement.php +++ b/src/CliProgram/AutoPrefix/TAutoPrefixManagement.php @@ -32,18 +32,23 @@ trait TAutoPrefixManagement { * * @return Command The command */ - protected function applyAutoPrefixOnCommand (Command $command): Command { + public function applyAutoPrefixOnCommand (Command $command): Command { + /** @var IAutoPrefixManager $autoPrefixManager */ foreach ($this->getAutoPrefixManagers() as $autoPrefixManager) { if (($namesPrefix = $autoPrefixManager->getCommandPrefix($command)) !== null) { if (mb_strlen($namesPrefix) > 0) { $namesPrefix .= ':'; } - $command->setName($namesPrefix . $command->getName()); + if (self::getNamePrefix($command->getName()) !== $namesPrefix) { + $command->setName($namesPrefix . $command->getName()); + } $aliases = $command->getAliases(); foreach ($aliases as &$alias) { - $alias = $namesPrefix . $alias; + if (self::getNamePrefix($alias) !== $namesPrefix) { + $alias = $namesPrefix . $alias; + } } $command->setAliases($aliases); @@ -52,4 +57,22 @@ trait TAutoPrefixManagement { } return $command; } + /** + * Get the prefix from a command name or alias + * + * @param string $name The command name or alias + * + * @return string The prefix (empty if none) + */ + private static function getNamePrefix (string $name): string { + $lastPos = mb_strrpos($name, ':'); + if ($lastPos === false) { + return ''; + } + $prefix = mb_substr($name, 0, $lastPos); + if (mb_strlen($prefix) > 0) { + $prefix .= ':'; + } + return $prefix; + } } \ No newline at end of file