From 218f8b3af028e3c4724adf5aadfc7ee415b38e81 Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Fri, 14 Apr 2023 12:36:01 +0200 Subject: [PATCH] Add safe application --- src/CliProgram/SafeApplication.php | 75 ++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/CliProgram/SafeApplication.php diff --git a/src/CliProgram/SafeApplication.php b/src/CliProgram/SafeApplication.php new file mode 100644 index 0000000..a7b9151 --- /dev/null +++ b/src/CliProgram/SafeApplication.php @@ -0,0 +1,75 @@ +processRuntimeException($exception, $input, $output); + } + } + + /** + * The exit code used when command failed with an exception + * + * @return int The exit code used when command failed with an exception + */ + public function getExceptionFailureExitCode (): int { + return $this->exceptionFailureExitCode; + } + /** + * Set the exit code used when command failed with an exception + * + * @param int $exceptionFailureExitCode The new exit code used when command failed with an exception + * + * @return $this + */ + public function setExceptionFailureExitCode (int $exceptionFailureExitCode): self { + $this->exceptionFailureExitCode = $exceptionFailureExitCode; + return $this; + } + /** + * Process runtime exception + * + * Default behavior: write exception message to $output (error) and return {@see SafeApplication::$exceptionFailureExitCode} + * + * @param Throwable $exception The exception + * @param InputInterface $input The input (i.e. argv) + * @param OutputInterface $output The output (i.e. STDOUT and STDERR) + * + * @return int The exit code + * + * @noinspection PhpUnusedParameterInspection + */ + protected function processRuntimeException (Throwable $exception, InputInterface $input, OutputInterface $output): int { + $output->writeln('' . $exception->getMessage() . ''); + return $this->exceptionFailureExitCode; + } +} \ No newline at end of file