示例#1
0
 /**
  * Suggest alternative commands for [[$command]] based on string similarity.
  *
  * Alternatives are searched using the following steps:
  *
  * - suggest alternatives that begin with `$command`
  * - find typos by calculating the Levenshtein distance between the unknown command and all
  *   available commands. The Levenshtein distance is defined as the minimal number of
  *   characters you have to replace, insert or delete to transform str1 into str2.
  *
  * @see http://php.net/manual/en/function.levenshtein.php
  * @return array a list of suggested alternatives sorted by similarity.
  */
 public function getSuggestedAlternatives()
 {
     $help = $this->application->createController('help');
     if ($help === false) {
         return [];
     }
     /** @var $helpController HelpController */
     list($helpController, $actionID) = $help;
     $availableActions = [];
     $commands = $helpController->getCommands();
     foreach ($commands as $command) {
         $result = $this->application->createController($command);
         if ($result === false) {
             continue;
         }
         // add the command itself (default action)
         $availableActions[] = $command;
         // add all actions of this controller
         /** @var $controller Controller */
         list($controller, $actionID) = $result;
         $actions = $helpController->getActions($controller);
         if (!empty($actions)) {
             $prefix = $controller->getUniqueId();
             foreach ($actions as $action) {
                 $availableActions[] = $prefix . '/' . $action;
             }
         }
     }
     return $this->filterBySimilarity($availableActions, $this->command);
 }