Skip to content

Commit

Permalink
Улучшенные манипуляции с $_ENV.
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Jul 23, 2021
1 parent 9701a30 commit 8ecec90
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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.
*
Expand Down
41 changes: 41 additions & 0 deletions tests/Cases/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
}

/**
* Рекурсивно удалить папку со всем файлами и папками.
*
Expand Down

0 comments on commit 8ecec90

Please sign in to comment.