| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266 | <?php/* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */namespace Symfony\Component\Console;use Symfony\Component\Console\Command\Command;use Symfony\Component\Console\Command\HelpCommand;use Symfony\Component\Console\Command\ListCommand;use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;use Symfony\Component\Console\Event\ConsoleCommandEvent;use Symfony\Component\Console\Event\ConsoleErrorEvent;use Symfony\Component\Console\Event\ConsoleExceptionEvent;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\Formatter\OutputFormatter;use Symfony\Component\Console\Helper\DebugFormatterHelper;use Symfony\Component\Console\Helper\FormatterHelper;use Symfony\Component\Console\Helper\Helper;use Symfony\Component\Console\Helper\HelperSet;use Symfony\Component\Console\Helper\ProcessHelper;use Symfony\Component\Console\Helper\QuestionHelper;use Symfony\Component\Console\Input\ArgvInput;use Symfony\Component\Console\Input\ArrayInput;use Symfony\Component\Console\Input\InputArgument;use Symfony\Component\Console\Input\InputAwareInterface;use Symfony\Component\Console\Input\InputDefinition;use Symfony\Component\Console\Input\InputInterface;use Symfony\Component\Console\Input\InputOption;use Symfony\Component\Console\Input\StreamableInputInterface;use Symfony\Component\Console\Output\ConsoleOutput;use Symfony\Component\Console\Output\ConsoleOutputInterface;use Symfony\Component\Console\Output\OutputInterface;use Symfony\Component\Debug\ErrorHandler;use Symfony\Component\Debug\Exception\FatalThrowableError;use Symfony\Component\EventDispatcher\EventDispatcherInterface;/** * An Application is the container for a collection of commands. * * It is the main entry point of a Console application. * * This class is optimized for a standard CLI environment. * * Usage: * *     $app = new Application('myapp', '1.0 (stable)'); *     $app->add(new SimpleCommand()); *     $app->run(); * * @author Fabien Potencier <fabien@symfony.com> */class Application{    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;    /**     * @param string $name    The name of the application     * @param string $version The version of the application     */    public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')    {        $this->name = $name;        $this->version = $version;        $this->terminal = new Terminal();        $this->defaultCommand = 'list';    }    public function setDispatcher(EventDispatcherInterface $dispatcher)    {        $this->dispatcher = $dispatcher;    }    public function setCommandLoader(CommandLoaderInterface $commandLoader)    {        $this->commandLoader = $commandLoader;    }    /**     * Runs the current application.     *     * @return int 0 if everything went fine, or an error code     *     * @throws \Exception When running fails. Bypass this when {@link setCatchExceptions()}.     */    public function run(InputInterface $input = null, OutputInterface $output = null)    {        putenv('LINES='.$this->terminal->getHeight());        putenv('COLUMNS='.$this->terminal->getWidth());        if (null === $input) {            $input = new ArgvInput();        }        if (null === $output) {            $output = new ConsoleOutput();        }        $renderException = function ($e) use ($output) {            if (!$e instanceof \Exception) {                $e = class_exists(FatalThrowableError::class) ? new FatalThrowableError($e) : new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine());            }            if ($output instanceof ConsoleOutputInterface) {                $this->renderException($e, $output->getErrorOutput());            } else {                $this->renderException($e, $output);            }        };        if ($phpHandler = set_exception_handler($renderException)) {            restore_exception_handler();            if (!\is_array($phpHandler) || !$phpHandler[0] instanceof ErrorHandler) {                $debugHandler = true;            } elseif ($debugHandler = $phpHandler[0]->setExceptionHandler($renderException)) {                $phpHandler[0]->setExceptionHandler($debugHandler);            }        }        if (null !== $this->dispatcher && $this->dispatcher->hasListeners(ConsoleEvents::EXCEPTION)) {            @trigger_error(sprintf('The "ConsoleEvents::EXCEPTION" event is deprecated since Symfony 3.3 and will be removed in 4.0. Listen to the "ConsoleEvents::ERROR" event instead.'), E_USER_DEPRECATED);        }        $this->configureIO($input, $output);        try {            $exitCode = $this->doRun($input, $output);        } catch (\Exception $e) {            if (!$this->catchExceptions) {                throw $e;            }            $renderException($e);            $exitCode = $e->getCode();            if (is_numeric($exitCode)) {                $exitCode = (int) $exitCode;                if (0 === $exitCode) {                    $exitCode = 1;                }            } else {                $exitCode = 1;            }        } finally {            // if the exception handler changed, keep it            // otherwise, unregister $renderException            if (!$phpHandler) {                if (set_exception_handler($renderException) === $renderException) {                    restore_exception_handler();                }                restore_exception_handler();            } elseif (!$debugHandler) {                $finalHandler = $phpHandler[0]->setExceptionHandler(null);                if ($finalHandler !== $renderException) {                    $phpHandler[0]->setExceptionHandler($finalHandler);                }            }        }        if ($this->autoExit) {            if ($exitCode > 255) {                $exitCode = 255;            }            exit($exitCode);        }        return $exitCode;    }    /**     * Runs the current application.     *     * @return int 0 if everything went fine, or an error code     */    public function doRun(InputInterface $input, OutputInterface $output)    {        if (true === $input->hasParameterOption(['--version', '-V'], true)) {            $output->writeln($this->getLongVersion());            return 0;        }        try {            // Makes ArgvInput::getFirstArgument() able to distinguish an option from an argument.            $input->bind($this->getDefinition());        } catch (ExceptionInterface $e) {            // Errors must be ignored, full binding/validation happens later when the command is known.        }        $name = $this->getCommandName($input);        if (true === $input->hasParameterOption(['--help', '-h'], true)) {            if (!$name) {                $name = 'help';                $input = new ArrayInput(['command_name' => $this->defaultCommand]);            } else {                $this->wantHelps = true;            }        }        if (!$name) {            $name = $this->defaultCommand;            $definition = $this->getDefinition();            $definition->setArguments(array_merge(                $definition->getArguments(),                [                    'command' => new InputArgument('command', InputArgument::OPTIONAL, $definition->getArgument('command')->getDescription(), $name),                ]            ));        }        try {            $e = $this->runningCommand = null;            // the command name MUST be the first element of the input            $command = $this->find($name);        } catch (\Exception $e) {        } catch (\Throwable $e) {        }        if (null !== $e) {            if (null !== $this->dispatcher) {                $event = new ConsoleErrorEvent($input, $output, $e);                $this->dispatcher->dispatch(ConsoleEvents::ERROR, $event);                $e = $event->getError();                if (0 === $event->getExitCode()) {                    return 0;                }            }            throw $e;        }        $this->runningCommand = $command;        $exitCode = $this->doRunCommand($command, $input, $output);        $this->runningCommand = null;        return $exitCode;    }    public function setHelperSet(HelperSet $helperSet)    {        $this->helperSet = $helperSet;    }    /**     * Get the helper set associated with the command.     *     * @return HelperSet The HelperSet instance associated with this command     */    public function getHelperSet()    {        if (!$this->helperSet) {            $this->helperSet = $this->getDefaultHelperSet();        }        return $this->helperSet;    }    public function setDefinition(InputDefinition $definition)    {        $this->definition = $definition;    }    /**     * Gets the InputDefinition related to this Application.     *     * @return InputDefinition The InputDefinition instance     */    public function getDefinition()    {        if (!$this->definition) {            $this->definition = $this->getDefaultInputDefinition();        }        if ($this->singleCommand) {            $inputDefinition = $this->definition;            $inputDefinition->setArguments();            return $inputDefinition;        }        return $this->definition;    }    /**     * Gets the help message.     *     * @return string A help message     */    public function getHelp()    {        return $this->getLongVersion();    }    /**     * Gets whether to catch exceptions or not during commands execution.     *     * @return bool Whether to catch exceptions or not during commands execution     */    public function areExceptionsCaught()    {        return $this->catchExceptions;    }    /**     * Sets whether to catch exceptions or not during commands execution.     *     * @param bool $boolean Whether to catch exceptions or not during commands execution     */    public function setCatchExceptions($boolean)    {        $this->catchExceptions = (bool) $boolean;    }    /**     * Gets whether to automatically exit after a command execution or not.     *     * @return bool Whether to automatically exit after a command execution or not     */    public function isAutoExitEnabled()    {        return $this->autoExit;    }    /**     * Sets whether to automatically exit after a command execution or not.     *     * @param bool $boolean Whether to automatically exit after a command execution or not     */    public function setAutoExit($boolean)    {        $this->autoExit = (bool) $boolean;    }    /**     * Gets the name of the application.     *     * @return string The application name     */    public function getName()    {        return $this->name;    }    /**     * Sets the application name.     *     * @param string $name The application name     */    public function setName($name)    {        $this->name = $name;    }    /**     * Gets the application version.     *     * @return string The application version     */    public function getVersion()    {        return $this->version;    }    /**     * Sets the application version.     *     * @param string $version The application version     */    public function setVersion($version)    {        $this->version = $version;    }    /**     * Returns the long version of the application.     *     * @return string The long application version     */    public function getLongVersion()    {        if ('UNKNOWN' !== $this->getName()) {            if ('UNKNOWN' !== $this->getVersion()) {                return sprintf('%s <info>%s</info>', $this->getName(), $this->getVersion());            }            return $this->getName();        }        return 'Console Tool';    }    /**     * Registers a new command.     *     * @param string $name The command name     *     * @return Command The newly created command     */    public function register($name)    {        return $this->add(new Command($name));    }    /**     * Adds an array of command objects.     *     * If a Command is not enabled it will not be added.     *     * @param Command[] $commands An array of commands     */    public function addCommands(array $commands)    {        foreach ($commands as $command) {            $this->add($command);        }    }    /**     * Adds a command object.     *     * If a command with the same name already exists, it will be overridden.     * If the command is not enabled it will not be added.     *     * @return Command|null The registered command if enabled or null     */    public function add(Command $command)    {        $this->init();        $command->setApplication($this);        if (!$command->isEnabled()) {            $command->setApplication(null);            return null;        }        // 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)));        }        $this->commands[$command->getName()] = $command;        foreach ($command->getAliases() as $alias) {            $this->commands[$alias] = $command;        }        return $command;    }    /**     * Returns a registered command by name or alias.     *     * @param string $name The command name or alias     *     * @return Command A Command object     *     * @throws CommandNotFoundException When given command name does not exist     */    public function get($name)    {        $this->init();        if (!$this->has($name)) {            throw new CommandNotFoundException(sprintf('The command "%s" does not exist.', $name));        }        $command = $this->commands[$name];        if ($this->wantHelps) {            $this->wantHelps = false;            $helpCommand = $this->get('help');            $helpCommand->setCommand($command);            return $helpCommand;        }        return $command;    }    /**     * Returns true if the command exists, false otherwise.     *     * @param string $name The command name or alias     *     * @return bool true if the command exists, false otherwise     */    public function has($name)    {        $this->init();        return isset($this->commands[$name]) || ($this->commandLoader && $this->commandLoader->has($name) && $this->add($this->commandLoader->get($name)));    }    /**     * Returns an array of all unique namespaces used by currently registered commands.     *     * It does not return the global namespace which always exists.     *     * @return string[] An array of namespaces     */    public function getNamespaces()    {        $namespaces = [];        foreach ($this->all() as $command) {            if ($command->isHidden()) {                continue;            }            $namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));            foreach ($command->getAliases() as $alias) {                $namespaces = array_merge($namespaces, $this->extractAllNamespaces($alias));            }        }        return array_values(array_unique(array_filter($namespaces)));    }    /**     * Finds a registered namespace by a name or an abbreviation.     *     * @param string $namespace A namespace or abbreviation to search for     *     * @return string A registered namespace     *     * @throws CommandNotFoundException When namespace is incorrect or ambiguous     */    public function findNamespace($namespace)    {        $allNamespaces = $this->getNamespaces();        $expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $namespace);        $namespaces = preg_grep('{^'.$expr.'}', $allNamespaces);        if (empty($namespaces)) {            $message = sprintf('There are no commands defined in the "%s" namespace.', $namespace);            if ($alternatives = $this->findAlternatives($namespace, $allNamespaces)) {                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 CommandNotFoundException($message, $alternatives);        }        $exact = \in_array($namespace, $namespaces, true);        if (\count($namespaces) > 1 && !$exact) {            throw new CommandNotFoundException(sprintf("The namespace \"%s\" is ambiguous.\nDid you mean one of these?\n%s", $namespace, $this->getAbbreviationSuggestions(array_values($namespaces))), array_values($namespaces));        }        return $exact ? $namespace : reset($namespaces);    }    /**     * 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.     *     * @param string $name A command name or a command alias     *     * @return Command A Command instance     *     * @throws CommandNotFoundException When command name is incorrect or ambiguous     */    public function find($name)    {        $this->init();        $aliases = [];        foreach ($this->commands as $command) {            foreach ($command->getAliases() as $alias) {                if (!$this->has($alias)) {                    $this->commands[$alias] = $command;                }            }        }        $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);        $commands = preg_grep('{^'.$expr.'}', $allCommands);        if (empty($commands)) {            $commands = preg_grep('{^'.$expr.'}i', $allCommands);        }        // if no commands matched or we just matched namespaces        if (empty($commands) || \count(preg_grep('{^'.$expr.'$}i', $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)) {                // remove hidden commands                $alternatives = array_filter($alternatives, function ($name) {                    return !$this->get($name)->isHidden();                });                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 CommandNotFoundException($message, array_values($alternatives));        }        // filter out aliases for commands which are already on the list        if (\count($commands) > 1) {            $commandList = $this->commandLoader ? array_merge(array_flip($this->commandLoader->getNames()), $this->commands) : $this->commands;            $commands = array_unique(array_filter($commands, function ($nameOrAlias) use ($commandList, $commands, &$aliases) {                $commandName = $commandList[$nameOrAlias] instanceof Command ? $commandList[$nameOrAlias]->getName() : $nameOrAlias;                $aliases[$nameOrAlias] = $commandName;                return $commandName === $nameOrAlias || !\in_array($commandName, $commands);            }));        }        $exact = \in_array($name, $commands, true) || isset($aliases[$name]);        if (\count($commands) > 1 && !$exact) {            $usableWidth = $this->terminal->getWidth() - 10;            $abbrevs = array_values($commands);            $maxLen = 0;            foreach ($abbrevs as $abbrev) {                $maxLen = max(Helper::strlen($abbrev), $maxLen);            }            $abbrevs = array_map(function ($cmd) use ($commandList, $usableWidth, $maxLen) {                if (!$commandList[$cmd] instanceof Command) {                    $commandList[$cmd] = $this->commandLoader->get($cmd);                }                if ($commandList[$cmd]->isHidden()) {                    return false;                }                $abbrev = str_pad($cmd, $maxLen, ' ').' '.$commandList[$cmd]->getDescription();                return Helper::strlen($abbrev) > $usableWidth ? Helper::substr($abbrev, 0, $usableWidth - 3).'...' : $abbrev;            }, array_values($commands));            $suggestions = $this->getAbbreviationSuggestions(array_filter($abbrevs));            throw new CommandNotFoundException(sprintf("Command \"%s\" is ambiguous.\nDid you mean one of these?\n%s", $name, $suggestions), array_values($commands));        }        return $this->get($exact ? $name : reset($commands));    }    /**     * Gets the commands (registered in the given namespace if provided).     *     * The array keys are the full names and the values the command instances.     *     * @param string $namespace A namespace name     *     * @return Command[] An array of Command instances     */    public function all($namespace = null)    {        $this->init();        if (null === $namespace) {            if (!$this->commandLoader) {                return $this->commands;            }            $commands = $this->commands;            foreach ($this->commandLoader->getNames() as $name) {                if (!isset($commands[$name]) && $this->has($name)) {                    $commands[$name] = $this->get($name);                }            }            return $commands;        }        $commands = [];        foreach ($this->commands as $name => $command) {            if ($namespace === $this->extractNamespace($name, substr_count($namespace, ':') + 1)) {                $commands[$name] = $command;            }        }        if ($this->commandLoader) {            foreach ($this->commandLoader->getNames() as $name) {                if (!isset($commands[$name]) && $namespace === $this->extractNamespace($name, substr_count($namespace, ':') + 1) && $this->has($name)) {                    $commands[$name] = $this->get($name);                }            }        }        return $commands;    }    /**     * Returns an array of possible abbreviations given a set of names.     *     * @param array $names An array of names     *     * @return array An array of abbreviations     */    public static function getAbbreviations($names)    {        $abbrevs = [];        foreach ($names as $name) {            for ($len = \strlen($name); $len > 0; --$len) {                $abbrev = substr($name, 0, $len);                $abbrevs[$abbrev][] = $name;            }        }        return $abbrevs;    }    /**     * Renders a caught exception.     */    public function renderException(\Exception $e, OutputInterface $output)    {        $output->writeln('', OutputInterface::VERBOSITY_QUIET);        $this->doRenderException($e, $output);        if (null !== $this->runningCommand) {            $output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())), OutputInterface::VERBOSITY_QUIET);            $output->writeln('', OutputInterface::VERBOSITY_QUIET);        }    }    protected function doRenderException(\Exception $e, OutputInterface $output)    {        do {            $message = trim($e->getMessage());            if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {                $title = sprintf('  [%s%s]  ', \get_class($e), 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : '');                $len = Helper::strlen($title);            } else {                $len = 0;            }            $width = $this->terminal->getWidth() ? $this->terminal->getWidth() - 1 : PHP_INT_MAX;            // HHVM only accepts 32 bits integer in str_split, even when PHP_INT_MAX is a 64 bit integer: https://github.com/facebook/hhvm/issues/1327            if (\defined('HHVM_VERSION') && $width > 1 << 31) {                $width = 1 << 31;            }            $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;                    $lines[] = [$line, $lineLength];                    $len = max($lineLength, $len);                }            }            $messages = [];            if (!$e instanceof ExceptionInterface || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {                $messages[] = sprintf('<comment>%s</comment>', OutputFormatter::escape(sprintf('In %s line %s:', basename($e->getFile()) ?: 'n/a', $e->getLine() ?: 'n/a')));            }            $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))));            }            foreach ($lines as $line) {                $messages[] = sprintf('<error>  %s  %s</error>', OutputFormatter::escape($line[0]), str_repeat(' ', $len - $line[1]));            }            $messages[] = $emptyLine;            $messages[] = '';            $output->writeln($messages, OutputInterface::VERBOSITY_QUIET);            if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {                $output->writeln('<comment>Exception trace:</comment>', OutputInterface::VERBOSITY_QUIET);                // exception related properties                $trace = $e->getTrace();                array_unshift($trace, [                    'function' => '',                    'file' => $e->getFile() ?: 'n/a',                    'line' => $e->getLine() ?: 'n/a',                    'args' => [],                ]);                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';                    $output->writeln(sprintf(' %s%s at <info>%s:%s</info>', $class, $function ? $type.$function.'()' : '', $file, $line), OutputInterface::VERBOSITY_QUIET);                }                $output->writeln('', OutputInterface::VERBOSITY_QUIET);            }        } while ($e = $e->getPrevious());    }    /**     * Tries to figure out the terminal width in which this application runs.     *     * @return int|null     *     * @deprecated since version 3.2, to be removed in 4.0. Create a Terminal instance instead.     */    protected function getTerminalWidth()    {        @trigger_error(sprintf('The "%s()" method is deprecated as of 3.2 and will be removed in 4.0. Create a Terminal instance instead.', __METHOD__), E_USER_DEPRECATED);        return $this->terminal->getWidth();    }    /**     * Tries to figure out the terminal height in which this application runs.     *     * @return int|null     *     * @deprecated since version 3.2, to be removed in 4.0. Create a Terminal instance instead.     */    protected function getTerminalHeight()    {        @trigger_error(sprintf('The "%s()" method is deprecated as of 3.2 and will be removed in 4.0. Create a Terminal instance instead.', __METHOD__), E_USER_DEPRECATED);        return $this->terminal->getHeight();    }    /**     * Tries to figure out the terminal dimensions based on the current environment.     *     * @return array Array containing width and height     *     * @deprecated since version 3.2, to be removed in 4.0. Create a Terminal instance instead.     */    public function getTerminalDimensions()    {        @trigger_error(sprintf('The "%s()" method is deprecated as of 3.2 and will be removed in 4.0. Create a Terminal instance instead.', __METHOD__), E_USER_DEPRECATED);        return [$this->terminal->getWidth(), $this->terminal->getHeight()];    }    /**     * Sets terminal dimensions.     *     * Can be useful to force terminal dimensions for functional tests.     *     * @param int $width  The width     * @param int $height The height     *     * @return $this     *     * @deprecated since version 3.2, to be removed in 4.0. Set the COLUMNS and LINES env vars instead.     */    public function setTerminalDimensions($width, $height)    {        @trigger_error(sprintf('The "%s()" method is deprecated as of 3.2 and will be removed in 4.0. Set the COLUMNS and LINES env vars instead.', __METHOD__), E_USER_DEPRECATED);        putenv('COLUMNS='.$width);        putenv('LINES='.$height);        return $this;    }    /**     * Configures the input and output instances based on the user arguments and options.     */    protected function configureIO(InputInterface $input, OutputInterface $output)    {        if (true === $input->hasParameterOption(['--ansi'], true)) {            $output->setDecorated(true);        } elseif (true === $input->hasParameterOption(['--no-ansi'], true)) {            $output->setDecorated(false);        }        if (true === $input->hasParameterOption(['--no-interaction', '-n'], true)) {            $input->setInteractive(false);        } elseif (\function_exists('posix_isatty')) {            $inputStream = null;            if ($input instanceof StreamableInputInterface) {                $inputStream = $input->getStream();            }            // This check ensures that calling QuestionHelper::setInputStream() works            // To be removed in 4.0 (in the same time as QuestionHelper::setInputStream)            if (!$inputStream && $this->getHelperSet()->has('question')) {                $inputStream = $this->getHelperSet()->get('question')->getInputStream(false);            }            if (!@posix_isatty($inputStream) && false === getenv('SHELL_INTERACTIVE')) {                $input->setInteractive(false);            }        }        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;        }        if (true === $input->hasParameterOption(['--quiet', '-q'], true)) {            $output->setVerbosity(OutputInterface::VERBOSITY_QUIET);            $shellVerbosity = -1;        } else {            if ($input->hasParameterOption('-vvv', true) || $input->hasParameterOption('--verbose=3', true) || 3 === $input->getParameterOption('--verbose', false, true)) {                $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);                $shellVerbosity = 3;            } elseif ($input->hasParameterOption('-vv', true) || $input->hasParameterOption('--verbose=2', true) || 2 === $input->getParameterOption('--verbose', false, true)) {                $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);                $shellVerbosity = 2;            } elseif ($input->hasParameterOption('-v', true) || $input->hasParameterOption('--verbose=1', true) || $input->hasParameterOption('--verbose', true) || $input->getParameterOption('--verbose', false, true)) {                $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);                $shellVerbosity = 1;            }        }        if (-1 === $shellVerbosity) {            $input->setInteractive(false);        }        putenv('SHELL_VERBOSITY='.$shellVerbosity);        $_ENV['SHELL_VERBOSITY'] = $shellVerbosity;        $_SERVER['SHELL_VERBOSITY'] = $shellVerbosity;    }    /**     * Runs the current command.     *     * If an event dispatcher has been attached to the application,     * events are also dispatched during the life-cycle of the command.     *     * @return int 0 if everything went fine, or an error code     */    protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)    {        foreach ($command->getHelperSet() as $helper) {            if ($helper instanceof InputAwareInterface) {                $helper->setInput($input);            }        }        if (null === $this->dispatcher) {            return $command->run($input, $output);        }        // bind before the console.command event, so the listeners have access to input options/arguments        try {            $command->mergeApplicationDefinition();            $input->bind($command->getDefinition());        } catch (ExceptionInterface $e) {            // ignore invalid options/arguments for now, to allow the event listeners to customize the InputDefinition        }        $event = new ConsoleCommandEvent($command, $input, $output);        $e = null;        try {            $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);            if ($event->commandShouldRun()) {                $exitCode = $command->run($input, $output);            } else {                $exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;            }        } catch (\Exception $e) {        } catch (\Throwable $e) {        }        if (null !== $e) {            if ($this->dispatcher->hasListeners(ConsoleEvents::EXCEPTION)) {                $x = $e instanceof \Exception ? $e : new FatalThrowableError($e);                $event = new ConsoleExceptionEvent($command, $input, $output, $x, $x->getCode());                $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);                if ($x !== $event->getException()) {                    $e = $event->getException();                }            }            $event = new ConsoleErrorEvent($input, $output, $e, $command);            $this->dispatcher->dispatch(ConsoleEvents::ERROR, $event);            $e = $event->getError();            if (0 === $exitCode = $event->getExitCode()) {                $e = null;            }        }        $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);        $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);        if (null !== $e) {            throw $e;        }        return $event->getExitCode();    }    /**     * Gets the name of the command based on input.     *     * @return string|null     */    protected function getCommandName(InputInterface $input)    {        return $this->singleCommand ? $this->defaultCommand : $input->getFirstArgument();    }    /**     * Gets the default input definition.     *     * @return InputDefinition An InputDefinition instance     */    protected function getDefaultInputDefinition()    {        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('--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('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'),        ]);    }    /**     * Gets the default commands that should always be available.     *     * @return Command[] An array of default Command instances     */    protected function getDefaultCommands()    {        return [new HelpCommand(), new ListCommand()];    }    /**     * Gets the default helper set with the helpers that should always be available.     *     * @return HelperSet A HelperSet instance     */    protected function getDefaultHelperSet()    {        return new HelperSet([            new FormatterHelper(),            new DebugFormatterHelper(),            new ProcessHelper(),            new QuestionHelper(),        ]);    }    /**     * Returns abbreviated suggestions in string format.     *     * @param array $abbrevs Abbreviated suggestions to convert     *     * @return string A formatted string of abbreviated suggestions     */    private function getAbbreviationSuggestions($abbrevs)    {        return '    '.implode("\n    ", $abbrevs);    }    /**     * Returns the namespace part of the command name.     *     * This method is not part of public API and should not be used directly.     *     * @param string $name  The full name of the command     * @param string $limit The maximum number of parts of the namespace     *     * @return string The namespace of the command     */    public function extractNamespace($name, $limit = null)    {        $parts = explode(':', $name, -1);        return implode(':', null === $limit ? $parts : \array_slice($parts, 0, $limit));    }    /**     * Finds alternative of $name among $collection,     * if nothing is found in $collection, try in $abbrevs.     *     * @param string   $name       The string     * @param iterable $collection The collection     *     * @return string[] A sorted array of similar string     */    private function findAlternatives($name, $collection)    {        $threshold = 1e3;        $alternatives = [];        $collectionParts = [];        foreach ($collection as $item) {            $collectionParts[$item] = explode(':', $item);        }        foreach (explode(':', $name) as $i => $subname) {            foreach ($collectionParts as $collectionName => $parts) {                $exists = isset($alternatives[$collectionName]);                if (!isset($parts[$i]) && $exists) {                    $alternatives[$collectionName] += $threshold;                    continue;                } elseif (!isset($parts[$i])) {                    continue;                }                $lev = levenshtein($subname, $parts[$i]);                if ($lev <= \strlen($subname) / 3 || '' !== $subname && false !== strpos($parts[$i], $subname)) {                    $alternatives[$collectionName] = $exists ? $alternatives[$collectionName] + $lev : $lev;                } elseif ($exists) {                    $alternatives[$collectionName] += $threshold;                }            }        }        foreach ($collection as $item) {            $lev = levenshtein($name, $item);            if ($lev <= \strlen($name) / 3 || false !== strpos($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);        return array_keys($alternatives);    }    /**     * Sets the default Command name.     *     * @param string $commandName     The Command name     * @param bool   $isSingleCommand Set to true if there is only one command in this application     *     * @return self     */    public function setDefaultCommand($commandName, $isSingleCommand = false)    {        $this->defaultCommand = $commandName;        if ($isSingleCommand) {            // Ensure the command exist            $this->find($commandName);            $this->singleCommand = true;        }        return $this;    }    /**     * @internal     */    public function isSingleCommand()    {        return $this->singleCommand;    }    private function splitStringByWidth($string, $width)    {        // str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly.        // additionally, array_slice() is not enough as some character has doubled width.        // we need a function to split string not by character count but by string width        if (false === $encoding = mb_detect_encoding($string, null, true)) {            return str_split($string, $width);        }        $utf8String = mb_convert_encoding($string, 'utf8', $encoding);        $lines = [];        $line = '';        foreach (preg_split('//u', $utf8String) as $char) {            // test if $char could be appended to current line            if (mb_strwidth($line.$char, 'utf8') <= $width) {                $line .= $char;                continue;            }            // if not, push current line to array and make new line            $lines[] = str_pad($line, $width);            $line = $char;        }        $lines[] = \count($lines) ? str_pad($line, $width) : $line;        mb_convert_variables($encoding, 'utf8', $lines);        return $lines;    }    /**     * Returns all namespaces of the command name.     *     * @param string $name The full name of the command     *     * @return string[] The namespaces of the command     */    private function extractAllNamespaces($name)    {        // -1 as third argument is needed to skip the command short name when exploding        $parts = explode(':', $name, -1);        $namespaces = [];        foreach ($parts as $part) {            if (\count($namespaces)) {                $namespaces[] = end($namespaces).':'.$part;            } else {                $namespaces[] = $part;            }        }        return $namespaces;    }    private function init()    {        if ($this->initialized) {            return;        }        $this->initialized = true;        foreach ($this->getDefaultCommands() as $command) {            $this->add($command);        }    }}
 |