diff --git a/composer.json b/composer.json index 319e12a..c26be1f 100644 --- a/composer.json +++ b/composer.json @@ -5,13 +5,14 @@ "minimum-stability": "stable", "require": { - "php": "^7.4 || ^8.0", - "symfony/console": "^6.0", + "php": "^7.4 || ^8.0", + "symfony/console": "^6.0", "jrosset/betterphptoken": "^1.0" }, "autoload": { "psr-4": { - "jrosset\\": "src/" + "jrosset\\": "src/", + "jrosset\\Tests\\": "tests" }, "exclude-from-classmap": [ "tests/" ] }, diff --git a/src/CliProgram/AutoDiscoverySpot.php b/src/CliProgram/AutoDiscoverySpot.php index b17d156..9695c43 100644 --- a/src/CliProgram/AutoDiscoverySpot.php +++ b/src/CliProgram/AutoDiscoverySpot.php @@ -88,7 +88,7 @@ class AutoDiscoverySpot implements IAutoDiscoverySpot { private static function getClassesOfDirectory (string $directoryPath, bool $processSubDirectories): array { $classes = []; - $directoryIterator = new FilesystemIterator($directoryPath); + $directoryIterator = new FilesystemIterator($directoryPath, FilesystemIterator::CURRENT_AS_SELF); foreach ($directoryIterator as $fileInfo) { if ($fileInfo->isDot() || mb_substr($fileInfo->getFilename(), 0, 1) === '.') { continue; diff --git a/src/CliProgram/AutoDiscoverySpotClass.php b/src/CliProgram/AutoDiscoverySpotClass.php index a3df848..2b20b74 100644 --- a/src/CliProgram/AutoDiscoverySpotClass.php +++ b/src/CliProgram/AutoDiscoverySpotClass.php @@ -78,7 +78,7 @@ class AutoDiscoverySpotClass implements IAutoDiscoverySpotClass { for ($currTokenSub = $currTokenGlobal + 1; $currTokenSub < $nbTokens; $currTokenSub++) { $subToken = $tokens[$currTokenSub]; if ($subToken->getText() === '{') { - $class = $tokens[$currTokenGlobal + 2]; + $class = $tokens[$currTokenGlobal + 2]->getText(); } } } diff --git a/src/CliProgram/AutoPrefixApplication.php b/src/CliProgram/AutoPrefixApplication.php index 2b804da..a16b8c4 100644 --- a/src/CliProgram/AutoPrefixApplication.php +++ b/src/CliProgram/AutoPrefixApplication.php @@ -38,7 +38,7 @@ trait AutoPrefixApplication { * * @return $this */ - public function addAutoPrefixNamespaces (IAutoPrefixManager $manager, IAutoPrefixManager ...$extraManagers): self { + public function addAutoPrefixManagers (IAutoPrefixManager $manager, IAutoPrefixManager ...$extraManagers): self { $this->autoPrefixManagers[] = $manager; foreach ($extraManagers as $extraNamespace) { $this->autoPrefixManagers[] = $extraNamespace; @@ -52,7 +52,8 @@ trait AutoPrefixApplication { * @return $this */ public function applyAutoPrefixes (): self { - foreach ($this->all() as $command) { + $commands = array_unique($this->all(), SORT_REGULAR); // Remove commands duplicate caused by aliases + foreach ($commands as $command) { foreach ($this->getAutoPrefixManagers() as $autoPrefixManager) { if (($namesPrefix = $autoPrefixManager->getCommandPrefix($command)) !== null) { if (mb_strlen($namesPrefix) > 0) { diff --git a/src/CliProgram/AutoPrefixManager.php b/src/CliProgram/AutoPrefixNamespaceManager.php similarity index 70% rename from src/CliProgram/AutoPrefixManager.php rename to src/CliProgram/AutoPrefixNamespaceManager.php index 7aba997..b19044e 100644 --- a/src/CliProgram/AutoPrefixManager.php +++ b/src/CliProgram/AutoPrefixNamespaceManager.php @@ -8,7 +8,7 @@ use Symfony\Component\Console\Command\Command; /** * A manager of commands auto prefix base on class namespace */ -class AutoPrefixManager implements IAutoPrefixManager { +class AutoPrefixNamespaceManager implements IAutoPrefixManager { /** * @var string The base namespace */ @@ -45,6 +45,9 @@ class AutoPrefixManager implements IAutoPrefixManager { * @return $this */ public function setNamespace (string $namespace): self { + if (mb_substr($namespace, 0, 1) !== '\\') { + $namespace = '\\' . $namespace; + } $this->namespace = $namespace; return $this; } @@ -74,22 +77,30 @@ class AutoPrefixManager implements IAutoPrefixManager { */ public function getCommandPrefix (Command $command): ?string { $commandClass = new ReflectionClass($command); - $commandNamespace = $commandClass->getNamespaceName(); + $commandNamespace = '\\' . $commandClass->getNamespaceName(); if ($commandNamespace !== $this->getNamespace() && mb_substr($commandNamespace, mb_strlen($this->getNamespace() . '\\')) !== $this->getNamespace() . '\\') { return null; } - return (mb_strlen($this->getInitialPrefix()) > 0 ? $this->getInitialPrefix() . ':' : '') . mb_strtolower( - str_replace( - [ - $this->getNamespace() . '\\', - '\\', - ], + return implode( + ':', + array_filter( [ - '', - ':', - ], - $commandNamespace + $this->getInitialPrefix(), + mb_strtolower( + preg_replace( + [ + '/^' . preg_quote($this->getNamespace(), '/') . '(?:\\\\|$)/', + '/\\\\/', + ], + [ + '', + ':', + ], + $commandNamespace + ) + ), + ] ) ); }