New trait for classes with commands auto prefix

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

@ -0,0 +1,76 @@
<?php
namespace jrosset\CliProgram;
/**
* An application managing commands with auto prefix
*/
trait AutoPrefixApplication {
/**
* @var IAutoPrefixManager[] The lists of manager for commands auto prefix
*/
private array $autoPrefixManagers;
/**
* The lists of manager for commands auto prefix
*
* @return IAutoPrefixManager[] The lists of manager for commands auto prefix
*/
public function getAutoPrefixManagers (): array {
return $this->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;
}
}

@ -0,0 +1,96 @@
<?php
namespace jrosset\CliProgram;
use ReflectionClass;
use Symfony\Component\Console\Command\Command;
/**
* A manager of commands auto prefix base on class namespace
*/
class AutoPrefixManager implements IAutoPrefixManager {
/**
* @var string The base namespace
*/
private string $namespace;
/**
* @var string|null The initial prefix, Null if none
*/
private ?string $initialPrefix;
/**
* Initialization
*
* @param string $namespace The base namespace
* @param string|null $initialPrefix The initial prefix, Null if none
*/
public function __construct (string $namespace, ?string $initialPrefix = null) {
$this->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
)
);
}
}

@ -0,0 +1,19 @@
<?php
namespace jrosset\CliProgram;
use Symfony\Component\Console\Command\Command;
/**
* Interface for a manager of commands auto prefix
*/
interface IAutoPrefixManager {
/**
* The prefix for a command
*
* @param Command $command The command
*
* @return string|null The prefix for the command, Null if not applicable
*/
public function getCommandPrefix (Command $command): ?string;
}

@ -1,14 +1,17 @@
<?php <?php
use jrosset\CliProgram\AutoDiscoveryApplication; use jrosset\CliProgram\AutoDiscoveryApplication;
use jrosset\CliProgram\AutoPrefixApplication;
use jrosset\CliProgram\SafeApplication; use jrosset\CliProgram\SafeApplication;
class Application extends \Symfony\Component\Console\Application { class Application extends \Symfony\Component\Console\Application {
use SafeApplication; use SafeApplication;
use AutoDiscoveryApplication; use AutoDiscoveryApplication;
use AutoPrefixApplication;
public function __construct (string $name = 'UNKNOWN', string $version = 'UNKNOWN') { public function __construct (string $name = 'UNKNOWN', string $version = 'UNKNOWN') {
parent::__construct($name, $version); parent::__construct($name, $version);
$this->addDiscoveredCommands(); $this->addAutoDiscoveredCommands();
$this->applyAutoPrefixes();
} }
} }
Loading…
Cancel
Save