merged branch Tobion/console-status-code (PR #8202)

This PR was merged into the 2.1 branch.

Discussion
----------

[Console] fix status and exit code

Fix https://github.com/symfony/symfony/pull/8183#discussion_r4525267
and
http://www.php.net/manual/en/function.exit.php
> Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.

Commits
-------

6b9180a [Console] ensure exit code between 0-254
445b2e3 [Console] fix status code when Exception::getCode returns something like 0.1
This commit is contained in:
Fabien Potencier 2013-06-11 09:13:57 +02:00
commit 82e555263f
1 changed files with 11 additions and 6 deletions

View File

@ -113,17 +113,22 @@ class Application
} else {
$this->renderException($e, $output);
}
$statusCode = $e->getCode();
$statusCode = is_numeric($statusCode) && $statusCode ? (int) $statusCode : 1;
$statusCode = $e->getCode();
if (is_numeric($statusCode)) {
$statusCode = (int) $statusCode;
if (0 === $statusCode) {
$statusCode = 1;
}
} else {
$statusCode = 1;
}
}
if ($this->autoExit) {
if ($statusCode > 255) {
$statusCode = 255;
}
// ensure exit code is between 0-254 (255 is reserved by PHP and should not be used)
// @codeCoverageIgnoreStart
exit($statusCode);
exit(max(0, min(254, $statusCode)));
// @codeCoverageIgnoreEnd
}