Dep: update
주로 PHAN
This commit is contained in:
+97
-33
@@ -13,15 +13,19 @@ namespace Symfony\Component\Console;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Command\HelpCommand;
|
||||
use Symfony\Component\Console\Command\LazyCommand;
|
||||
use Symfony\Component\Console\Command\ListCommand;
|
||||
use Symfony\Component\Console\Command\SignalableCommandInterface;
|
||||
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
|
||||
use Symfony\Component\Console\Event\ConsoleCommandEvent;
|
||||
use Symfony\Component\Console\Event\ConsoleErrorEvent;
|
||||
use Symfony\Component\Console\Event\ConsoleSignalEvent;
|
||||
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
|
||||
use Symfony\Component\Console\Exception\CommandNotFoundException;
|
||||
use Symfony\Component\Console\Exception\ExceptionInterface;
|
||||
use Symfony\Component\Console\Exception\LogicException;
|
||||
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\FormatterHelper;
|
||||
@@ -39,6 +43,7 @@ use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\ConsoleOutput;
|
||||
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\SignalRegistry\SignalRegistry;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\ErrorHandler\ErrorHandler;
|
||||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||
@@ -76,6 +81,8 @@ class Application implements ResetInterface
|
||||
private $defaultCommand;
|
||||
private $singleCommand = false;
|
||||
private $initialized;
|
||||
private $signalRegistry;
|
||||
private $signalsToDispatchEvent = [];
|
||||
|
||||
public function __construct(string $name = 'UNKNOWN', string $version = 'UNKNOWN')
|
||||
{
|
||||
@@ -83,6 +90,10 @@ class Application implements ResetInterface
|
||||
$this->version = $version;
|
||||
$this->terminal = new Terminal();
|
||||
$this->defaultCommand = 'list';
|
||||
if (\defined('SIGINT') && SignalRegistry::isSupported()) {
|
||||
$this->signalRegistry = new SignalRegistry();
|
||||
$this->signalsToDispatchEvent = [\SIGINT, \SIGTERM, \SIGUSR1, \SIGUSR2];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,6 +109,20 @@ class Application implements ResetInterface
|
||||
$this->commandLoader = $commandLoader;
|
||||
}
|
||||
|
||||
public function getSignalRegistry(): SignalRegistry
|
||||
{
|
||||
if (!$this->signalRegistry) {
|
||||
throw new RuntimeException('Signals are not supported. Make sure that the `pcntl` extension is installed and that "pcntl_*" functions are not disabled by your php.ini\'s "disable_functions" directive.');
|
||||
}
|
||||
|
||||
return $this->signalRegistry;
|
||||
}
|
||||
|
||||
public function setSignalsToDispatchEvent(int ...$signalsToDispatchEvent)
|
||||
{
|
||||
$this->signalsToDispatchEvent = $signalsToDispatchEvent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the current application.
|
||||
*
|
||||
@@ -107,8 +132,10 @@ class Application implements ResetInterface
|
||||
*/
|
||||
public function run(InputInterface $input = null, OutputInterface $output = null)
|
||||
{
|
||||
putenv('LINES='.$this->terminal->getHeight());
|
||||
putenv('COLUMNS='.$this->terminal->getWidth());
|
||||
if (\function_exists('putenv')) {
|
||||
@putenv('LINES='.$this->terminal->getHeight());
|
||||
@putenv('COLUMNS='.$this->terminal->getWidth());
|
||||
}
|
||||
|
||||
if (null === $input) {
|
||||
$input = new ArgvInput();
|
||||
@@ -260,6 +287,10 @@ class Application implements ResetInterface
|
||||
$command = $this->find($alternative);
|
||||
}
|
||||
|
||||
if ($command instanceof LazyCommand) {
|
||||
$command = $command->getCommand();
|
||||
}
|
||||
|
||||
$this->runningCommand = $command;
|
||||
$exitCode = $this->doRunCommand($command, $input, $output);
|
||||
$this->runningCommand = null;
|
||||
@@ -463,11 +494,13 @@ class Application implements ResetInterface
|
||||
return null;
|
||||
}
|
||||
|
||||
// Will throw if the command is not correctly initialized.
|
||||
$command->getDefinition();
|
||||
if (!$command instanceof LazyCommand) {
|
||||
// Will throw if the command is not correctly initialized.
|
||||
$command->getDefinition();
|
||||
}
|
||||
|
||||
if (!$command->getName()) {
|
||||
throw new LogicException(sprintf('The command defined in "%s" cannot have an empty name.', \get_class($command)));
|
||||
throw new LogicException(sprintf('The command defined in "%s" cannot have an empty name.', get_debug_type($command)));
|
||||
}
|
||||
|
||||
$this->commands[$command->getName()] = $command;
|
||||
@@ -494,6 +527,11 @@ class Application implements ResetInterface
|
||||
throw new CommandNotFoundException(sprintf('The command "%s" does not exist.', $name));
|
||||
}
|
||||
|
||||
// When the command has a different name than the one used at the command loader level
|
||||
if (!isset($this->commands[$name])) {
|
||||
throw new CommandNotFoundException(sprintf('The "%s" command cannot be found because it is registered under multiple names. Make sure you don\'t set a different name via constructor or "setName()".', $name));
|
||||
}
|
||||
|
||||
$command = $this->commands[$name];
|
||||
|
||||
if ($this->wantHelps) {
|
||||
@@ -555,7 +593,7 @@ class Application implements ResetInterface
|
||||
public function findNamespace(string $namespace)
|
||||
{
|
||||
$allNamespaces = $this->getNamespaces();
|
||||
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $namespace);
|
||||
$expr = implode('[^:]*:', array_map('preg_quote', explode(':', $namespace))).'[^:]*';
|
||||
$namespaces = preg_grep('{^'.$expr.'}', $allNamespaces);
|
||||
|
||||
if (empty($namespaces)) {
|
||||
@@ -611,7 +649,7 @@ class Application implements ResetInterface
|
||||
}
|
||||
|
||||
$allCommands = $this->commandLoader ? array_merge($this->commandLoader->getNames(), array_keys($this->commands)) : array_keys($this->commands);
|
||||
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
|
||||
$expr = implode('[^:]*:', array_map('preg_quote', explode(':', $name))).'[^:]*';
|
||||
$commands = preg_grep('{^'.$expr.'}', $allCommands);
|
||||
|
||||
if (empty($commands)) {
|
||||
@@ -665,7 +703,7 @@ class Application implements ResetInterface
|
||||
$abbrevs = array_values($commands);
|
||||
$maxLen = 0;
|
||||
foreach ($abbrevs as $abbrev) {
|
||||
$maxLen = max(Helper::strlen($abbrev), $maxLen);
|
||||
$maxLen = max(Helper::width($abbrev), $maxLen);
|
||||
}
|
||||
$abbrevs = array_map(function ($cmd) use ($commandList, $usableWidth, $maxLen, &$commands) {
|
||||
if ($commandList[$cmd]->isHidden()) {
|
||||
@@ -676,7 +714,7 @@ class Application implements ResetInterface
|
||||
|
||||
$abbrev = str_pad($cmd, $maxLen, ' ').' '.$commandList[$cmd]->getDescription();
|
||||
|
||||
return Helper::strlen($abbrev) > $usableWidth ? Helper::substr($abbrev, 0, $usableWidth - 3).'...' : $abbrev;
|
||||
return Helper::width($abbrev) > $usableWidth ? Helper::substr($abbrev, 0, $usableWidth - 3).'...' : $abbrev;
|
||||
}, array_values($commands));
|
||||
|
||||
if (\count($commands) > 1) {
|
||||
@@ -764,7 +802,7 @@ class Application implements ResetInterface
|
||||
$this->doRenderThrowable($e, $output);
|
||||
|
||||
if (null !== $this->runningCommand) {
|
||||
$output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())), OutputInterface::VERBOSITY_QUIET);
|
||||
$output->writeln(sprintf('<info>%s</info>', OutputFormatter::escape(sprintf($this->runningCommand->getSynopsis(), $this->getName()))), OutputInterface::VERBOSITY_QUIET);
|
||||
$output->writeln('', OutputInterface::VERBOSITY_QUIET);
|
||||
}
|
||||
}
|
||||
@@ -774,26 +812,25 @@ class Application implements ResetInterface
|
||||
do {
|
||||
$message = trim($e->getMessage());
|
||||
if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
|
||||
$class = \get_class($e);
|
||||
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
|
||||
$class = get_debug_type($e);
|
||||
$title = sprintf(' [%s%s] ', $class, 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : '');
|
||||
$len = Helper::strlen($title);
|
||||
$len = Helper::width($title);
|
||||
} else {
|
||||
$len = 0;
|
||||
}
|
||||
|
||||
if (false !== strpos($message, "class@anonymous\0")) {
|
||||
$message = preg_replace_callback('/class@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) {
|
||||
return class_exists($m[0], false) ? get_parent_class($m[0]).'@anonymous' : $m[0];
|
||||
if (str_contains($message, "@anonymous\0")) {
|
||||
$message = preg_replace_callback('/[a-zA-Z_\x7f-\xff][\\\\a-zA-Z0-9_\x7f-\xff]*+@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) {
|
||||
return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0])) ?: 'class').'@anonymous' : $m[0];
|
||||
}, $message);
|
||||
}
|
||||
|
||||
$width = $this->terminal->getWidth() ? $this->terminal->getWidth() - 1 : PHP_INT_MAX;
|
||||
$width = $this->terminal->getWidth() ? $this->terminal->getWidth() - 1 : \PHP_INT_MAX;
|
||||
$lines = [];
|
||||
foreach ('' !== $message ? preg_split('/\r?\n/', $message) : [] as $line) {
|
||||
foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
|
||||
// pre-format lines to get the right string length
|
||||
$lineLength = Helper::strlen($line) + 4;
|
||||
$lineLength = Helper::width($line) + 4;
|
||||
$lines[] = [$line, $lineLength];
|
||||
|
||||
$len = max($lineLength, $len);
|
||||
@@ -806,7 +843,7 @@ class Application implements ResetInterface
|
||||
}
|
||||
$messages[] = $emptyLine = sprintf('<error>%s</error>', str_repeat(' ', $len));
|
||||
if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
|
||||
$messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - Helper::strlen($title))));
|
||||
$messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - Helper::width($title))));
|
||||
}
|
||||
foreach ($lines as $line) {
|
||||
$messages[] = sprintf('<error> %s %s</error>', OutputFormatter::escape($line[0]), str_repeat(' ', $len - $line[1]));
|
||||
@@ -830,11 +867,11 @@ class Application implements ResetInterface
|
||||
]);
|
||||
|
||||
for ($i = 0, $count = \count($trace); $i < $count; ++$i) {
|
||||
$class = isset($trace[$i]['class']) ? $trace[$i]['class'] : '';
|
||||
$type = isset($trace[$i]['type']) ? $trace[$i]['type'] : '';
|
||||
$function = isset($trace[$i]['function']) ? $trace[$i]['function'] : '';
|
||||
$file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a';
|
||||
$line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a';
|
||||
$class = $trace[$i]['class'] ?? '';
|
||||
$type = $trace[$i]['type'] ?? '';
|
||||
$function = $trace[$i]['function'] ?? '';
|
||||
$file = $trace[$i]['file'] ?? 'n/a';
|
||||
$line = $trace[$i]['line'] ?? 'n/a';
|
||||
|
||||
$output->writeln(sprintf(' %s%s at <info>%s:%s</info>', $class, $function ? $type.$function.'()' : '', $file, $line), OutputInterface::VERBOSITY_QUIET);
|
||||
}
|
||||
@@ -887,7 +924,9 @@ class Application implements ResetInterface
|
||||
$input->setInteractive(false);
|
||||
}
|
||||
|
||||
putenv('SHELL_VERBOSITY='.$shellVerbosity);
|
||||
if (\function_exists('putenv')) {
|
||||
@putenv('SHELL_VERBOSITY='.$shellVerbosity);
|
||||
}
|
||||
$_ENV['SHELL_VERBOSITY'] = $shellVerbosity;
|
||||
$_SERVER['SHELL_VERBOSITY'] = $shellVerbosity;
|
||||
}
|
||||
@@ -908,6 +947,33 @@ 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->dispatcher) {
|
||||
foreach ($this->signalsToDispatchEvent as $signal) {
|
||||
$event = new ConsoleSignalEvent($command, $input, $output, $signal);
|
||||
|
||||
$this->signalRegistry->register($signal, function ($signal, $hasNext) use ($event) {
|
||||
$this->dispatcher->dispatch($event, ConsoleEvents::SIGNAL);
|
||||
|
||||
// No more handlers, we try to simulate PHP default behavior
|
||||
if (!$hasNext) {
|
||||
if (!\in_array($signal, [\SIGUSR1, \SIGUSR2], true)) {
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($command->getSubscribedSignals() as $signal) {
|
||||
$this->signalRegistry->register($signal, [$command, 'handleSignal']);
|
||||
}
|
||||
}
|
||||
|
||||
if (null === $this->dispatcher) {
|
||||
return $command->run($input, $output);
|
||||
}
|
||||
@@ -970,13 +1036,11 @@ class Application implements ResetInterface
|
||||
{
|
||||
return new InputDefinition([
|
||||
new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
|
||||
|
||||
new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message'),
|
||||
new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display help for the given command. When no command is given display help for the <info>'.$this->defaultCommand.'</info> command'),
|
||||
new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'),
|
||||
new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug'),
|
||||
new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this application version'),
|
||||
new InputOption('--ansi', '', InputOption::VALUE_NONE, 'Force ANSI output'),
|
||||
new InputOption('--no-ansi', '', InputOption::VALUE_NONE, 'Disable ANSI output'),
|
||||
new InputOption('--ansi', '', InputOption::VALUE_NEGATABLE, 'Force (or disable --no-ansi) ANSI output', false),
|
||||
new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'),
|
||||
]);
|
||||
}
|
||||
@@ -1055,7 +1119,7 @@ class Application implements ResetInterface
|
||||
}
|
||||
|
||||
$lev = levenshtein($subname, $parts[$i]);
|
||||
if ($lev <= \strlen($subname) / 3 || '' !== $subname && false !== strpos($parts[$i], $subname)) {
|
||||
if ($lev <= \strlen($subname) / 3 || '' !== $subname && str_contains($parts[$i], $subname)) {
|
||||
$alternatives[$collectionName] = $exists ? $alternatives[$collectionName] + $lev : $lev;
|
||||
} elseif ($exists) {
|
||||
$alternatives[$collectionName] += $threshold;
|
||||
@@ -1065,13 +1129,13 @@ class Application implements ResetInterface
|
||||
|
||||
foreach ($collection as $item) {
|
||||
$lev = levenshtein($name, $item);
|
||||
if ($lev <= \strlen($name) / 3 || false !== strpos($item, $name)) {
|
||||
if ($lev <= \strlen($name) / 3 || str_contains($item, $name)) {
|
||||
$alternatives[$item] = isset($alternatives[$item]) ? $alternatives[$item] - $lev : $lev;
|
||||
}
|
||||
}
|
||||
|
||||
$alternatives = array_filter($alternatives, function ($lev) use ($threshold) { return $lev < 2 * $threshold; });
|
||||
ksort($alternatives, SORT_NATURAL | SORT_FLAG_CASE);
|
||||
ksort($alternatives, \SORT_NATURAL | \SORT_FLAG_CASE);
|
||||
|
||||
return array_keys($alternatives);
|
||||
}
|
||||
@@ -1083,7 +1147,7 @@ class Application implements ResetInterface
|
||||
*/
|
||||
public function setDefaultCommand(string $commandName, bool $isSingleCommand = false)
|
||||
{
|
||||
$this->defaultCommand = $commandName;
|
||||
$this->defaultCommand = explode('|', ltrim($commandName, '|'))[0];
|
||||
|
||||
if ($isSingleCommand) {
|
||||
// Ensure the command exist
|
||||
|
||||
Reference in New Issue
Block a user