diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 8014b68..d8630fd 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -180,18 +180,7 @@ public function __construct( string $filename = self::SERVICE_CONFIG_FILE, ?string $pathBundlesConfig = null ) { - // Buggy local fix. - $_ENV['DEBUG'] = env('DEBUG', false); - if (array_key_exists('APP_DEBUG', $_ENV) && $_ENV['APP_DEBUG'] !== null) { - $_ENV['DEBUG'] = (bool)$_ENV['APP_DEBUG']; - } - - $this->environment = $_ENV['DEBUG'] ? 'dev' : 'prod'; - if (array_key_exists('APP_ENV', $_ENV) && $_ENV['APP_ENV'] !== null) { - $this->environment = $_ENV['APP_ENV']; - } - - $this->debug = (bool)$_ENV['DEBUG']; + $this->setupEnv(); $this->errorHandler = new ErrorScreen(new CMain()); $this->filesystem = new Filesystem(); @@ -300,6 +289,31 @@ public function shutdown() : void static::$containerBuilder = null; } + /** + * Манипуляции с переменными окружения. + * + * @return void + */ + private function setupEnv() : void + { + $_ENV['DEBUG'] = $_ENV['DEBUG'] ?? false; + if ($_ENV['DEBUG'] !== false) { + if (is_string($_ENV['DEBUG'])) { + $_ENV['DEBUG'] = $_ENV['DEBUG'] === 'true' || $_ENV['DEBUG'] === '1'; + } else { + $_ENV['DEBUG'] = (bool)$_ENV['DEBUG']; + } + } + + $this->environment = $_ENV['DEBUG'] ? 'dev' : 'prod'; + + if (array_key_exists('APP_ENV', $_ENV) && $_ENV['APP_ENV'] !== null) { + $this->environment = $_ENV['APP_ENV']; + } + + $this->debug = $_ENV['DEBUG']; + } + /** * Boot. * diff --git a/tests/Cases/ServiceProviderTest.php b/tests/Cases/ServiceProviderTest.php index e246db0..c207d97 100644 --- a/tests/Cases/ServiceProviderTest.php +++ b/tests/Cases/ServiceProviderTest.php @@ -288,6 +288,47 @@ public function testGetPathCacheDirectory() : void ); } + /** + * Манипуляции с $_ENV. + * + * @param mixed $envDebug Значение $_ENV['DEBUG']. + * @param bool $expected Ожидаемое. + * + * @return void + * + * @throws Exception + * @dataProvider dataProviderEnvDebugValues + */ + public function testSetEnv($envDebug, bool $expected) : void + { + $backup = $_ENV; + $_ENV['DEBUG'] = $envDebug; + + $this->obTestObject = new ServiceProvider($this->pathYamlConfig); + + $this->assertSame($_ENV['DEBUG'], $expected); + + $_ENV = $backup; + } + + /** + * @return array + */ + public function dataProviderEnvDebugValues() : array + { + return [ + '0-number' => [0, false], + '1-number' => [1, true], + '0-string' => ['0', false], + '1-string' => ['1', true], + 'true-string' => ['true', true], + 'false-string' => ['false', false], + 'null-string' => [null, false], + 'true' => [true, true], + 'false' => [false, false], + ]; + } + /** * Рекурсивно удалить папку со всем файлами и папками. *