src/Viserio/Component/Console/Application.php
public function run(InputInterface $input = null, OutputInterface $output = null)
    {
        \putenv('LINES=' . $this->terminal->getHeight());
        \putenv('COLUMNS=' . $this->terminal->getWidth());

        if ($input === null) {
            $input = new ArgvInput();
        }

        if ($output === null) {
            $output = new ConsoleOutput();
        }

        $renderException = function (Throwable $e) use ($output): void {
            if (! $e instanceof Exception) {
                $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);
            }
        };

        $debugHandler = false;

        /** @var (callable|array<object,string>)[] $phpHandler */
        $phpHandler = \set_exception_handler($renderException);

        if (\is_callable($phpHandler)) {
            \restore_exception_handler();

            if (! \is_array($phpHandler) || ! $phpHandler[0] instanceof ErrorHandler) {
                $debugHandler = true;
            } elseif ($debugHandler = $phpHandler[0]->setExceptionHandler($renderException)) {
                $phpHandler[0]->setExceptionHandler($debugHandler);
            }
        }

        $this->configureIO($input, $output);

        try {
            $exitCode = $this->doRun($input, $output);
        } catch (Throwable $exception) {
            $exitCode = $exception->getCode();

            if ($this->eventManager !== null) {
                $this->eventManager->trigger($event = new ConsoleErrorEvent($this->runningCommand, $input, $output, $exception));

                $exitCode = $event->getExitCode();

                $this->eventManager->trigger(new ConsoleTerminateEvent($this->runningCommand, $input, $output, $exitCode));

                if ($exitCode === 0) {
                    return 0;
                }

                $exception = $event->getError();
            }

            if (! $this->areExceptionsCaught() || ! $exception instanceof Exception) {
                throw $exception;
            }

            $renderException($exception);

            if (\is_numeric($exitCode)) {
                $exitCode = (int) $exitCode;

                if ($exitCode === 0) {
                    $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);
                }
            }

            $this->runningCommand = null;
        }

        if ($this->isAutoExitEnabled()) {
            if ($exitCode > 255) {
                $exitCode = 255;
            }

            exit($exitCode);
        }

        return $exitCode;
    }
public function run(InputInterface $input = null, OutputInterface $output = null)
    {
        \putenv('LINES=' . $this->terminal->getHeight());
        \putenv('COLUMNS=' . $this->terminal->getWidth());

        if ($input === null) {
            $input = new ArgvInput();
        }

        if ($output === null) {
            $output = new ConsoleOutput();
        }

        $renderException = function (Throwable $e) use ($output): void {
            if (! $e instanceof Exception) {
                $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);
            }
        };

        $debugHandler = false;

        /** @var (callable|array<object,string>)[] $phpHandler */
        $phpHandler = \set_exception_handler($renderException);

        if (\is_callable($phpHandler)) {
            \restore_exception_handler();

            if (! \is_array($phpHandler) || ! $phpHandler[0] instanceof ErrorHandler) {
                $debugHandler = true;
            } elseif ($debugHandler = $phpHandler[0]->setExceptionHandler($renderException)) {
                $phpHandler[0]->setExceptionHandler($debugHandler);
            }
        }

        $this->configureIO($input, $output);

        try {
            $exitCode = $this->doRun($input, $output);
        } catch (Throwable $exception) {
            $exitCode = $exception->getCode();

            if ($this->eventManager !== null) {
                $this->eventManager->trigger($event = new ConsoleErrorEvent($this->runningCommand, $input, $output, $exception));

                $exitCode = $event->getExitCode();

                $this->eventManager->trigger(new ConsoleTerminateEvent($this->runningCommand, $input, $output, $exitCode));

                if ($exitCode === 0) {
                    return 0;
                }

                $exception = $event->getError();
            }

            if (! $this->areExceptionsCaught() || ! $exception instanceof Exception) {
                throw $exception;
            }

            $renderException($exception);

            if (\is_numeric($exitCode)) {
                $exitCode = (int) $exitCode;

                if ($exitCode === 0) {
                    $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);
                }
            }

            $this->runningCommand = null;
        }

        if ($this->isAutoExitEnabled()) {
            if ($exitCode > 255) {
                $exitCode = 255;
            }

            exit($exitCode);
        }

        return $exitCode;
    }
{@inheritdoc}