類別 yii\console\UnknownCommandException
UnknownCommandException 代表因不正確使用控制台命令而造成的例外。
公共屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$application | yii\console\Application | yii\console\UnknownCommandException | |
$command | 字串 | 無法識別的命令名稱。 | yii\console\UnknownCommandException |
公共方法
屬性詳情
方法詳情
建構例外。
public void __construct ( $route, $application, $code = 0, $previous = null ) | ||
$route | 字串 |
找不到的命令路由。 |
$application | yii\console\Application |
涉及的控制台應用程式實例。 |
$code | 整數 |
例外代碼。 |
$previous | Throwable|null |
用於例外鏈結的前一個例外。 |
public function __construct($route, $application, $code = 0, $previous = null)
{
$this->command = $route;
$this->application = $application;
parent::__construct("Unknown command \"$route\".", $code, $previous);
}
public string getName ( ) | ||
return | 字串 |
此例外的使用者友善名稱 |
---|
public function getName()
{
return 'Unknown command';
}
根據字串相似度,為 $command 建議替代命令。
替代方案會使用以下步驟搜尋
- 建議以
$command
開頭的替代方案 - 透過計算未知命令與所有可用命令之間的 Levenshtein 距離來尋找錯字。Levenshtein 距離定義為將 str1 轉換為 str2 必須替換、插入或刪除的最少字元數。
另請參閱 https://php.dev.org.tw/manual/en/function.levenshtein.php。
public array getSuggestedAlternatives ( ) | ||
return | array |
依相似度排序的建議替代方案列表。 |
---|
public function getSuggestedAlternatives()
{
$help = $this->application->createController('help');
if ($help === false || $this->command === '') {
return [];
}
/** @var $helpController HelpController */
list($helpController, $actionID) = $help;
$availableActions = [];
foreach ($helpController->getCommands() as $command) {
$result = $this->application->createController($command);
/** @var $controller Controller */
list($controller, $actionID) = $result;
if ($controller->createAction($controller->defaultAction) !== null) {
// add the command itself (default action)
$availableActions[] = $command;
}
// add all actions of this controller
$actions = $helpController->getActions($controller);
$prefix = $controller->getUniqueId();
foreach ($actions as $action) {
$availableActions[] = $prefix . '/' . $action;
}
}
return $this->filterBySimilarity($availableActions, $this->command);
}
註冊 或 登入 以發表評論。