您的位置:首页 > 编程语言 > PHP开发

[李景山php]每天TP5-20161225|thinkphp5-Console.php-2

2016-11-17 08:38 447 查看
/**
* 执行指令
* @param Input  $input
* @param Output $output
* @return int
*/
public function doRun(Input $input, Output $output)
{// 真正 运行 函数开始
if (true === $input->hasParameterOption(['--version', '-V'])) {
$output->writeln($this->getLongVersion());// 写出版本信息

return 0;// 返回 0
}// 如果有 版本

$name = $this->getCommandName($input);// 获取命令 名字

if (true === $input->hasParameterOption(['--help', '-h'])) {// 如果是帮助信息
if (!$name) {// 明显 跟 linux 靠近了
$name  = 'help';// help
$input = new Input(['help']);// 返回帮助信息
} else {
$this->wantHelps = true;// 把想要帮助的信息 设置为1
}
}

if (!$name) {// 如果没有获取到相应的命令
$name  = $this->defaultCommand;// 设置默认的命令
$input = new Input([$this->defaultCommand]);// 输入信息为默认命令
}

$command = $this->find($name);// 查找命令 根据 名字 ,名字,其实就是个索引 主键 id

$exitCode = $this->doRunCommand($command, $input, $output);// 执行 命令 带着 输入 跟输出

return $exitCode;// 返回执行后的结果
}

/**
* 设置输入参数定义
* @param InputDefinition $definition
*/
public function setDefinition(InputDefinition $definition)
{
$this->definition = $definition;
}// 设置输入参数定义

/**
* 获取输入参数定义
* @return InputDefinition The InputDefinition instance
*/
public function getDefinition()
{
return $this->definition;
}// 获取输入 参数 定义

/**
* Gets the help message.
* @return string A help message.
*/
public function getHelp()
{
return $this->getLongVersion();
}// 获取帮助信息 就是 获取版本信息

/**
* 是否捕获异常
* @param bool $boolean
* @api
*/
public function setCatchExceptions($boolean)
{
$this->catchExceptions = (bool)$boolean;
}// 设置 是否 捕获异常,强制  转换 一些 数据

/**
* 是否自动退出
* @param bool $boolean
* @api
*/
public function setAutoExit($boolean)
{
$this->autoExit = (bool)$boolean;
}// 自动退出

/**
* 获取名称
* @return string
*/
public function getName()
{
return $this->name;
}// 返回名字

/**
* 设置名称
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}// 设置名字

/**
* 获取版本
* @return string
* @api
*/
public function getVersion()
{
return $this->version;
}// 设置版本

/**
* 设置版本
* @param string $version
*/
public function setVersion($version)
{
$this->version = $version;
}//设置版本

/**
* 获取完整的版本号
* @return string
*/
public function getLongVersion()
{
if ('UNKNOWN' !== $this->getName() && 'UNKNOWN' !== $this->getVersion()) {
return sprintf('<info>%s</info> version <comment>%s</comment>', $this->getName(), $this->getVersion());
}

return '<info>Console Tool</info>';// 信息 显示 控制
}// 获取版本号

/**
* 注册一个指令
* @param string $name
* @return Command
*/
public function register($name)
{
return $this->add(new Command($name));
}// 注册 指令

/**
* 添加指令
* @param Command[] $commands
*/
public function addCommands(array $commands)
{
foreach ($commands as $command) {
$this->add($command);
}
}// 添加 指令

/**
* 添加一个指令
* @param Command $command
* @return Command
*/
public function add(Command $command)
{
$command->setConsole($this);// 设置命令 控制台

if (!$command->isEnabled()) {
$command->setConsole(null);
return null;
}// 允许设计,进行设置

if (null === $command->getDefinition()) {
throw new \LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command)));
}// 获取默认 参数 定义

$this->commands[$command->getName()] = $command;// 命令 设置

foreach ($command->getAliases() as $alias) {
$this->commands[$alias] = $command;
}// 循环 设置 别名

return $command;// 返回 数据
}

/**
* 获取指令
* @param string $name 指令名称
* @return Command
* @throws \InvalidArgumentException
*/
public function get($name)
{// 获取 指令
if (!isset($this->commands[$name])) {
throw new \InvalidArgumentException(sprintf('The command "%s" does not exist.', $name));
}// 抛出 设置 异常

$command = $this->commands[$name];// 设置命令

if ($this->wantHelps) {
$this->wantHelps = false;

/** @var HelpCommand $helpCommand */
$helpCommand = $this->get('help');
$helpCommand->setCommand($command);

return $helpCommand;
}// 一些 帮助的命令

return $command;// 返回命令
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  thinkphp php 函数