From 54d7fc2c0782384021c34f1f49e728802a7bc539 Mon Sep 17 00:00:00 2001 From: Bogdans Ozerkins Date: Fri, 30 Sep 2016 21:02:55 +0300 Subject: [PATCH 1/5] added catching of php7 fatal exceptions - added new private method to application, which would process the exception and return the exit code / throw it - added another catch into command run method, which would ensure that under PHP7 all exceptions are being catched as well --- src/Symfony/Component/Console/Application.php | 55 ++++++++++++------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 41991ef76b95b..14321deac5d1c 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -123,25 +123,9 @@ public function run(InputInterface $input = null, OutputInterface $output = null try { $exitCode = $this->doRun($input, $output); } catch (\Exception $e) { - if (!$this->catchExceptions) { - throw $e; - } - - if ($output instanceof ConsoleOutputInterface) { - $this->renderException($e, $output->getErrorOutput()); - } else { - $this->renderException($e, $output); - } - - $exitCode = $e->getCode(); - if (is_numeric($exitCode)) { - $exitCode = (int) $exitCode; - if (0 === $exitCode) { - $exitCode = 1; - } - } else { - $exitCode = 1; - } + $exitCode = $this->handleCommandRunException($e, $output); + } catch (\Throwable $e) { + $exitCode = $this->handleCommandRunException($e, $output); } if ($this->autoExit) { @@ -155,6 +139,39 @@ public function run(InputInterface $input = null, OutputInterface $output = null return $exitCode; } + /** + * Handler command run exception and provides with the exit code in return + * + * @param \Exception|\Throwable $e + * @param OutputInterface $output + * @return int|mixed + * @throws \Throwable + */ + private function handleCommandRunException($e, OutputInterface $output) + { + if (!$this->catchExceptions) { + throw $e; + } + + if ($output instanceof ConsoleOutputInterface) { + $this->renderException($e, $output->getErrorOutput()); + } else { + $this->renderException($e, $output); + } + + $exitCode = $e->getCode(); + if (is_numeric($exitCode)) { + $exitCode = (int) $exitCode; + if (0 === $exitCode) { + $exitCode = 1; + } + } else { + $exitCode = 1; + } + + return $exitCode; + } + /** * Runs the current application. * From 999d7bc62cfbeb85fbae9e679f4c17b7007c8cc4 Mon Sep 17 00:00:00 2001 From: Bogdans Ozerkins Date: Sat, 1 Oct 2016 15:18:12 +0300 Subject: [PATCH 2/5] added catching throwable exceptions when running console application - implemented compatibilit with PHP7 fatal errors - using FatalThrowableError for BC with PHP5 --- src/Symfony/Component/Console/Application.php | 63 ++++++++----------- 1 file changed, 27 insertions(+), 36 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 14321deac5d1c..2ac68d2cb9af0 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -106,7 +106,7 @@ public function setDispatcher(EventDispatcherInterface $dispatcher) * * @return int 0 if everything went fine, or an error code * - * @throws \Exception When doRun returns Exception + * @throws \Exception When doRun returns Exception or Throwable */ public function run(InputInterface $input = null, OutputInterface $output = null) { @@ -120,53 +120,44 @@ public function run(InputInterface $input = null, OutputInterface $output = null $this->configureIO($input, $output); + $exception = null; + $exitCode = null; try { $exitCode = $this->doRun($input, $output); } catch (\Exception $e) { - $exitCode = $this->handleCommandRunException($e, $output); + $exception = $e; } catch (\Throwable $e) { - $exitCode = $this->handleCommandRunException($e, $output); + $exception = new FatalThrowableError($e);; } - if ($this->autoExit) { - if ($exitCode > 255) { - $exitCode = 255; + if ($exception) { + if (!$this->catchExceptions) { + throw $exception; } - exit($exitCode); - } - - return $exitCode; - } - - /** - * Handler command run exception and provides with the exit code in return - * - * @param \Exception|\Throwable $e - * @param OutputInterface $output - * @return int|mixed - * @throws \Throwable - */ - private function handleCommandRunException($e, OutputInterface $output) - { - if (!$this->catchExceptions) { - throw $e; - } + if ($output instanceof ConsoleOutputInterface) { + $this->renderException($exception, $output->getErrorOutput()); + } else { + $this->renderException($exception, $output); + } - if ($output instanceof ConsoleOutputInterface) { - $this->renderException($e, $output->getErrorOutput()); - } else { - $this->renderException($e, $output); + $exitCode = $exception->getCode(); + if (is_numeric($exitCode)) { + $exitCode = (int) $exitCode; + if (0 === $exitCode) { + $exitCode = 1; + } + } else { + $exitCode = 1; + } } - $exitCode = $e->getCode(); - if (is_numeric($exitCode)) { - $exitCode = (int) $exitCode; - if (0 === $exitCode) { - $exitCode = 1; + if ($this->autoExit) { + if ($exitCode > 255) { + $exitCode = 255; } - } else { - $exitCode = 1; + + exit($exitCode); } return $exitCode; From b3d88abb2aaca7156b190b6502c91443321f4307 Mon Sep 17 00:00:00 2001 From: Bogdans Ozerkins Date: Sat, 1 Oct 2016 15:19:22 +0300 Subject: [PATCH 3/5] minor code tweak --- src/Symfony/Component/Console/Application.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 2ac68d2cb9af0..ae0a7433afd0d 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -127,7 +127,7 @@ public function run(InputInterface $input = null, OutputInterface $output = null } catch (\Exception $e) { $exception = $e; } catch (\Throwable $e) { - $exception = new FatalThrowableError($e);; + $exception = new FatalThrowableError($e); } if ($exception) { From 97dc736f9aeeb945f1ad8d5a4617886db1771ff9 Mon Sep 17 00:00:00 2001 From: Bogdans Ozerkins Date: Mon, 3 Oct 2016 22:38:20 +0300 Subject: [PATCH 4/5] rethrowing original exception extend of wrapper over Error exception --- src/Symfony/Component/Console/Application.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index ae0a7433afd0d..bd7ebca3ff00a 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -106,7 +106,7 @@ public function setDispatcher(EventDispatcherInterface $dispatcher) * * @return int 0 if everything went fine, or an error code * - * @throws \Exception When doRun returns Exception or Throwable + * @throws \Exception When doRun returns Exception */ public function run(InputInterface $input = null, OutputInterface $output = null) { @@ -120,28 +120,24 @@ public function run(InputInterface $input = null, OutputInterface $output = null $this->configureIO($input, $output); - $exception = null; - $exitCode = null; try { $exitCode = $this->doRun($input, $output); } catch (\Exception $e) { - $exception = $e; } catch (\Throwable $e) { - $exception = new FatalThrowableError($e); } - if ($exception) { + if ($e) { if (!$this->catchExceptions) { - throw $exception; + throw $e; } if ($output instanceof ConsoleOutputInterface) { - $this->renderException($exception, $output->getErrorOutput()); + $this->renderException($e, $output->getErrorOutput()); } else { - $this->renderException($exception, $output); + $this->renderException($e, $output); } - $exitCode = $exception->getCode(); + $exitCode = $e->getCode(); if (is_numeric($exitCode)) { $exitCode = (int) $exitCode; if (0 === $exitCode) { From ebed4757bbaa45bfa5e4b68257c9bbacf3406288 Mon Sep 17 00:00:00 2001 From: Bogdans Ozerkins Date: Tue, 4 Oct 2016 11:43:07 +0300 Subject: [PATCH 5/5] check if exception variable isset --- src/Symfony/Component/Console/Application.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index bd7ebca3ff00a..f93fc917a63d0 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -126,7 +126,7 @@ public function run(InputInterface $input = null, OutputInterface $output = null } catch (\Throwable $e) { } - if ($e) { + if (isset($e)) { if (!$this->catchExceptions) { throw $e; }