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()));
}
}