diff --git a/src/CliProgram/AutoPrefixApplication.php b/src/CliProgram/AutoPrefixApplication.php new file mode 100644 index 0000000..2b804da --- /dev/null +++ b/src/CliProgram/AutoPrefixApplication.php @@ -0,0 +1,76 @@ +autoPrefixManagers; + } + /** + * Set The lists of manager for commands auto prefix + * + * @param IAutoPrefixManager[] $autoPrefixManagers The lists of manager for commands auto prefix + * + * @return $this + */ + public function setAutoPrefixManagers (array $autoPrefixManagers): self { + $this->autoPrefixManagers = $autoPrefixManagers; + return $this; + } + /** + * Add a manager for commands auto prefix + * + * @param IAutoPrefixManager $manager The manager + * @param IAutoPrefixManager ...$extraManagers Extra managers to add + * + * @return $this + */ + public function addAutoPrefixNamespaces (IAutoPrefixManager $manager, IAutoPrefixManager ...$extraManagers): self { + $this->autoPrefixManagers[] = $manager; + foreach ($extraManagers as $extraNamespace) { + $this->autoPrefixManagers[] = $extraNamespace; + } + return $this; + } + + /** + * Apply commands auto prefixes + * + * @return $this + */ + public function applyAutoPrefixes (): self { + foreach ($this->all() as $command) { + foreach ($this->getAutoPrefixManagers() as $autoPrefixManager) { + if (($namesPrefix = $autoPrefixManager->getCommandPrefix($command)) !== null) { + if (mb_strlen($namesPrefix) > 0) { + $namesPrefix .= ':'; + } + + $command->setName($namesPrefix . $command->getName()); + + $aliases = $command->getAliases(); + foreach ($aliases as &$alias) { + $alias = $namesPrefix . $alias; + } + $command->setAliases($aliases); + + break; + } + } + } + return $this; + } +} \ No newline at end of file diff --git a/src/CliProgram/AutoPrefixManager.php b/src/CliProgram/AutoPrefixManager.php new file mode 100644 index 0000000..7aba997 --- /dev/null +++ b/src/CliProgram/AutoPrefixManager.php @@ -0,0 +1,96 @@ +setNamespace($namespace); + $this->setInitialPrefix($initialPrefix); + } + + /** + * The base namespace + * + * @return string The base namespace + */ + public function getNamespace (): string { + return $this->namespace; + } + /** + * Set the base namespace + * + * @param string $namespace The base namespace + * + * @return $this + */ + public function setNamespace (string $namespace): self { + $this->namespace = $namespace; + return $this; + } + + /** + * The initial prefix, Null if none + * + * @return string|null The initial prefix, Null if none + */ + public function getInitialPrefix (): ?string { + return $this->initialPrefix; + } + /** + * Set the initial prefix, Null if none + * + * @param string|null $initialPrefix The initial prefix, Null if none + * + * @return $this + */ + public function setInitialPrefix (?string $initialPrefix): self { + $this->initialPrefix = $initialPrefix; + return $this; + } + + /** + * @inheritDoc + */ + public function getCommandPrefix (Command $command): ?string { + $commandClass = new ReflectionClass($command); + $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() . '\\', + '\\', + ], + [ + '', + ':', + ], + $commandNamespace + ) + ); + } +} \ No newline at end of file diff --git a/src/CliProgram/IAutoPrefixManager.php b/src/CliProgram/IAutoPrefixManager.php new file mode 100644 index 0000000..5a248f9 --- /dev/null +++ b/src/CliProgram/IAutoPrefixManager.php @@ -0,0 +1,19 @@ +addDiscoveredCommands(); + $this->addAutoDiscoveredCommands(); + $this->applyAutoPrefixes(); } } \ No newline at end of file