2.x
Julien Rosset 2 years ago
parent 52deb6665d
commit cd3cf756e6

@ -11,7 +11,8 @@
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"jrosset\\": "src/" "jrosset\\": "src/",
"jrosset\\Tests\\": "tests"
}, },
"exclude-from-classmap": [ "tests/" ] "exclude-from-classmap": [ "tests/" ]
}, },

@ -88,7 +88,7 @@ class AutoDiscoverySpot implements IAutoDiscoverySpot {
private static function getClassesOfDirectory (string $directoryPath, bool $processSubDirectories): array { private static function getClassesOfDirectory (string $directoryPath, bool $processSubDirectories): array {
$classes = []; $classes = [];
$directoryIterator = new FilesystemIterator($directoryPath); $directoryIterator = new FilesystemIterator($directoryPath, FilesystemIterator::CURRENT_AS_SELF);
foreach ($directoryIterator as $fileInfo) { foreach ($directoryIterator as $fileInfo) {
if ($fileInfo->isDot() || mb_substr($fileInfo->getFilename(), 0, 1) === '.') { if ($fileInfo->isDot() || mb_substr($fileInfo->getFilename(), 0, 1) === '.') {
continue; continue;

@ -78,7 +78,7 @@ class AutoDiscoverySpotClass implements IAutoDiscoverySpotClass {
for ($currTokenSub = $currTokenGlobal + 1; $currTokenSub < $nbTokens; $currTokenSub++) { for ($currTokenSub = $currTokenGlobal + 1; $currTokenSub < $nbTokens; $currTokenSub++) {
$subToken = $tokens[$currTokenSub]; $subToken = $tokens[$currTokenSub];
if ($subToken->getText() === '{') { if ($subToken->getText() === '{') {
$class = $tokens[$currTokenGlobal + 2]; $class = $tokens[$currTokenGlobal + 2]->getText();
} }
} }
} }

@ -38,7 +38,7 @@ trait AutoPrefixApplication {
* *
* @return $this * @return $this
*/ */
public function addAutoPrefixNamespaces (IAutoPrefixManager $manager, IAutoPrefixManager ...$extraManagers): self { public function addAutoPrefixManagers (IAutoPrefixManager $manager, IAutoPrefixManager ...$extraManagers): self {
$this->autoPrefixManagers[] = $manager; $this->autoPrefixManagers[] = $manager;
foreach ($extraManagers as $extraNamespace) { foreach ($extraManagers as $extraNamespace) {
$this->autoPrefixManagers[] = $extraNamespace; $this->autoPrefixManagers[] = $extraNamespace;
@ -52,7 +52,8 @@ trait AutoPrefixApplication {
* @return $this * @return $this
*/ */
public function applyAutoPrefixes (): self { 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) { foreach ($this->getAutoPrefixManagers() as $autoPrefixManager) {
if (($namesPrefix = $autoPrefixManager->getCommandPrefix($command)) !== null) { if (($namesPrefix = $autoPrefixManager->getCommandPrefix($command)) !== null) {
if (mb_strlen($namesPrefix) > 0) { if (mb_strlen($namesPrefix) > 0) {

@ -8,7 +8,7 @@ use Symfony\Component\Console\Command\Command;
/** /**
* A manager of commands auto prefix base on class namespace * A manager of commands auto prefix base on class namespace
*/ */
class AutoPrefixManager implements IAutoPrefixManager { class AutoPrefixNamespaceManager implements IAutoPrefixManager {
/** /**
* @var string The base namespace * @var string The base namespace
*/ */
@ -45,6 +45,9 @@ class AutoPrefixManager implements IAutoPrefixManager {
* @return $this * @return $this
*/ */
public function setNamespace (string $namespace): self { public function setNamespace (string $namespace): self {
if (mb_substr($namespace, 0, 1) !== '\\') {
$namespace = '\\' . $namespace;
}
$this->namespace = $namespace; $this->namespace = $namespace;
return $this; return $this;
} }
@ -74,16 +77,21 @@ class AutoPrefixManager implements IAutoPrefixManager {
*/ */
public function getCommandPrefix (Command $command): ?string { public function getCommandPrefix (Command $command): ?string {
$commandClass = new ReflectionClass($command); $commandClass = new ReflectionClass($command);
$commandNamespace = $commandClass->getNamespaceName(); $commandNamespace = '\\' . $commandClass->getNamespaceName();
if ($commandNamespace !== $this->getNamespace() && mb_substr($commandNamespace, mb_strlen($this->getNamespace() . '\\')) !== $this->getNamespace() . '\\') { if ($commandNamespace !== $this->getNamespace() && mb_substr($commandNamespace, mb_strlen($this->getNamespace() . '\\')) !== $this->getNamespace() . '\\') {
return null; return null;
} }
return (mb_strlen($this->getInitialPrefix()) > 0 ? $this->getInitialPrefix() . ':' : '') . mb_strtolower( return implode(
str_replace( ':',
array_filter(
[ [
$this->getNamespace() . '\\', $this->getInitialPrefix(),
'\\', mb_strtolower(
preg_replace(
[
'/^' . preg_quote($this->getNamespace(), '/') . '(?:\\\\|$)/',
'/\\\\/',
], ],
[ [
'', '',
@ -91,6 +99,9 @@ class AutoPrefixManager implements IAutoPrefixManager {
], ],
$commandNamespace $commandNamespace
) )
),
]
)
); );
} }
} }
Loading…
Cancel
Save