Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
/composer.lock
/.php-cs-fixer.cache
/.phpunit.result.cache
4 changes: 3 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Define in which folders to search and which folders to exclude
// Exclude some directories that are excluded by Git anyways to speed up the sniffing
$finder = PhpCsFixer\Finder::create()
->in(__DIR__ . '/src/');
->in(__DIR__ . '/src/')
->in(__DIR__ . '/tests/')
;

$configFinder = PhpCsFixer\Finder::create()
->in(__DIR__)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ to do so and also set `APP_ENV` and use the variables that are directly exposed
However for smaller scale projects it is still a valid and easy solution to use a `.env` file
also for production environments.

You can set the environment variable `DOTENV_CONNECTOR_OVERRIDE` to enable overriding of existing environment variables with the values of the `.env` file.

## configuration options

Usually you don't need any configuration options. However if you need to, you can
Expand Down
6 changes: 5 additions & 1 deletion src/Adapter/SymfonyDotEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ public function exposeToEnvironment(string $dotEnvFile): void
if (!getenv('APP_ENV') && file_exists($dotEnvFile)) {
$dotEnv = new Dotenv();
$dotEnv->usePutenv();
$dotEnv->load($dotEnvFile);
if (getenv('DOTENV_CONNECTOR_OVERRIDE')) {
$dotEnv->overload($dotEnvFile);
} else {
$dotEnv->load($dotEnvFile);
}
}
}
}
2 changes: 1 addition & 1 deletion src/Adapter/SymfonyLoadEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function exposeToEnvironment(string $dotEnvFile): void
if (is_file($dotEnvFile) || is_file("$dotEnvFile.dist")) {
$dotEnv = new Dotenv();
$dotEnv->usePutenv();
$dotEnv->loadEnv($dotEnvFile);
$dotEnv->loadEnv($dotEnvFile, null, 'dev', ['test'], (bool)getenv('DOTENV_CONNECTOR_OVERRIDE'));
}
}
}
68 changes: 57 additions & 11 deletions tests/Unit/IncludeFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Helhum\DotEnvConnector\Config;
use Helhum\DotEnvConnector\IncludeFile;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophet;

