Skip to content

Commit

Permalink
Merge pull request #21 from helhum/feature/change-template
Browse files Browse the repository at this point in the history
  • Loading branch information
helhum authored Feb 12, 2020
2 parents 85fdbce + e9d0a52 commit 4fa7d7c
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 29 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ are available very early, so that you can use it also during boot time of your a
If the environment variable `APP_ENV` is set to any value, or the specified `.env` file does not
exist, no operation is performed, so that you can safely require this package for production.

If you have the possiblity to expose environment variables in a production environment, it is recommended
If you have the possibility to expose environment variables in a production environment, it is recommended
to do so and also set `APP_ENV` and use the variables that are directly exposed in the environment.

However for smaller scale projects it is still a valid and easy solution to use a `.env` file
Expand All @@ -32,10 +32,11 @@ adapt the path or name of the `.env` to fit your requirements.

You configure dotenv connector in the extra section of the root `composer.json` file like that:

```
```json
"extra": {
"helhum/dotenv-connector": {
"env-file": ".env"
"env-file": ".env",
"include-template-file": "{$vendor-dir}/helhum/dotenv-connector/res/PHP/dotenv-include.php.tmpl"
}
}
```
Expand All @@ -46,7 +47,22 @@ You can specify a relative path from the base directory, if you want to put your
*The default value* is ".env", which means next to your root `composer.json`.

##### Side note for quoting values in the `.env` file
As the `.env` file parsing behaves like if it was included in a shell, you have to be aware of that values with literal `$` signs need to be enclosed in single quotes. This may be the case if you use argon hashed values in credetentials you pass via `.env`, for example.
As the `.env` file parsing behaves like if it was included in a shell,
you have to be aware of that values with literal `$` signs
need to be enclosed in single quotes.
This may be the case if you use hashed values of credentials you pass via `.env`, for example.

#### `include-template-file`
You can specify a relative path from the base directory,
if you need a different template for the to be created include file.

*The default value* is "{$vendor-dir}/helhum/dotenv-connector/res/PHP/dotenv-include.php.tmpl", which uses the
default template, which uses symfony/dotenv default parsing of the one .env file.
Change this option with great care and at your own risk.
This could be useful though e.g. if you prefer to use another dotenv parsing library to expose the variables defined in .env
or you want to switch to another parsing strategy of the Symfony dotenv parsing. In the latter case use
"{$vendor-dir}/helhum/dotenv-connector/res/PHP/dotenv-include-sf-loadenv.php.tmpl" as value for this option.
Have a look at the existing template files for examples for your own files in case you need them.

## Feedback

Expand Down
6 changes: 6 additions & 0 deletions res/PHP/dotenv-include-sf-loadenv.php.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
call_user_func(static function($dotEnvFile) {
if (file_exists($dotEnvFile)) {
(new \Symfony\Component\Dotenv\Dotenv(true))->loadEnv($dotEnvFile);
}
}, '{$env-file}');
4 changes: 2 additions & 2 deletions res/PHP/dotenv-include.php.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
call_user_func(function($dotEnvFile) {
call_user_func(static function($dotEnvFile) {
if (!getenv('APP_ENV') && file_exists($dotEnvFile)) {
(new \Symfony\Component\Dotenv\Dotenv())->load($dotEnvFile);
(new \Symfony\Component\Dotenv\Dotenv(true))->load($dotEnvFile);
}
}, '{$env-file}');
15 changes: 13 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Config
*/
public static $defaultConfig = [
'env-file' => '.env',
'include-template-file' => '{$vendor-dir}/helhum/dotenv-connector/res/PHP/dotenv-include.php.tmpl',
];

/**
Expand Down Expand Up @@ -72,7 +73,7 @@ public function get($key, $flags = 0)
switch ($key) {
case 'env-file':
$val = rtrim($this->process($this->config[$key], $flags), '/\\');
return ($flags & self::RELATIVE_PATHS == 1) ? $val : $this->realpath($val);
return ($flags & self::RELATIVE_PATHS === 1) ? $val : $this->realpath($val);
default:
return $this->process($this->config[$key], $flags);
}
Expand Down Expand Up @@ -175,12 +176,22 @@ public static function load(\Composer\IO\IOInterface $io, \Composer\Config $comp
{
static $config;
if ($config === null) {
$baseDir = realpath(substr($composerConfig->get('vendor-dir'), 0, -strlen($composerConfig->get('vendor-dir', self::RELATIVE_PATHS))));
$vendorDir = $composerConfig->get('vendor-dir');
$baseDir = realpath(substr($vendorDir, 0, -strlen($composerConfig->get('vendor-dir', self::RELATIVE_PATHS))));
$localConfig = \Composer\Factory::getComposerFile();
$file = new \Composer\Json\JsonFile($localConfig, null, $io);

$config = new static($baseDir);
$config->merge($file->read());
$config->merge(
[
'extra' => [
'helhum/dotenv-connector' => [
'vendor-dir' => $vendorDir,
],
],
]
);
}
return $config;
}
Expand Down
11 changes: 4 additions & 7 deletions src/IncludeFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,10 @@ class IncludeFile

public function __construct(Config $config, $loader, $includeFile = '', $includeFileTemplate = '', Filesystem $filesystem = null)
{
if (!$loader instanceof ClassLoader) {
// We're called by a previous version of the plugin
$includeFileTemplate = $includeFile;
$includeFile = $loader;
$loader = new ClassLoader();
}
$this->config = $config;
$this->loader = $loader;
$this->includeFile = $includeFile;
$this->includeFileTemplate = $includeFileTemplate ?: dirname(__DIR__) . '/res/PHP/dotenv-include.php.tmpl';
$this->includeFileTemplate = $includeFileTemplate ?: $config->get('include-template-file');
$this->filesystem = $filesystem ?: new Filesystem();
}

Expand All @@ -81,6 +75,9 @@ public function dump()
*/
private function getIncludeFileContent()
{
if (!file_exists($this->includeFileTemplate)) {
throw new \RuntimeException('Include file template defined for helhum/dotenv-connector does not exist!', 1581515568);
}
$envFile = $this->config->get('env-file');
$pathToEnvFileCode = $this->filesystem->findShortestPathCode(
$this->includeFile,
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function onPreAutoloadDump()
$autoloadDefinition = $rootPackage->getAutoload();
$autoloadDefinition['files'][] = $includeFilePath;
$rootPackage->setAutoload($autoloadDefinition);
$this->io->writeError('<info>Registered helhum/dotenv-connector</info>');
$this->io->writeError('<info>helhum/dotenv-connector:</info> Generated dotenv include file');
} else {
$this->io->writeError('<error>Could not dump helhum/dotenv-connector autoload include file</error>');
}
Expand Down
18 changes: 5 additions & 13 deletions tests/Unit/IncludeFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function dumpDumpsFile()
{
$configProphecy = $this->prophesize(Config::class);
$configProphecy->get('env-file')->willReturn(__DIR__ . '/Fixtures/env/.env');
$configProphecy->get('include-template-file')->willReturn(__DIR__ . '/../../res/PHP/dotenv-include.php.tmpl');
$loaderProphecy = $this->prophesize(ClassLoader::class);
$loaderProphecy->register()->shouldBeCalled();
$loaderProphecy->unregister()->shouldBeCalled();
Expand All @@ -46,6 +47,7 @@ public function includingFileExposesEnvVars()
{
$configProphecy = $this->prophesize(Config::class);
$configProphecy->get('env-file')->willReturn(__DIR__ . '/Fixtures/env/.env');
$configProphecy->get('include-template-file')->willReturn(__DIR__ . '/../../res/PHP/dotenv-include.php.tmpl');
$loaderProphecy = $this->prophesize(ClassLoader::class);
$loaderProphecy->register()->shouldBeCalled();
$loaderProphecy->unregister()->shouldBeCalled();
Expand All @@ -66,6 +68,7 @@ public function includingFileDoesNothingIfEnvVarSet()
putenv('APP_ENV=1');
$configProphecy = $this->prophesize(Config::class);
$configProphecy->get('env-file')->willReturn(__DIR__ . '/Fixtures/env/.env');
$configProphecy->get('include-template-file')->willReturn(__DIR__ . '/../../res/PHP/dotenv-include.php.tmpl');
$loaderProphecy = $this->prophesize(ClassLoader::class);
$loaderProphecy->register()->shouldBeCalled();
$loaderProphecy->unregister()->shouldBeCalled();
Expand All @@ -85,6 +88,7 @@ public function includingFileDoesNothingIfEnvFileDoesNotExist()
{
$configProphecy = $this->prophesize(Config::class);
$configProphecy->get('env-file')->willReturn(__DIR__ . '/Fixtures/env/.no-env');
$configProphecy->get('include-template-file')->willReturn(__DIR__ . '/../../res/PHP/dotenv-include.php.tmpl');
$loaderProphecy = $this->prophesize(ClassLoader::class);
$loaderProphecy->register()->shouldBeCalled();
$loaderProphecy->unregister()->shouldBeCalled();
Expand All @@ -104,6 +108,7 @@ public function dumpReturnsFalseIfFileCannotBeWritten()
{
$configProphecy = $this->prophesize(Config::class);
$configProphecy->get('env-file')->willReturn(__DIR__ . '/Fixtures/env/.no-env');
$configProphecy->get('include-template-file')->willReturn(__DIR__ . '/../../res/PHP/dotenv-include.php.tmpl');
$loaderProphecy = $this->prophesize(ClassLoader::class);
$loaderProphecy->register()->shouldNotBeCalled();
$loaderProphecy->unregister()->shouldNotBeCalled();
Expand All @@ -113,17 +118,4 @@ public function dumpReturnsFalseIfFileCannotBeWritten()
$includeFile = new IncludeFile($configProphecy->reveal(), $loaderProphecy->reveal(), $includeFilePath);
$this->assertFalse($includeFile->dump());
}

/**
* @test
*/
public function creatingTheObjectAndDumpingWorksWithLegacyPlugin()
{
$configProphecy = $this->prophesize(Config::class);
$configProphecy->get('env-file')->willReturn(__DIR__ . '/Fixtures/env/.no-env');

$includeFilePath = __DIR__ . '/Fixtures/vendor/helhum/include.php';
$includeFile = new IncludeFile($configProphecy->reveal(), $includeFilePath);
$this->assertTrue($includeFile->dump());
}
}

0 comments on commit 4fa7d7c

Please sign in to comment.