0 追蹤者

類別 yii\console\UnknownCommandException

繼承yii\console\UnknownCommandException » yii\console\Exception » yii\base\UserException » yii\base\Exception » Exception
自版本起可用2.0.11
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/console/UnknownCommandException.php

UnknownCommandException 代表因不正確使用控制台命令而造成的例外。

受保護的屬性

隱藏繼承的屬性

屬性 類型 描述 定義於

屬性詳情

隱藏繼承的屬性

$application 受保護屬性
$command 公共屬性

無法識別的命令名稱。

public string $command null

方法詳情

隱藏繼承的方法

__construct() 公共方法

建構例外。

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);
}

            
getName() 公共方法

public string getName ( )
return 字串

此例外的使用者友善名稱

                public function getName()
{
    return 'Unknown command';
}

            
getSuggestedAlternatives() 公共方法

根據字串相似度,為 $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);
}