class IncludeFileTest extends TestCase
{
Expand All @@ -19,6 +18,9 @@ protected function tearDown(): void
}
putenv('FOO');
putenv('APP_ENV');
putenv('DOTENV_CONNECTOR_OVERRIDE');
unset($_ENV['FOO'], $_ENV['APP_ENV'], $_ENV['DOTENV_CONNECTOR_OVERRIDE'], $_ENV['SYMFONY_DOTENV_VARS']);
unset($_SERVER['FOO'], $_SERVER['APP_ENV'], $_SERVER['DOTENV_CONNECTOR_OVERRIDE'], $_SERVER['SYMFONY_DOTENV_VARS']);
if (file_exists(__DIR__ . '/Fixtures/foo')) {
chmod(__DIR__ . '/Fixtures/foo', 777);
rmdir(__DIR__ . '/Fixtures/foo');
Expand All @@ -28,14 +30,13 @@ protected function tearDown(): void
/**
* @test
*/
public function dumpDumpsFile()
public function dumpDumpsFile(): void
{
$config = new Config();
$config->merge(['extra' => ['helhum/dotenv-connector' => [
'env-file' => __DIR__ . '/Fixtures/env/.env',
'adapter' => SymfonyDotEnv::class
]]]);
/** @var ClassLoader|\PHPUnit\Framework\MockObject\MockObject $loaderMock */
$loaderMock = $this->createMock(ClassLoader::class);
$loaderMock->expects($this->once())->method('register');
$loaderMock->expects($this->once())->method('unregister');
Expand All @@ -49,14 +50,13 @@ public function dumpDumpsFile()
/**
* @test
*/
public function includingFileExposesEnvVars()
public function includingFileExposesEnvVars(): void
{
$config = new Config();
$config->merge(['extra' => ['helhum/dotenv-connector' => [
'env-file' => __DIR__ . '/Fixtures/env/.env',
'adapter' => SymfonyDotEnv::class
]]]);
/** @var ClassLoader|\PHPUnit\Framework\MockObject\MockObject $loaderMock */
$loaderMock = $this->createMock(ClassLoader::class);
$loaderMock->expects($this->once())->method('register');
$loaderMock->expects($this->once())->method('unregister');
Expand All @@ -72,15 +72,14 @@ public function includingFileExposesEnvVars()
/**
* @test
*/
public function includingFileDoesNothingIfEnvVarSet()
public function includingFileDoesNothingIfEnvVarSet(): void
{
putenv('APP_ENV=1');
$config = new Config();
$config->merge(['extra' => ['helhum/dotenv-connector' => [
'env-file' => __DIR__ . '/Fixtures/env/.env',
'adapter' => SymfonyDotEnv::class
]]]);
/** @var ClassLoader|\PHPUnit\Framework\MockObject\MockObject $loaderMock */
$loaderMock = $this->createMock(ClassLoader::class);
$loaderMock->expects($this->once())->method('register');
$loaderMock->expects($this->once())->method('unregister');
Expand All @@ -96,14 +95,13 @@ public function includingFileDoesNothingIfEnvVarSet()
/**
* @test
*/
public function includingFileDoesNothingIfEnvFileDoesNotExist()
public function includingFileDoesNothingIfEnvFileDoesNotExist(): void
{
$config = new Config();
$config->merge(['extra' => ['helhum/dotenv-connector' => [
'env-file' => __DIR__ . '/Fixtures/env/.no-env',
'adapter' => SymfonyDotEnv::class
]]]);
/** @var ClassLoader|\PHPUnit\Framework\MockObject\MockObject $loaderMock */
$loaderMock = $this->createMock(ClassLoader::class);
$loaderMock->expects($this->once())->method('register');
$loaderMock->expects($this->once())->method('unregister');
Expand All @@ -119,14 +117,13 @@ public function includingFileDoesNothingIfEnvFileDoesNotExist()
/**
* @test
*/
public function dumpReturnsFalseIfFileCannotBeWritten()
public function dumpReturnsFalseIfFileCannotBeWritten(): void
{
$config = new Config();
$config->merge(['extra' => ['helhum/dotenv-connector' => [
'env-file' => __DIR__ . '/Fixtures/env/.no-env',
'adapter' => SymfonyDotEnv::class
]]]);
/** @var ClassLoader|\PHPUnit\Framework\MockObject\MockObject $loaderMock */
$loaderMock = $this->createMock(ClassLoader::class);
$loaderMock->expects($this->once())->method('register');
$loaderMock->expects($this->once())->method('unregister');
Expand All @@ -136,4 +133,53 @@ public function dumpReturnsFalseIfFileCannotBeWritten()
$includeFile = new IncludeFile($config, $loaderMock, $includeFilePath);
$this->assertFalse($includeFile->dump());
}

/**
* @test
*/
public function includingFileDoesNotOverrideExistingEnvVars(): void
{
putenv('FOO=baz');
$_ENV['FOO'] = 'baz';
$config = new Config();
$config->merge(['extra' => ['helhum/dotenv-connector' => [
'env-file' => __DIR__ . '/Fixtures/env/.env',
'adapter' => SymfonyDotEnv::class
]]]);
$loaderMock = $this->createMock(ClassLoader::class);
$loaderMock->expects($this->once())->method('register');
$loaderMock->expects($this->once())->method('unregister');

$includeFilePath = __DIR__ . '/Fixtures/vendor/helhum/include.php';
$includeFile = new IncludeFile($config, $loaderMock, $includeFilePath);
$includeFile->dump();
$this->assertFileExists($includeFilePath);

$this->assertSame('baz', getenv('FOO'));
}

/**
* @test
*/
public function includingFileDoesOverrideExistingEnvVars(): void
{
putenv('FOO=baz');
$_ENV['FOO'] = 'baz';
putenv('DOTENV_CONNECTOR_OVERRIDE=1');
$config = new Config();
$config->merge(['extra' => ['helhum/dotenv-connector' => [
'env-file' => __DIR__ . '/Fixtures/env/.env',
'adapter' => SymfonyDotEnv::class
]]]);
$loaderMock = $this->createMock(ClassLoader::class);
$loaderMock->expects($this->once())->method('register');
$loaderMock->expects($this->once())->method('unregister');

$includeFilePath = __DIR__ . '/Fixtures/vendor/helhum/include.php';
$includeFile = new IncludeFile($config, $loaderMock, $includeFilePath);
$includeFile->dump();
$this->assertFileExists($includeFilePath);

$this->assertSame('bar', getenv('FOO'));
}
}