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

    /**
     * 某个指令是否存在
     * @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));// 返回获取到的信息
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值