/** * @param Application $application * @param RawArgs $args * @param InputStream|null $inputStream * @param OutputStream|null $outputStream * @param OutputStream|null $errorStream * * @return ConsoleIO */ public function createIO(Application $application, RawArgs $args, InputStream $inputStream = null, OutputStream $outputStream = null, OutputStream $errorStream = null) { $inputStream = $inputStream ?: new StandardInputStream(); $outputStream = $outputStream ?: new StandardOutputStream(); $errorStream = $errorStream ?: new ErrorOutputStream(); $styleSet = $application->getConfig()->getStyleSet(); if ($args->hasToken('--no-ansi')) { $outputFormatter = $errorFormatter = new PlainFormatter($styleSet); } elseif ($args->hasToken('--ansi')) { $outputFormatter = $errorFormatter = new AnsiFormatter($styleSet); } else { $outputFormatter = $outputStream->supportsAnsi() ? new AnsiFormatter($styleSet) : new PlainFormatter($styleSet); $errorFormatter = $errorStream->supportsAnsi() ? new AnsiFormatter($styleSet) : new PlainFormatter($styleSet); } $io = new ConsoleIO(new Input($inputStream), new Output($outputStream, $outputFormatter), new Output($errorStream, $errorFormatter)); if ($args->hasToken('-vvv') || $this->isDebug()) { $io->setVerbosity(IO::DEBUG); } elseif ($args->hasToken('-vv')) { $io->setVerbosity(IO::VERY_VERBOSE); } elseif ($args->hasToken('-v')) { $io->setVerbosity(IO::VERBOSE); } if ($args->hasToken('--quiet') || $args->hasToken('-q')) { $io->setQuiet(true); } if ($args->hasToken('--no-interaction') || $args->hasToken('-n')) { $io->setInteractive(false); } return $io; }
/** * Creates a new console application. * * @param ApplicationConfig|callable $config The application configuration * or a callable that creates the * configuration. */ public function __construct($config) { $this->preliminaryIo = new ConsoleIO(); // Enable trace output for exceptions thrown during boot $this->preliminaryIo->setVerbosity(IO::VERY_VERBOSE); if (is_callable($config)) { try { $config = $config(); } catch (Exception $e) { $trace = new ExceptionTrace($e); $trace->render($this->preliminaryIo); exit($this->exceptionToExitCode($e->getCode())); } } Assert::isInstanceOf($config, 'Webmozart\\Console\\Api\\Config\\ApplicationConfig', 'The $config argument must be an ApplicationConfig or a callable. Got: %s'); try { $dispatcher = $config->getEventDispatcher(); if ($dispatcher && $dispatcher->hasListeners(ConsoleEvents::CONFIG)) { $dispatcher->dispatch(ConsoleEvents::CONFIG, new ConfigEvent($config)); } $this->config = $config; $this->dispatcher = $config->getEventDispatcher(); $this->commands = new CommandCollection(); $this->namedCommands = new CommandCollection(); $this->defaultCommands = new CommandCollection(); $this->globalArgsFormat = new ArgsFormat(array_merge($config->getOptions(), $config->getArguments())); foreach ($config->getCommandConfigs() as $commandConfig) { $this->addCommand($commandConfig); } } catch (Exception $e) { if (!$config->isExceptionCaught()) { throw $e; } // Render the trace to the preliminary IO $trace = new ExceptionTrace($e); $trace->render($this->preliminaryIo); // Ignore isTerminatedAfterRun() setting. This is a fatal error. exit($this->exceptionToExitCode($e->getCode())); } }