package and depencies
This commit is contained in:
188
vendor/symfony/console/Application.php
vendored
188
vendor/symfony/console/Application.php
vendored
@@ -21,6 +21,7 @@ use Symfony\Component\Console\Command\SignalableCommandInterface;
|
||||
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
|
||||
use Symfony\Component\Console\Completion\CompletionInput;
|
||||
use Symfony\Component\Console\Completion\CompletionSuggestions;
|
||||
use Symfony\Component\Console\Completion\Suggestion;
|
||||
use Symfony\Component\Console\Event\ConsoleCommandEvent;
|
||||
use Symfony\Component\Console\Event\ConsoleErrorEvent;
|
||||
use Symfony\Component\Console\Event\ConsoleSignalEvent;
|
||||
@@ -32,6 +33,7 @@ use Symfony\Component\Console\Exception\NamespaceNotFoundException;
|
||||
use Symfony\Component\Console\Exception\RuntimeException;
|
||||
use Symfony\Component\Console\Formatter\OutputFormatter;
|
||||
use Symfony\Component\Console\Helper\DebugFormatterHelper;
|
||||
use Symfony\Component\Console\Helper\DescriptorHelper;
|
||||
use Symfony\Component\Console\Helper\FormatterHelper;
|
||||
use Symfony\Component\Console\Helper\Helper;
|
||||
use Symfony\Component\Console\Helper\HelperSet;
|
||||
@@ -70,23 +72,23 @@ use Symfony\Contracts\Service\ResetInterface;
|
||||
*/
|
||||
class Application implements ResetInterface
|
||||
{
|
||||
private $commands = [];
|
||||
private $wantHelps = false;
|
||||
private $runningCommand;
|
||||
private $name;
|
||||
private $version;
|
||||
private $commandLoader;
|
||||
private $catchExceptions = true;
|
||||
private $autoExit = true;
|
||||
private $definition;
|
||||
private $helperSet;
|
||||
private $dispatcher;
|
||||
private $terminal;
|
||||
private $defaultCommand;
|
||||
private $singleCommand = false;
|
||||
private $initialized;
|
||||
private $signalRegistry;
|
||||
private $signalsToDispatchEvent = [];
|
||||
private array $commands = [];
|
||||
private bool $wantHelps = false;
|
||||
private ?Command $runningCommand = null;
|
||||
private string $name;
|
||||
private string $version;
|
||||
private ?CommandLoaderInterface $commandLoader = null;
|
||||
private bool $catchExceptions = true;
|
||||
private bool $autoExit = true;
|
||||
private InputDefinition $definition;
|
||||
private HelperSet $helperSet;
|
||||
private ?EventDispatcherInterface $dispatcher = null;
|
||||
private Terminal $terminal;
|
||||
private string $defaultCommand;
|
||||
private bool $singleCommand = false;
|
||||
private bool $initialized = false;
|
||||
private SignalRegistry $signalRegistry;
|
||||
private array $signalsToDispatchEvent = [];
|
||||
|
||||
public function __construct(string $name = 'UNKNOWN', string $version = 'UNKNOWN')
|
||||
{
|
||||
@@ -134,20 +136,15 @@ class Application implements ResetInterface
|
||||
*
|
||||
* @throws \Exception When running fails. Bypass this when {@link setCatchExceptions()}.
|
||||
*/
|
||||
public function run(InputInterface $input = null, OutputInterface $output = null)
|
||||
public function run(InputInterface $input = null, OutputInterface $output = null): int
|
||||
{
|
||||
if (\function_exists('putenv')) {
|
||||
@putenv('LINES='.$this->terminal->getHeight());
|
||||
@putenv('COLUMNS='.$this->terminal->getWidth());
|
||||
}
|
||||
|
||||
if (null === $input) {
|
||||
$input = new ArgvInput();
|
||||
}
|
||||
|
||||
if (null === $output) {
|
||||
$output = new ConsoleOutput();
|
||||
}
|
||||
$input ??= new ArgvInput();
|
||||
$output ??= new ConsoleOutput();
|
||||
|
||||
$renderException = function (\Throwable $e) use ($output) {
|
||||
if ($output instanceof ConsoleOutputInterface) {
|
||||
@@ -228,7 +225,7 @@ class Application implements ResetInterface
|
||||
try {
|
||||
// Makes ArgvInput::getFirstArgument() able to distinguish an option from an argument.
|
||||
$input->bind($this->getDefinition());
|
||||
} catch (ExceptionInterface $e) {
|
||||
} catch (ExceptionInterface) {
|
||||
// Errors must be ignored, full binding/validation happens later when the command is known.
|
||||
}
|
||||
|
||||
@@ -258,7 +255,26 @@ class Application implements ResetInterface
|
||||
// the command name MUST be the first element of the input
|
||||
$command = $this->find($name);
|
||||
} catch (\Throwable $e) {
|
||||
if (!($e instanceof CommandNotFoundException && !$e instanceof NamespaceNotFoundException) || 1 !== \count($alternatives = $e->getAlternatives()) || !$input->isInteractive()) {
|
||||
if (($e instanceof CommandNotFoundException && !$e instanceof NamespaceNotFoundException) && 1 === \count($alternatives = $e->getAlternatives()) && $input->isInteractive()) {
|
||||
$alternative = $alternatives[0];
|
||||
|
||||
$style = new SymfonyStyle($input, $output);
|
||||
$output->writeln('');
|
||||
$formattedBlock = (new FormatterHelper())->formatBlock(sprintf('Command "%s" is not defined.', $name), 'error', true);
|
||||
$output->writeln($formattedBlock);
|
||||
if (!$style->confirm(sprintf('Do you want to run "%s" instead? ', $alternative), false)) {
|
||||
if (null !== $this->dispatcher) {
|
||||
$event = new ConsoleErrorEvent($input, $output, $e);
|
||||
$this->dispatcher->dispatch($event, ConsoleEvents::ERROR);
|
||||
|
||||
return $event->getExitCode();
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
$command = $this->find($alternative);
|
||||
} else {
|
||||
if (null !== $this->dispatcher) {
|
||||
$event = new ConsoleErrorEvent($input, $output, $e);
|
||||
$this->dispatcher->dispatch($event, ConsoleEvents::ERROR);
|
||||
@@ -270,27 +286,24 @@ class Application implements ResetInterface
|
||||
$e = $event->getError();
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
try {
|
||||
if ($e instanceof CommandNotFoundException && $namespace = $this->findNamespace($name)) {
|
||||
$helper = new DescriptorHelper();
|
||||
$helper->describe($output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output, $this, [
|
||||
'format' => 'txt',
|
||||
'raw_text' => false,
|
||||
'namespace' => $namespace,
|
||||
'short' => false,
|
||||
]);
|
||||
|
||||
$alternative = $alternatives[0];
|
||||
return isset($event) ? $event->getExitCode() : 1;
|
||||
}
|
||||
|
||||
$style = new SymfonyStyle($input, $output);
|
||||
$output->writeln('');
|
||||
$formattedBlock = (new FormatterHelper())->formatBlock(sprintf('Command "%s" is not defined.', $name), 'error', true);
|
||||
$output->writeln($formattedBlock);
|
||||
if (!$style->confirm(sprintf('Do you want to run "%s" instead? ', $alternative), false)) {
|
||||
if (null !== $this->dispatcher) {
|
||||
$event = new ConsoleErrorEvent($input, $output, $e);
|
||||
$this->dispatcher->dispatch($event, ConsoleEvents::ERROR);
|
||||
|
||||
return $event->getExitCode();
|
||||
throw $e;
|
||||
} catch (NamespaceNotFoundException) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
$command = $this->find($alternative);
|
||||
}
|
||||
|
||||
if ($command instanceof LazyCommand) {
|
||||
@@ -304,9 +317,6 @@ class Application implements ResetInterface
|
||||
return $exitCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function reset()
|
||||
{
|
||||
}
|
||||
@@ -318,16 +328,10 @@ class Application implements ResetInterface
|
||||
|
||||
/**
|
||||
* Get the helper set associated with the command.
|
||||
*
|
||||
* @return HelperSet
|
||||
*/
|
||||
public function getHelperSet()
|
||||
public function getHelperSet(): HelperSet
|
||||
{
|
||||
if (!$this->helperSet) {
|
||||
$this->helperSet = $this->getDefaultHelperSet();
|
||||
}
|
||||
|
||||
return $this->helperSet;
|
||||
return $this->helperSet ??= $this->getDefaultHelperSet();
|
||||
}
|
||||
|
||||
public function setDefinition(InputDefinition $definition)
|
||||
@@ -337,14 +341,10 @@ class Application implements ResetInterface
|
||||
|
||||
/**
|
||||
* Gets the InputDefinition related to this Application.
|
||||
*
|
||||
* @return InputDefinition
|
||||
*/
|
||||
public function getDefinition()
|
||||
public function getDefinition(): InputDefinition
|
||||
{
|
||||
if (!$this->definition) {
|
||||
$this->definition = $this->getDefaultInputDefinition();
|
||||
}
|
||||
$this->definition ??= $this->getDefaultInputDefinition();
|
||||
|
||||
if ($this->singleCommand) {
|
||||
$inputDefinition = $this->definition;
|
||||
@@ -365,18 +365,16 @@ class Application implements ResetInterface
|
||||
CompletionInput::TYPE_ARGUMENT_VALUE === $input->getCompletionType()
|
||||
&& 'command' === $input->getCompletionName()
|
||||
) {
|
||||
$commandNames = [];
|
||||
foreach ($this->all() as $name => $command) {
|
||||
// skip hidden commands and aliased commands as they already get added below
|
||||
if ($command->isHidden() || $command->getName() !== $name) {
|
||||
continue;
|
||||
}
|
||||
$commandNames[] = $command->getName();
|
||||
$suggestions->suggestValue(new Suggestion($command->getName(), $command->getDescription()));
|
||||
foreach ($command->getAliases() as $name) {
|
||||
$commandNames[] = $name;
|
||||
$suggestions->suggestValue(new Suggestion($name, $command->getDescription()));
|
||||
}
|
||||
}
|
||||
$suggestions->suggestValues(array_filter($commandNames));
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -390,20 +388,16 @@ class Application implements ResetInterface
|
||||
|
||||
/**
|
||||
* Gets the help message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getHelp()
|
||||
public function getHelp(): string
|
||||
{
|
||||
return $this->getLongVersion();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether to catch exceptions or not during commands execution.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function areExceptionsCaught()
|
||||
public function areExceptionsCaught(): bool
|
||||
{
|
||||
return $this->catchExceptions;
|
||||
}
|
||||
@@ -418,10 +412,8 @@ class Application implements ResetInterface
|
||||
|
||||
/**
|
||||
* Gets whether to automatically exit after a command execution or not.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isAutoExitEnabled()
|
||||
public function isAutoExitEnabled(): bool
|
||||
{
|
||||
return $this->autoExit;
|
||||
}
|
||||
@@ -436,10 +428,8 @@ class Application implements ResetInterface
|
||||
|
||||
/**
|
||||
* Gets the name of the application.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
@@ -454,10 +444,8 @@ class Application implements ResetInterface
|
||||
|
||||
/**
|
||||
* Gets the application version.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getVersion()
|
||||
public function getVersion(): string
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
@@ -490,10 +478,8 @@ class Application implements ResetInterface
|
||||
|
||||
/**
|
||||
* Registers a new command.
|
||||
*
|
||||
* @return Command
|
||||
*/
|
||||
public function register(string $name)
|
||||
public function register(string $name): Command
|
||||
{
|
||||
return $this->add(new Command($name));
|
||||
}
|
||||
@@ -586,14 +572,12 @@ class Application implements ResetInterface
|
||||
|
||||
/**
|
||||
* Returns true if the command exists, false otherwise.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has(string $name)
|
||||
public function has(string $name): bool
|
||||
{
|
||||
$this->init();
|
||||
|
||||
return isset($this->commands[$name]) || ($this->commandLoader && $this->commandLoader->has($name) && $this->add($this->commandLoader->get($name)));
|
||||
return isset($this->commands[$name]) || ($this->commandLoader?->has($name) && $this->add($this->commandLoader->get($name)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -603,7 +587,7 @@ class Application implements ResetInterface
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNamespaces()
|
||||
public function getNamespaces(): array
|
||||
{
|
||||
$namespaces = [];
|
||||
foreach ($this->all() as $command) {
|
||||
@@ -624,11 +608,9 @@ class Application implements ResetInterface
|
||||
/**
|
||||
* Finds a registered namespace by a name or an abbreviation.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws NamespaceNotFoundException When namespace is incorrect or ambiguous
|
||||
*/
|
||||
public function findNamespace(string $namespace)
|
||||
public function findNamespace(string $namespace): string
|
||||
{
|
||||
$allNamespaces = $this->getNamespaces();
|
||||
$expr = implode('[^:]*:', array_map('preg_quote', explode(':', $namespace))).'[^:]*';
|
||||
@@ -820,7 +802,7 @@ class Application implements ResetInterface
|
||||
*
|
||||
* @return string[][]
|
||||
*/
|
||||
public static function getAbbreviations(array $names)
|
||||
public static function getAbbreviations(array $names): array
|
||||
{
|
||||
$abbrevs = [];
|
||||
foreach ($names as $name) {
|
||||
@@ -1044,7 +1026,7 @@ class Application implements ResetInterface
|
||||
try {
|
||||
$command->mergeApplicationDefinition();
|
||||
$input->bind($command->getDefinition());
|
||||
} catch (ExceptionInterface $e) {
|
||||
} catch (ExceptionInterface) {
|
||||
// ignore invalid options/arguments for now, to allow the event listeners to customize the InputDefinition
|
||||
}
|
||||
|
||||
@@ -1081,20 +1063,16 @@ class Application implements ResetInterface
|
||||
|
||||
/**
|
||||
* Gets the name of the command based on input.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
protected function getCommandName(InputInterface $input)
|
||||
protected function getCommandName(InputInterface $input): ?string
|
||||
{
|
||||
return $this->singleCommand ? $this->defaultCommand : $input->getFirstArgument();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default input definition.
|
||||
*
|
||||
* @return InputDefinition
|
||||
*/
|
||||
protected function getDefaultInputDefinition()
|
||||
protected function getDefaultInputDefinition(): InputDefinition
|
||||
{
|
||||
return new InputDefinition([
|
||||
new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
|
||||
@@ -1112,17 +1090,15 @@ class Application implements ResetInterface
|
||||
*
|
||||
* @return Command[]
|
||||
*/
|
||||
protected function getDefaultCommands()
|
||||
protected function getDefaultCommands(): array
|
||||
{
|
||||
return [new HelpCommand(), new ListCommand(), new CompleteCommand(), new DumpCompletionCommand()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default helper set with the helpers that should always be available.
|
||||
*
|
||||
* @return HelperSet
|
||||
*/
|
||||
protected function getDefaultHelperSet()
|
||||
protected function getDefaultHelperSet(): HelperSet
|
||||
{
|
||||
return new HelperSet([
|
||||
new FormatterHelper(),
|
||||
@@ -1144,10 +1120,8 @@ class Application implements ResetInterface
|
||||
* Returns the namespace part of the command name.
|
||||
*
|
||||
* This method is not part of public API and should not be used directly.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function extractNamespace(string $name, int $limit = null)
|
||||
public function extractNamespace(string $name, int $limit = null): string
|
||||
{
|
||||
$parts = explode(':', $name, -1);
|
||||
|
||||
@@ -1207,7 +1181,7 @@ class Application implements ResetInterface
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDefaultCommand(string $commandName, bool $isSingleCommand = false)
|
||||
public function setDefaultCommand(string $commandName, bool $isSingleCommand = false): static
|
||||
{
|
||||
$this->defaultCommand = explode('|', ltrim($commandName, '|'))[0];
|
||||
|
||||
|
Reference in New Issue
Block a user