企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] * * * * * ## 1 文件 tp在console目录下实现了控制台的 输入,输出,命令实现,控制台助手等功能 ~~~ \bin\ 输入助手 \command\ 命令实现 \helpler\ 控制台助手 \input\ 控制台输入 \output\ 控制台输出 Input.php 输入类 Output.php 输出类型 ~~~ ## 2 控制台输入 ### 1 (输入接口)Input.php 1 成员变量 ~~~ protected $definition; protected $options = []; protected $arguments = []; protected $interactive = true; private $tokens; private $parsed; ~~~ 2 输入对象构造函数 `public function __construct($argv = null)` > $argv: 输入参数;默认为$_SERVER['argv'] 3 初始化解析字符串 `protected function setTokens(array $tokens)` > $token:待解析字符串 4 绑定实例 `public function bind(Definition $definition)` > $definition:实例对象 5 参数解析 `protected function parse()` 6 短指令操作 - `private function parseShortOption($token)` > $token:待解析字符串 7 短指令获取 `private function parseShortOption($token)` > $name:短指令名称 8 长指令操作 -- `private function parseLongOption($token)` > $token:待解析字符串 9 参数解析 '' `private function parseArgument($token)` > $token:待解析字符串 10 注册短指令选项的值 ~~~ private function addShortOption($shortcut, $value) ~~~ > $shortcut:短指令名称 $value:短指令值 11 注册长指令选项的值 `private function addLongOption($name, $value)` > $name: 长指令名称 > $value:长指令值 12 获取第一个参数 `public function getFirstArgument()` 13 参数检查 `public function hasParameterOption($values)` > $value:需要检查的值 14 获取原始选项的值 ~~~ public function getParameterOption($values, $default = false) ~~~ > $values: 检查值 $default: 默认值 15 输入验证 `public function validate()` 16 操作的交互控制 ~~~ public function isInteractive() public function setInteractive($interactive) ~~~ 17 参数的操作 ~~~ public function getArguments() public function getArgument($name) public function setArgument($name, $value) public function hasArgument($name) ~~~ 18 选项的操作 ~~~ public function getOption($name) public function setOption($name, $value) public function hasOption($name) ~~~ 19 指令操作 ~~~ public function escapeToken($token) public function __toString() ~~~ ### 2 (输入指令)input\Definition.php 1 成员变量 ~~~ private $arguments; private $requiredCount; private $hasAnArrayArgument = false; private $hasOptional; private $options; private $shortcuts; ~~~ 2 构造函数 ~~~ public function __construct(array $definition = []) ~~~ 3 设置选项的指令 ~~~ public function setDefinition(array $definition) ~~~ 4 设置指令的参数 ~~~ public function setArguments($arguments = []) ~~~ 5 添加指令参数 ~~~ public function addArguments($arguments = []) public function addArgument(Argument $argument) ~~~ 6 参数操作 ~~~ public function getArgument($name) public function getArguments() public function hasArgument($name) public function getArgumentCount() public function getArgumentRequiredCount() public function getArgumentDefaults() ~~~ 7 选项操作 ~~~ public function setOptions($options = []) public function addOptions($options = []) public function addOption(Option $option) public function getOption($name) public function hasOption($name) public function getOptions() public function getOptionDefaults() ~~~ 8 短指令选项操作 ~~~ public function hasShortcut($name) public function getOptionForShortcut($shortcut) private function shortcutToName($shortcut) ~~~ 9 获取指令介绍 ~~~ public function getSynopsis($short = false) ~~~ ### 3 (输入参数)input\Argument.php 1 成员变量 ~~~ const REQUIRED = 1; const OPTIONAL = 2; const IS_ARRAY = 4; private $name; private $mode; private $default; private $description; ~~~ 2 构造函数 `public function __construct($name, $mode = null, $description = '', $default = null)` 3 获取参数名 `public function getName()` 4 是否必须 `public function isRequired()` 5 是否接受数组参数 `public function isArray()` 6 设置默认值 ~~~ public function setDefault($default = null) ~~~ 7 获取默认值 ~~~ public function getDefault() ~~~ 8 获取描述 ~~~ public function getDescription() ~~~ ### 4 (输入选项)input\Option.php 1 成员变量 ~~~ const VALUE_NONE = 1; const VALUE_REQUIRED = 2; const VALUE_OPTIONAL = 4; const VALUE_IS_ARRAY = 8; private $name; private $shortcut; private $mode; private $default; private $description; ~~~ 2 构造函数 `public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)` 3 获取短指令 `public function getShortcut()` 4 获取选项名 `public function getName()` 5 选项状态 ~~~ public function acceptValue() public function isValueRequired() public function isValueOptional() public function isArray() ~~~ 6 设置选项默认值 ~~~ public function setDefault($default = null) public function getDefault() ~~~ 7 获取选项描述 ~~~ public function getDescription() ~~~ 8 选项比较 ~~~ public function equals(Option $option) ~~~ ## 3 控制台输出 ### 1 (输出接口)Out.php 1 成员变量 ~~~ private $stderr; ~~~ 2 构造函数 `public function __construct()` 3 设置是否美化 public function setDecorated($decorated) 4 设置输出格式 public function setFormatter(Formatter $formatter) 5 设置输出状态 public function setVerbosity($level) 6 错误输出设置 ~~~ public function getErrorOutput() public function setErrorOutput(Output $error) ~~~ 7 控制台输出检查 `protected function hasStdoutSupport()` ### 2 (输出流)\output\Stream.php 1 成员变量 ~~~ const VERBOSITY_QUIET = 0; const VERBOSITY_NORMAL = 1; const VERBOSITY_VERBOSE = 2; const VERBOSITY_VERY_VERBOSE = 3; const VERBOSITY_DEBUG = 4; const OUTPUT_NORMAL = 0; const OUTPUT_RAW = 1; const OUTPUT_PLAIN = 2; private $verbosity = self::VERBOSITY_NORMAL; private $formatter; private $stream; ~~~ 2 构造函数 `public function __construct($stream, Formatter $formatter = null)` > $stream:输出流 > $formatter:输出格式 3 输出格式操作 ~~~ public function setFormatter(Formatter $formatter) public function getFormatter() ~~~ 4 输出格式美化操作 ~~~ public function setDecorated($decorated) public function isDecorated() ~~~ 5 输出状态操作 ~~~ public function setVerbosity($level) public function getVerbosity() public function isQuiet() public function isVerbose() public function isVeryVerbose() public function isDebug() ~~~ 6 行输出 ~~~ public function writeln($messages, $type = self::OUTPUT_NORMAL) ~~~ 7 输出接口 `public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)` 8 输出操作 `protected function doWrite($message, $newline)` 9 获取输出流 `public function getStream()` 10 是否支持着色 `protected function hasColorSupport()` ### 3 (输出格式) \output\Formatter.php 1 成员变量 ~~~ private $decorated = false; private $styles = []; private $styleStack; ~~~ 2 输出内容转义 `public static function escape($text)` 3 输出格式初始化 `public function __construct()` 4 输出美化控制 ~~~ public function setDecorated($decorated) public function isDecorated() ~~~ 5 输出样式操作 ~~~ public function setStyle($name, Style $style) public function hasStyle($name) public function getStyle($name) public function getStyleStack() ~~~ 6 格式化输出内容 `public function format($message)` 7 创建输出样式 ~~~ private function createStyleFromString($string) ~~~ 8 应用当前输出样式 ~~~ private function applyCurrentStyle($text) ~~~ ### 4 (输出样式栈)\output\formatter\Stack.php 1 成员变量 ~~~ private $styles; private $emptyStyle; ~~~ 2 构造函数 ~~~ public function __construct(Style $emptyStyle = null) ~~~ 3 清空输出样式栈 `public function reset()` 4 添加输出样式到栈 `public function push(Style $style)` 5 弹出栈一个样式 `public function pop(Style $style = null)` 6 获取堆栈的顶层样式 `public function getCurrent()` 6 空样式操作 ~~~ public function setEmptyStyle(Style $emptyStyle) public function getEmptyStyle() ~~~ ### 5 (输出样式)\output\formatter\Style.php 1 成员变量 ~~~ private static $availableForegroundColors private static $availableBackgroundColors private static $availableOptions private $foreground; private $background; private $options = []; ~~~ 2 构造函数 `public function __construct($foreground = null, $background = null, array $options = [])` 3 设置字体颜色 `public function setForeground($color = null)` 4 设置背景色 `public function setBackground($color = null)` 5 设置字体格式 ~~~ public function setOption($option) ~~~ 6 重置字体格式 ~~~ public function unsetOption($option) ~~~ 7 批量设置字体格式 `public function setOptions(array $options)` 8 应用样式到文字 `public function apply($text)` ## 4 控制台命令 ### 1 (命令行父类)\command\Command.php 1 成员变量 ~~~ private $console; private $name; private $aliases = []; private $definition; private $help; private $description; private $ignoreValidationErrors = false; private $consoleDefinitionMerged = false; private $consoleDefinitionMergedWithArgs = false; private $code; private $synopsis = []; private $usages = []; private $helperSet; ~~~ 2 构造函数 ~~~ public function __construct($name = null) ~~~ 3 忽略验证错误 `public function ignoreValidationErrors()` 4 控制台操作 ~~~ public function setConsole(Console $console = null) public function getConsole() ~~~ 5 帮助集操作 ~~~ public function setHelperSet(HelperSet $helperSet) public function getHelperSet() public function getHelper($name) ~~~ 6 指令是否有效 `public function isEnabled()` 7 指令配置 `protected function configure()` 8 指令的执行实现 ~~~ protected function execute(Input $input, Output $output) ~~~ 9 用户验证 `protected function interact(Input $input, Output $output)` 10 命令初始化回调 `protected function initialize(Input $input, Output $output)` 11 命令的执行接口 `public function run(Input $input, Output $output)` 12 执行代码设置 `public function setCode(callable $code)` 13 参数定义操作 ~~~ public function mergeConsoleDefinition($mergeArgs = true) public function setDefinition($definition) public function getDefinition() public function getNativeDefinition() ~~~ 14 添加参数 `public function addArgument($name, $mode = null, $description = '', $default = null)` 15 添加选项 `public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null)` 16 指令名称 ~~~ public function setName($name) public function getName() ~~~ 17 指令描述 ~~~ public function setDescription($description) public function getDescription() ~~~ 18 帮助信息 ~~~ public function setHelp($help) public function getHelp() ~~~ 19 描述信息 `public function getProcessedHelp()` 20 别名操作 ~~~ public function setAliases($aliases) public function getAliases() ~~~ 21 命令简介 `public function getSynopsis($short = false)` 22 命令使用方法 ~~~ public function addUsage($usage) public function getUsages() ~~~ 23 命令名称验证 ~~~ private function validateName($name) ~~~ ### 2 (自动构建应用命令)\command\Build.php 1 命令配置 `protected function configure()` 2 命令执行 ~~~ protected function execute(Input $input, Output $output) ~~~ ### 3 (列出命令)\command\Lists.php 1 命令配置 `protected function configure()` 2 命令定义 `public function getNativeDefinition()` 3 命令执行 `protected function execute(Input $input, Output $output)` 4 创建命令 `private function createDefinition()` ### 4 (帮助命令)\command\Help.php 1 命令配置 `protected function configure()` 2 命令添加 `public function setCommand(Command $command)` 3 命令执行 ~~~ protected function execute(Input $input, Output $output) ~~~ ### 5 (控制器创建命令)\command\make\Controller.php 1 构造函数 `public function __construct()` ### 6 (数据模型创建命令)\command\make\Model.php 1 构造函数 `public function __construct()` ## 5 控制台助手