From 0a9a99f13cf897a9775bf884d2b133eb642980f7 Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Tue, 3 Sep 2024 12:02:06 +0200 Subject: [PATCH] Add method for normalizing command name in TAutoDiscoveryApplication --- .../AutoDiscovery/AutoDiscoverySpotsList.php | 2 +- .../TAutoDiscoveryApplication.php | 17 +++++++++++ .../AutoPrefix/TAutoPrefixManagement.php | 29 +++++++++++++++++-- 3 files changed, 44 insertions(+), 4 deletions(-) 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 c50601a..0b4ccf1 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 (): self { + /** @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