fix:更新已知bug,优化代码

This commit is contained in:
Ying
2022-11-28 19:11:12 +08:00
parent f6aee95cfc
commit 9445b206a2
1378 changed files with 53759 additions and 20789 deletions

View File

@@ -70,23 +70,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 array $commands = [];
private bool $wantHelps = false;
private $runningCommand = null;
private string $name;
private string $version;
private $commandLoader = null;
private bool $catchExceptions = true;
private bool $autoExit = true;
private $definition;
private $helperSet;
private $dispatcher;
private $dispatcher = null;
private $terminal;
private $defaultCommand;
private $singleCommand = false;
private $initialized;
private string $defaultCommand;
private bool $singleCommand = false;
private bool $initialized = false;
private $signalRegistry;
private $signalsToDispatchEvent = [];
private array $signalsToDispatchEvent = [];
public function __construct(string $name = 'UNKNOWN', string $version = 'UNKNOWN')
{
@@ -134,7 +134,7 @@ 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());
@@ -276,7 +276,9 @@ class Application implements ResetInterface
$alternative = $alternatives[0];
$style = new SymfonyStyle($input, $output);
$style->block(sprintf("\nCommand \"%s\" is not defined.\n", $name), null, 'error');
$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);
@@ -316,16 +318,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)
@@ -335,14 +331,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;
@@ -388,20 +380,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;
}
@@ -416,10 +404,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;
}
@@ -434,10 +420,8 @@ class Application implements ResetInterface
/**
* Gets the name of the application.
*
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}
@@ -452,10 +436,8 @@ class Application implements ResetInterface
/**
* Gets the application version.
*
* @return string
*/
public function getVersion()
public function getVersion(): string
{
return $this->version;
}
@@ -488,10 +470,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));
}
@@ -584,10 +564,8 @@ 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();
@@ -601,7 +579,7 @@ class Application implements ResetInterface
*
* @return string[]
*/
public function getNamespaces()
public function getNamespaces(): array
{
$namespaces = [];
foreach ($this->all() as $command) {
@@ -622,11 +600,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))).'[^:]*';
@@ -818,7 +794,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) {
@@ -933,11 +909,21 @@ class Application implements ResetInterface
}
switch ($shellVerbosity = (int) getenv('SHELL_VERBOSITY')) {
case -1: $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); break;
case 1: $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); break;
case 2: $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); break;
case 3: $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); break;
default: $shellVerbosity = 0; break;
case -1:
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
break;
case 1:
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
break;
case 2:
$output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
break;
case 3:
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
break;
default:
$shellVerbosity = 0;
break;
}
if (true === $input->hasParameterOption(['--quiet', '-q'], true)) {
@@ -983,22 +969,30 @@ class Application implements ResetInterface
}
}
if ($command instanceof SignalableCommandInterface && ($this->signalsToDispatchEvent || $command->getSubscribedSignals())) {
if (!$this->signalRegistry) {
throw new RuntimeException('Unable to subscribe to signal events. Make sure that the `pcntl` extension is installed and that "pcntl_*" functions are not disabled by your php.ini\'s "disable_functions" directive.');
}
if ($this->signalsToDispatchEvent) {
$commandSignals = $command instanceof SignalableCommandInterface ? $command->getSubscribedSignals() : [];
if (Terminal::hasSttyAvailable()) {
$sttyMode = shell_exec('stty -g');
if ($commandSignals || null !== $this->dispatcher) {
if (!$this->signalRegistry) {
throw new RuntimeException('Unable to subscribe to signal events. Make sure that the `pcntl` extension is installed and that "pcntl_*" functions are not disabled by your php.ini\'s "disable_functions" directive.');
}
foreach ([\SIGINT, \SIGTERM] as $signal) {
$this->signalRegistry->register($signal, static function () use ($sttyMode) {
shell_exec('stty '.$sttyMode);
});
if (Terminal::hasSttyAvailable()) {
$sttyMode = shell_exec('stty -g');
foreach ([\SIGINT, \SIGTERM] as $signal) {
$this->signalRegistry->register($signal, static function () use ($sttyMode) {
shell_exec('stty '.$sttyMode);
});
}
}
foreach ($commandSignals as $signal) {
$this->signalRegistry->register($signal, [$command, 'handleSignal']);
}
}
if ($this->dispatcher) {
if (null !== $this->dispatcher) {
foreach ($this->signalsToDispatchEvent as $signal) {
$event = new ConsoleSignalEvent($command, $input, $output, $signal);
@@ -1014,10 +1008,6 @@ class Application implements ResetInterface
});
}
}
foreach ($command->getSubscribedSignals() as $signal) {
$this->signalRegistry->register($signal, [$command, 'handleSignal']);
}
}
if (null === $this->dispatcher) {
@@ -1065,20 +1055,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'),
@@ -1096,17 +1082,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(),
@@ -1128,10 +1112,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);
@@ -1191,7 +1173,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];