-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
305 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Prokl\ServiceProvider; | ||
|
||
use Symfony\Component\Dotenv\Dotenv; | ||
|
||
/** | ||
* Class LoadEnvironment | ||
* Хэлпер: загрузка окружения (dev или prod). | ||
* @package Prokl\ServiceProvider | ||
*/ | ||
class LoadEnvironment | ||
{ | ||
/** | ||
* @var string $pathEnvFile Путь к env файлам. | ||
*/ | ||
private $pathEnvFile; | ||
|
||
/** | ||
* LoadEnvironment constructor. | ||
* | ||
* @param string $pathEnvFile Путь к env файлам. | ||
*/ | ||
public function __construct(string $pathEnvFile) | ||
{ | ||
$this->pathEnvFile = $pathEnvFile; | ||
} | ||
|
||
/** | ||
* Загрузка конфигурации окружения. | ||
* | ||
* @return void | ||
*/ | ||
public function load() : void | ||
{ | ||
/** Путь к конфигурации окружения. .env.prod - продакшен. */ | ||
$pathEnvFile = @file_exists($this->pathEnvFile . '/.env.prod') | ||
? $this->pathEnvFile . '/.env.prod' | ||
: | ||
$_SERVER['DOCUMENT_ROOT'] . '/.env'; | ||
|
||
if (@file_exists($this->pathEnvFile . '/.env.local')) { | ||
$pathEnvFile = $this->pathEnvFile . '/.env.local'; | ||
} | ||
|
||
$dotenv = new Dotenv(); | ||
|
||
$dotenv->loadEnv($pathEnvFile); | ||
} | ||
|
||
/** | ||
* Обработка переменных окружения. | ||
* | ||
* @return void | ||
*/ | ||
public function process() : void | ||
{ | ||
$_ENV['DEBUG'] = $_ENV['DEBUG'] ?? false; | ||
|
||
if (array_key_exists('APP_DEBUG', $_ENV) && $_ENV['APP_DEBUG'] !== null) { | ||
$_ENV['DEBUG'] = (bool)$_ENV['APP_DEBUG']; | ||
} | ||
|
||
$_SERVER = array_merge($_SERVER, $_ENV); | ||
|
||
$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = ($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null) ?: 'dev'; | ||
$_SERVER['APP_DEBUG'] = $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? 'prod' !== $_SERVER['APP_ENV']; | ||
$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = (int)$_SERVER['APP_DEBUG'] | ||
|| filter_var($_SERVER['APP_DEBUG'], FILTER_VALIDATE_BOOLEAN) ? '1' : '0'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
namespace Prokl\ServiceProvider\Tests\Cases; | ||
|
||
use Prokl\ServiceProvider\LoadEnvironment; | ||
use Prokl\TestingTools\Base\BaseTestCase; | ||
|
||
/** | ||
* Class LoadEnvironmentTest | ||
* @package Prokl\ServiceProvider\Tests\Cases | ||
* | ||
* @since 04.07.2021 | ||
*/ | ||
class LoadEnvironmentTest extends BaseTestCase | ||
{ | ||
/** | ||
* @var LoadEnvironment $obTestObject | ||
*/ | ||
protected $obTestObject; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
@unlink($_SERVER['DOCUMENT_ROOT'] . '/.env'); | ||
@unlink($_SERVER['DOCUMENT_ROOT'] . '/.env.prod'); | ||
@unlink($_SERVER['DOCUMENT_ROOT'] . '/.env.local'); | ||
|
||
parent::setUp(); | ||
|
||
$_SERVER['DOCUMENT_ROOT'] = __DIR__ . '/../fixtures/env'; | ||
$this->obTestObject = new LoadEnvironment( | ||
$_SERVER['DOCUMENT_ROOT'] | ||
); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
unset($_ENV); | ||
unset($_SERVER['APP_ENV'], $_SERVER['APP_DEBUG'], $_SERVER['DEBUG']); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testProcess() : void | ||
{ | ||
$this->obTestObject->process(); | ||
|
||
$this->assertFalse($_ENV['DEBUG']); | ||
} | ||
|
||
/** | ||
* load(). Production. | ||
* | ||
* @return void | ||
*/ | ||
public function testLoadProd() : void | ||
{ | ||
file_put_contents( | ||
$_SERVER['DOCUMENT_ROOT'] . '/.env', | ||
'TEST=dev' | ||
); | ||
|
||
file_put_contents( | ||
$_SERVER['DOCUMENT_ROOT'] . '/.env.prod', | ||
'TEST=prod' | ||
); | ||
|
||
$this->obTestObject->load(); | ||
|
||
$this->assertSame($_ENV['TEST'], 'prod'); | ||
|
||
@unlink($_SERVER['DOCUMENT_ROOT'] . '/.env.prod'); | ||
@unlink($_SERVER['DOCUMENT_ROOT'] . '/.env'); | ||
} | ||
|
||
/** | ||
* load(). Local. | ||
* | ||
* @return void | ||
*/ | ||
public function testLoadLocal() : void | ||
{ | ||
file_put_contents( | ||
$_SERVER['DOCUMENT_ROOT'] . '/.env', | ||
'TEST=dev' | ||
); | ||
|
||
file_put_contents( | ||
$_SERVER['DOCUMENT_ROOT'] . '/.env.prod', | ||
'TEST=prod' | ||
); | ||
|
||
file_put_contents( | ||
$_SERVER['DOCUMENT_ROOT'] . '/.env.local', | ||
'TEST=local' | ||
); | ||
|
||
$this->obTestObject->load(); | ||
|
||
$this->assertSame($_ENV['TEST'], 'local'); | ||
|
||
@unlink($_SERVER['DOCUMENT_ROOT'] . '/.env'); | ||
@unlink($_SERVER['DOCUMENT_ROOT'] . '/.env.prod'); | ||
@unlink($_SERVER['DOCUMENT_ROOT'] . '/.env.local'); | ||
} | ||
|
||
/** | ||
* load(). .env. | ||
* | ||
* @return void | ||
*/ | ||
public function testLoadEnv() : void | ||
{ | ||
file_put_contents( | ||
$_SERVER['DOCUMENT_ROOT'] . '/.env', | ||
'TEST=dev' | ||
); | ||
|
||
$this->obTestObject->load(); | ||
|
||
$this->assertSame($_ENV['TEST'], 'dev'); | ||
|
||
@unlink($_SERVER['DOCUMENT_ROOT'] . '/.env'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters