Add CliCommand attribute

master 3.7.0
Julien Rosset 9 months ago
parent 26815fcac3
commit c40a14aba0

@ -14,19 +14,70 @@ use Throwable;
*/
class BaseCommand extends Command {
/**
* Initialize the command
*
* @param string|null $name The command name, Null = class name
* @param string ...$aliases The command aliases
* @return string|null The command default name
*/
public function __construct (?string $name = null, string ...$aliases) {
if ($name === null) {
$classShortName = (new ReflectionClass($this))->getShortName();
$name = mb_strtolower(mb_substr($classShortName, 0, 1)) . mb_substr($classShortName, 1);
public static function getDefaultName (): ?string {
$reflectionClass = new ReflectionClass(static::class);
if ($attribute = $reflectionClass->getAttributes(CliCommand::class)) {
/** @var CliCommand $cliCommandAttribute */
$cliCommandAttribute = $attribute[0]->newInstance();
return $cliCommandAttribute->name;
}
if (($name = parent::getDefaultName()) !== null) {
return $name;
}
$classShortName = $reflectionClass->getShortName();
return mb_strtolower(mb_substr($classShortName, 0, 1)) . mb_substr($classShortName, 1);
}
/**
* @return string[] The command default aliases
*/
public static function getDefaultAliases (): array {
$reflectionClass = new ReflectionClass(static::class);
if ($attribute = $reflectionClass->getAttributes(CliCommand::class)) {
/** @var CliCommand $cliCommandAttribute */
$cliCommandAttribute = $attribute[0]->newInstance();
return $cliCommandAttribute->aliases;
}
return [];
}
/**
* @return string|null The command default description
*/
public static function getDefaultDescription (): ?string {
if ($attribute = (new ReflectionClass(static::class))->getAttributes(CliCommand::class)) {
/** @var CliCommand $cliCommandAttribute */
$cliCommandAttribute = $attribute[0]->newInstance();
return $cliCommandAttribute->description;
}
return parent::getDefaultDescription();
}
/**
* @return bool Is the command hidden from command list by default ?
*/
public static function getDefaultHidden (): bool {
if ($attribute = (new ReflectionClass(static::class))->getAttributes(CliCommand::class)) {
/** @var CliCommand $cliCommandAttribute */
$cliCommandAttribute = $attribute[0]->newInstance();
return $cliCommandAttribute->hidden;
}
return false;
}
/**
* Initialize the command
*
* @param string|null $name The command name ; Null if the default one (cf. {@see static::getDefaultName()})
* @param string[]|null $aliases The command aliases ; Null if the default one (cf. {@see static::getDefaultAliases()})
* @param string|null $description The command description ; Null if the default one (cf. {@see static::getDefaultDescription()})
* @param bool|null $hidden Is the command hidden from command list ? Null if the default one (cf. {@see static::getDefaultHidden()})
*/
public function __construct (?string $name = null, ?array $aliases = null, ?string $description = null, ?bool $hidden = null) {
parent::__construct($name);
$this->setAliases($aliases);
$this->setAliases($aliases ?? static::getDefaultAliases());
$this->setDescription($description ?? $this->getDescription());
$this->setHidden($hidden ?? static::getDefaultHidden());
}
/**

@ -0,0 +1,47 @@
<?php
namespace jrosset\CliProgram;
use Attribute;
use jrosset\CliProgram\AutoDiscovery\IAutoDiscoverySpot;
use Symfony\Component\Console\Attribute\AsCommand;
/**
* Attribute to tag command
*
* Identical to {@see AsCommand} but allow empty command name (i.e., for {@see IAutoDiscoverySpot})
*/
#[Attribute(Attribute::TARGET_CLASS)]
class CliCommand {
/**
* @var string|null The commande name
*/
public readonly ?string $name;
/**
* @var string|null The command description
*/
public readonly ?string $description;
/**
* @var string[] The command aliases
*/
public readonly array $aliases;
/**
* @var bool Is the command hidden from command list ?
*/
public readonly bool $hidden;
/**
* Initialization
*
* @param string|null $name The commande name
* @param string|null $description The command description
* @param string[] $aliases The command aliases
* @param bool $hidden Is the command hidden from command list ?
*/
public function __construct (?string $name, ?string $description, array $aliases, bool $hidden) {
$this->name = $name;
$this->description = $description;
$this->aliases = $aliases;
$this->hidden = $hidden;
}
}
Loading…
Cancel
Save