Files
FendxPHP/fendx-framework/fendx-cli/src/Input/InputDefinition.php
Lawson 2782d765fb feat(database): 添加用户角色权限系统及相关监控功能
- 创建用户表(users)包含基本信息和认证字段
- 创建角色表(roles)用于权限控制
- 创建权限表(permissions)定义系统权限
- 创建用户角色关联表(user_roles)建立用户与角色关系
- 创建角色权限关联表(role_permissions)建立角色与权限关系
- 创建迁移记录表(migrations)追踪数据库变更
- 添加AdminController提供管理员面板功能
- 实现系统监控、配置管理、缓存清理等功能
- 添加AOP切面编程支持的各种通知类型
- 实现告警管理AlertManager支持多渠道告警
- 添加文档注解接口规范
2026-04-08 17:00:28 +08:00

180 lines
4.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Fendx\CLI\Input;
use Fendx\CLI\Exception\InvalidArgumentException;
class InputDefinition
{
private array $arguments = [];
private array $options = [];
private array $shortcuts = [];
public function __construct(array $definitions = [])
{
foreach ($definitions as $definition) {
if ($definition instanceof InputArgument) {
$this->addArgument($definition);
} elseif ($definition instanceof InputOption) {
$this->addOption($definition);
} else {
throw new InvalidArgumentException('Input definition must be an instance of InputArgument or InputOption.');
}
}
}
public function addArgument(InputArgument $argument): void
{
if (isset($this->arguments[$argument->getName()])) {
throw new InvalidArgumentException(sprintf('An argument with name "%s" already exists.', $argument->getName()));
}
$this->arguments[$argument->getName()] = $argument;
}
public function addOption(InputOption $option): void
{
if (isset($this->options[$option->getName()])) {
throw new InvalidArgumentException(sprintf('An option with name "%s" already exists.', $option->getName()));
}
if ($option->getShortcut()) {
foreach (explode('|', $option->getShortcut()) as $shortcut) {
if (isset($this->shortcuts[$shortcut])) {
throw new InvalidArgumentException(sprintf('An option with shortcut "%s" already exists.', $shortcut));
}
$this->shortcuts[$shortcut] = $option->getName();
}
}
$this->options[$option->getName()] = $option;
}
public function getArguments(): array
{
return $this->arguments;
}
public function getArgument(string $name): InputArgument
{
if (!$this->hasArgument($name)) {
throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
}
return $this->arguments[$name];
}
public function hasArgument(string $name): bool
{
return isset($this->arguments[$name]);
}
public function getArgumentCount(): int
{
return count($this->arguments);
}
public function getArgumentRequiredCount(): int
{
$count = 0;
foreach ($this->arguments as $argument) {
if ($argument->isRequired()) {
$count++;
}
}
return $count;
}
public function getOptions(): array
{
return $this->options;
}
public function getOption(string $name): InputOption
{
if (!$this->hasOption($name)) {
throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
}
return $this->options[$name];
}
public function hasOption(string $name): bool
{
return isset($this->options[$name]);
}
public function hasShortcut(string $shortcut): bool
{
return isset($this->shortcuts[$shortcut]);
}
public function getOptionForShortcut(string $shortcut): InputOption
{
if (!$this->hasShortcut($shortcut)) {
throw new InvalidArgumentException(sprintf('The "-%s" shortcut does not exist.', $shortcut));
}
return $this->getOption($this->shortcuts[$shortcut]);
}
public function getSynopsis(): string
{
$elements = [];
foreach ($this->getOptions() as $option) {
if ($option->isRequired()) {
$elements[] = sprintf('--%s', $option->getName());
}
}
foreach ($this->getArguments() as $argument) {
$elements[] = $argument->getName();
}
return implode(' ', $elements);
}
public function setDefinition(array $definitions): void
{
$this->arguments = [];
$this->options = [];
$this->shortcuts = [];
foreach ($definitions as $definition) {
if ($definition instanceof InputArgument) {
$this->addArgument($definition);
} elseif ($definition instanceof InputOption) {
$this->addOption($definition);
}
}
}
public function merge(self $definition): void
{
foreach ($definition->getArguments() as $argument) {
$this->addArgument($argument);
}
foreach ($definition->getOptions() as $option) {
$this->addOption($option);
}
}
public function __toString(): string
{
$synopsis = '';
foreach ($this->getOptions() as $option) {
$synopsis .= ' ' . $option->getSynopsis();
}
foreach ($this->getArguments() as $argument) {
$synopsis .= ' ' . $argument->getSynopsis();
}
return trim($synopsis);
}
}