think\Console::findNamespace PHP Method

findNamespace() public method

查找注册命名空间中的名称或缩写。
public findNamespace ( string $namespace ) : string
$namespace string
return string
    public function findNamespace($namespace)
    {
        $allNamespaces = $this->getNamespaces();
        $expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
            return preg_quote($matches[1]) . '[^:]*';
        }, $namespace);
        $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)) {
                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);
        }
        $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))));
        }
        return $exact ? $namespace : reset($namespaces);
    }

Usage Example

コード例 #1
0
ファイル: Console.php プロジェクト: Lofanmi/think
 private function inspectConsole()
 {
     $this->commands = [];
     $this->namespaces = [];
     $all = $this->console->all($this->namespace ? $this->console->findNamespace($this->namespace) : null);
     foreach ($this->sortCommands($all) as $namespace => $commands) {
         $names = [];
         /** @var Command $command */
         foreach ($commands as $name => $command) {
             if (!$command->getName()) {
                 continue;
             }
             if ($command->getName() === $name) {
                 $this->commands[$name] = $command;
             } else {
                 $this->aliases[$name] = $command;
             }
             $names[] = $name;
         }
         $this->namespaces[$namespace] = ['id' => $namespace, 'commands' => $names];
     }
 }