| Inheritance | yii\console\UnknownCommandException » yii\console\Exception » yii\base\UserException » yii\base\Exception » Exception |
|---|---|
| Available since version | 2.0.11 |
| Source Code | https://github.com/yiisoft/yii2/blob/master/framework/console/UnknownCommandException.php |
UnknownCommandException represents an exception caused by incorrect usage of a console command.
| Property | Type | Description | Defined By |
|---|---|---|---|
| $command | string | The name of the command that could not be recognized. | yii\console\UnknownCommandException |
| Property | Type | Description | Defined By |
|---|---|---|---|
| $application | yii\console\Application | yii\console\UnknownCommandException |
| Method | Description | Defined By |
|---|---|---|
| __construct() | Construct the exception. | yii\console\UnknownCommandException |
| getName() | yii\console\UnknownCommandException | |
| getSuggestedAlternatives() | Suggest alternative commands for $command based on string similarity. | yii\console\UnknownCommandException |
Construct the exception.
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 | string |
The user-friendly name of this exception |
|---|---|---|
public function getName()
{
return 'Unknown command';
}
Suggest alternative commands for $command based on string similarity.
Alternatives are searched using the following steps:
$commandSee also https://www.php.net/manual/en/function.levenshtein.php.
| public array getSuggestedAlternatives ( ) | ||
| return | array |
A list of suggested alternatives sorted by similarity. |
|---|---|---|
public function getSuggestedAlternatives()
{
$help = $this->application->createController('help');
if ($help === false || $this->command === '') {
return [];
}
/**
* @var HelpController $helpController
* @phpstan-var HelpController<Application> $helpController
*/
list($helpController, $actionID) = $help;
$availableActions = [];
foreach ($helpController->getCommands() as $command) {
$result = $this->application->createController($command);
/**
* @var Controller $controller
* @phpstan-var Controller<Application> $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);
}