You are here

public function Application::find in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/console/Application.php \Symfony\Component\Console\Application::find()

Finds a command by name or alias.

Contrary to get, this command tries to find the best match if you give it an abbreviation of a name or alias.

Parameters

string $name A command name or a command alias:

Return value

Command A Command instance

Throws

\InvalidArgumentException When command name is incorrect or ambiguous

1 call to Application::find()
Application::doRun in vendor/symfony/console/Application.php
Runs the current application.

File

vendor/symfony/console/Application.php, line 488

Class

Application
An Application is the container for a collection of commands.

Namespace

Symfony\Component\Console

Code

public function find($name) {
  $allCommands = array_keys($this->commands);
  $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, ':'))) {

      // check if a namespace exists and contains commands
      $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);
  }

  // filter out aliases for commands which are already on the list
  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));
  }
  return $this
    ->get($exact ? $name : reset($commands));
}