標籤:thinkphp
/** * 某個指令是否存在 * @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));// 返回擷取到的資訊 }
本文出自 “專註php 群號:414194301” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1874109
[李景山php]每天TP5-20161226|thinkphp5-Console.php-3