Add method for normalizing command name in TAutoDiscoveryApplication

master 3.5.0
Julien Rosset 11 months ago
parent 9d43547250
commit 7d21bb492c

@ -127,7 +127,7 @@ class AutoDiscoveryDirectory implements IAutoDiscoverySpot {
$classes[$fileInfo->getRealPath()] = $this->applyAutoPrefixOnCommand($class->newInstance()); $classes[$fileInfo->getRealPath()] = $this->applyAutoPrefixOnCommand($class->newInstance());
} }
catch (ReflectionException $exception) { catch (ReflectionException) {
continue; continue;
} }
} }

@ -7,7 +7,7 @@ use Arrayy\Collection\AbstractCollection;
/** /**
* A list of command auto discovery spots * A list of command auto discovery spots
* *
* @extends AbstractCollection<AutoDiscoverySpotsList> * @extends AbstractCollection<IAutoDiscoverySpot>
*/ */
class AutoDiscoverySpotsList extends AbstractCollection { class AutoDiscoverySpotsList extends AbstractCollection {
/** /**

@ -2,6 +2,8 @@
namespace jrosset\CliProgram\AutoDiscovery; namespace jrosset\CliProgram\AutoDiscovery;
use Symfony\Component\Console\Command\Command;
/** /**
* An application with command auto discovery * An application with command auto discovery
*/ */
@ -29,6 +31,7 @@ trait TAutoDiscoveryApplication {
* @return $this * @return $this
*/ */
public function addAutoDiscoveredCommands (): static { public function addAutoDiscoveredCommands (): static {
/** @var IAutoDiscoverySpot $autoDiscoverySpot */
foreach ($this->getAutoDiscoverySpots() as $autoDiscoverySpot) { foreach ($this->getAutoDiscoverySpots() as $autoDiscoverySpot) {
foreach ($autoDiscoverySpot->getCommands() as $command) { foreach ($autoDiscoverySpot->getCommands() as $command) {
$this->add($command); $this->add($command);
@ -36,4 +39,18 @@ trait TAutoDiscoveryApplication {
} }
return $this; 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;
}
} }

@ -32,19 +32,24 @@ trait TAutoPrefixManagement {
* *
* @return Command The command * @return Command The command
*/ */
protected function applyAutoPrefixOnCommand (Command $command): Command { public function applyAutoPrefixOnCommand (Command $command): Command {
/** @var IAutoPrefixManager $autoPrefixManager */
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) {
$namesPrefix .= ':'; $namesPrefix .= ':';
} }
if (self::getNamePrefix($command->getName()) !== $namesPrefix) {
$command->setName($namesPrefix . $command->getName()); $command->setName($namesPrefix . $command->getName());
}
$aliases = $command->getAliases(); $aliases = $command->getAliases();
foreach ($aliases as &$alias) { foreach ($aliases as &$alias) {
if (self::getNamePrefix($alias) !== $namesPrefix) {
$alias = $namesPrefix . $alias; $alias = $namesPrefix . $alias;
} }
}
$command->setAliases($aliases); $command->setAliases($aliases);
break; break;
@ -52,4 +57,22 @@ trait TAutoPrefixManagement {
} }
return $command; 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;
}
} }
Loading…
Cancel
Save