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

[李景山php]每天TP5-20161226|thinkphp5-Console.php-3

2016-11-18 08:36 357 查看
/**
* 某个指令是否存在
* @param string $name 指令名称
* @return bool
*/
public function has($name)
{
return isset($this->commands[$name]);
}// 判读是否拥有

/**
* 获取所有的命名空间
* @return array
*/
public function getNamespaces()
{
$namespaces = [];
foreach ($this->commands as $command) {
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));

foreach ($command->getAliases() as $alias) {
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($alias));
}
}

return array_values(array_unique(array_filter($namespaces)));
}// 命名空间大清洗, 过滤 唯一 返回值

/**
* 查找注册命名空间中的名称或缩写。
* @param string $namespace
* @return string
* @throws \InvalidArgumentException
*/
public function findNamespace($namespace)
{// 查找注册命名空间中的名称或缩写
$allNamespaces = $this->getNamespaces();// 获取 命名空间
$expr          = preg_replace_callback('{([^:]+|)}', function ($matches) {
return preg_quote($matches[1]) . '[^:]*';
}, $namespace);// preg_replace_callback
$namespaces    = preg_grep('{^' . $expr . '}', $allNamespaces);// 正则 处理 数据

if (empty($namespaces)) {// 如果为空
$message = sprintf('There are no commands defined in the "%s" namespace.', $namespace);
// 如果 信息 不存在
if ($alternatives = $this->findAlternatives($namespace, $allNamespaces)) {// 找到 对应的 findAlter natives
if (1 == count($alternatives)) {// 若干 count ==1
$message .= "\n\nDid you mean this?\n    ";
} else {
$message .= "\n\nDid you mean one of these?\n    ";
}// 设置信息

$message .= implode("\n    ", $alternatives); // 获取信息
}

throw new \InvalidArgumentException($message);// 抛出异常
}

$exact = in_array($namespace, $namespaces, true);// 在 数组中
if (count($namespaces) > 1 && !$exact) {
throw new \InvalidArgumentException(sprintf('The namespace "%s" is ambiguous (%s).', $namespace, $this->getAbbreviationSuggestions(array_values($namespaces))));
}// count($namespaces)

return $exact ? $namespace : reset($namespaces);// 返回命名空间
}

/**
* 查找指令
* @param string $name 名称或者别名
* @return Command
* @throws \InvalidArgumentException
*/
public function find($name)
{// 简单的查找命名
$allCommands = array_keys($this->commands);// 获取 key 数据
$expr        = preg_replace_callback('{([^:]+|)}', function ($matches) {
return preg_quote($matches[1]) . '[^:]*';
}, $name);// 正则 回调 替换
$commands    = preg_grep('{^' . $expr . '}', $allCommands);// 正则搜索 数据

if (empty($commands) || count(preg_grep('{^' . $expr . '$}', $commands)) < 1) {
if (false !== $pos = strrpos($name, ':')) {// 不为空
$this->findNamespace(substr($name, 0, $pos));// 查找命名空间
}//命令数据为空,获取 扶着的 数据为空

$message = sprintf('Command "%s" is not defined.', $name);// 格式化 输出 信息

if ($alternatives = $this->findAlternatives($name, $allCommands)) {
if (1 == count($alternatives)) {
$message .= "\n\nDid you mean this?\n    ";
} else {
$message .= "\n\nDid you mean one of these?\n    ";
}
$message .= implode("\n    ", $alternatives);
}// 格式化 处理 返回信息

throw new \InvalidArgumentException($message);
}// 抛出 参数 异常

if (count($commands) > 1) {
$commandList = $this->commands;
$commands    = array_filter($commands, function ($nameOrAlias) use ($commandList, $commands) {
$commandName = $commandList[$nameOrAlias]->getName();

return $commandName === $nameOrAlias || !in_array($commandName, $commands);
});
}// 计算 命令行

$exact = in_array($name, $commands, true);
if (count($commands) > 1 && !$exact) {
$suggestions = $this->getAbbreviationSuggestions(array_values($commands));

throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $name, $suggestions));
}// 如果 大于1

return $this->get($exact ? $name : reset($commands));// 返回获取到的信息
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  thinkphp