diff --git a/src/Internal/Util/Config.php b/src/Internal/Util/Config.php index a80dd9f1..6a9cae56 100644 --- a/src/Internal/Util/Config.php +++ b/src/Internal/Util/Config.php @@ -131,22 +131,34 @@ private static function getEnvValue(string $key): ?string public static function isEnabled(): bool { - $envDisable = self::getEnvValue('TYPEPHP_DISABLE'); - if ($envDisable !== null && trim($envDisable) !== '') { - if (filter_var($envDisable, FILTER_VALIDATE_BOOLEAN)) { - return false; + // Hot path: called by every runtime check. The overrides are re-probed on each call + // (Dotenv may populate them after boot), but only resolved when one is actually present. + if ( + isset($_ENV['TYPEPHP_DISABLE']) + || isset($_SERVER['TYPEPHP_DISABLE']) + || isset($_ENV['TYPEPHP_ENABLED']) + || isset($_SERVER['TYPEPHP_ENABLED']) + || getenv('TYPEPHP_DISABLE') !== false + || getenv('TYPEPHP_ENABLED') !== false + || \defined('TYPEPHP_DISABLE') + ) { + $envDisable = self::getEnvValue('TYPEPHP_DISABLE'); + if ($envDisable !== null && trim($envDisable) !== '') { + if (filter_var($envDisable, FILTER_VALIDATE_BOOLEAN)) { + return false; + } } - } - $envEnabled = self::getEnvValue('TYPEPHP_ENABLED'); - if ($envEnabled !== null && trim($envEnabled) !== '') { - if (! filter_var($envEnabled, FILTER_VALIDATE_BOOLEAN)) { - return false; + $envEnabled = self::getEnvValue('TYPEPHP_ENABLED'); + if ($envEnabled !== null && trim($envEnabled) !== '') { + if (! filter_var($envEnabled, FILTER_VALIDATE_BOOLEAN)) { + return false; + } } - } - if (\defined('TYPEPHP_DISABLE') && (bool) TYPEPHP_DISABLE) { - return false; + if (\defined('TYPEPHP_DISABLE') && (bool) TYPEPHP_DISABLE) { + return false; + } } if (self::$cachedConfig === null) { diff --git a/tests/Internal/Util/ConfigTest.php b/tests/Internal/Util/ConfigTest.php index 5241ad04..eda0d5bb 100644 --- a/tests/Internal/Util/ConfigTest.php +++ b/tests/Internal/Util/ConfigTest.php @@ -313,6 +313,22 @@ function cleanTypePhpEnvVariables(): void unset($_SERVER['TYPEPHP_ENABLED']); }); + test('isEnabled dynamically reflects TYPEPHP_DISABLE and TYPEPHP_ENABLED set through putenv()', function () { + expect(Config::isEnabled())->toBeTrue(); + + putenv('TYPEPHP_DISABLE=1'); + expect(Config::isEnabled())->toBeFalse(); + + putenv('TYPEPHP_DISABLE'); + expect(Config::isEnabled())->toBeTrue(); + + putenv('TYPEPHP_ENABLED=false'); + expect(Config::isEnabled())->toBeFalse(); + + putenv('TYPEPHP_ENABLED'); + expect(Config::isEnabled())->toBeTrue(); + }); + test('isAutoBootEnabled resolves values from $_ENV and $_SERVER', function () { $_ENV['TYPEPHP_AUTO_BOOT'] = 'false'; expect(Config::isAutoBootEnabled())->toBeFalse();