From 72cb77a8ee315efc9ab79ea7d238b899953cc617 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Sat, 8 Jun 2019 12:20:20 +0200 Subject: [PATCH] Read environment variable from superglobals The Dotenv component has recently been switched to using superglobals instead of putenv(). Let us support both and give priority to superglobals. Closes #31857 --- .../PhpUnit/DeprecationErrorHandler.php | 23 +++++++++++++++++- .../DeprecationErrorHandler/disabled.phpt | 4 ++-- .../disabled_from_env_superglobal.phpt | 24 +++++++++++++++++++ .../disabled_from_putenv.phpt | 24 +++++++++++++++++++ src/Symfony/Bridge/PhpUnit/bootstrap.php | 14 +++++++++-- 5 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/disabled_from_env_superglobal.phpt create mode 100644 src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/disabled_from_putenv.phpt diff --git a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php index c79aecfc3d651..add8ed792b1bd 100644 --- a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php +++ b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php @@ -90,6 +90,11 @@ public static function register($mode = 0) } } + public static function registerAndConfigureFromEnvironment() + { + self::register(self::getModeFromEnvironment()); + } + public static function collectDeprecations($outputFile) { $deprecations = []; @@ -209,12 +214,28 @@ public function shutdown() }); } + /** + * @return mixed string|false + */ + private static function getModeFromEnvironment() + { + if (isset($_SERVER['SYMFONY_DEPRECATIONS_HELPER'])) { + return $_SERVER['SYMFONY_DEPRECATIONS_HELPER']; + } + + if (isset($_ENV['SYMFONY_DEPRECATIONS_HELPER'])) { + return $_ENV['SYMFONY_DEPRECATIONS_HELPER']; + } + + return getenv('SYMFONY_DEPRECATIONS_HELPER'); + } + private function getConfiguration() { if (null !== $this->configuration) { return $this->configuration; } - $mode = getenv('SYMFONY_DEPRECATIONS_HELPER'); + $mode = self::getModeFromEnvironment(); if ('strict' === $mode) { return $this->configuration = Configuration::inStrictMode(); } diff --git a/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/disabled.phpt b/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/disabled.phpt index 0115bbd24259a..a55e88d1a8ed5 100644 --- a/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/disabled.phpt +++ b/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/disabled.phpt @@ -1,9 +1,9 @@ --TEST-- -Test DeprecationErrorHandler in weak mode +Test DeprecationErrorHandler in disabled mode --FILE-- +--EXPECTF-- +00 diff --git a/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/disabled_from_putenv.phpt b/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/disabled_from_putenv.phpt new file mode 100644 index 0000000000000..73a67241a495f --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/disabled_from_putenv.phpt @@ -0,0 +1,24 @@ +--TEST-- +Test DeprecationErrorHandler in disabled mode (via putenv) +--FILE-- + +--EXPECTF-- +00 diff --git a/src/Symfony/Bridge/PhpUnit/bootstrap.php b/src/Symfony/Bridge/PhpUnit/bootstrap.php index 5de946789155d..15b4582294deb 100644 --- a/src/Symfony/Bridge/PhpUnit/bootstrap.php +++ b/src/Symfony/Bridge/PhpUnit/bootstrap.php @@ -35,6 +35,16 @@ } } -if ('disabled' !== getenv('SYMFONY_DEPRECATIONS_HELPER')) { - DeprecationErrorHandler::register(getenv('SYMFONY_DEPRECATIONS_HELPER')); +if (isset($_SERVER['SYMFONY_DEPRECATIONS_HELPER']) && 'disabled' === $_SERVER['SYMFONY_DEPRECATIONS_HELPER']) { + return; +} + +if (isset($_ENV['SYMFONY_DEPRECATIONS_HELPER']) && 'disabled' === $_ENV['SYMFONY_DEPRECATIONS_HELPER']) { + return; } + +if ('disabled' === getenv('SYMFONY_DEPRECATIONS_HELPER')) { + return; +} + +DeprecationErrorHandler::registerAndConfigureFromEnvironment();