You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.7 KiB
PHP
57 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace jrosset\CliProgram\AutoPrefix;
|
|
|
|
use jrosset\Collections\Collection;
|
|
use jrosset\Collections\ICollection;
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
/**
|
|
* Implements a commands with auto prefix management
|
|
*/
|
|
trait TAutoPrefixManagement {
|
|
/**
|
|
* @var ICollection<IAutoPrefixManager> The lists of manager for commands auto prefix
|
|
*/
|
|
private ICollection $autoPrefixManagers;
|
|
|
|
/**
|
|
* The lists of manager for commands auto prefix
|
|
*
|
|
* @return ICollection<IAutoPrefixManager> The lists of manager for commands auto prefix
|
|
*/
|
|
public function getAutoPrefixManagers (): ICollection {
|
|
if (!isset($this->autoPrefixManagers)) {
|
|
$this->autoPrefixManagers = new Collection();
|
|
}
|
|
return $this->autoPrefixManagers;
|
|
}
|
|
|
|
/**
|
|
* Apply the auto prefix managers on a command (until one of them manage it)
|
|
*
|
|
* @param Command $command The command
|
|
*
|
|
* @return Command The command
|
|
*/
|
|
protected function applyAutoPrefixOnCommand (Command $command): 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 $command;
|
|
}
|
|
} |