8000 Allow configuration flags to be set within individual .env files by adam-prickett · Pull Request #200 · vlucas/phpdotenv · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Allow configuration flags to be set within individual .env files #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,17 @@ VAR="value" # comment
VAR=value # comment
```

Configuration Flags
-----------------------------

You may enable or disable certain features by using configuration flags at
the top of a .env file, before any variable declarations.

```shell
PHPDOTENV_NO_SET_ENV # Prevents environment variables being set to the $_ENV superglobal
PHPDOTENV_NO_SET_SERVER # Prevents environment variables being set to the $_SERVER superglobal
```

Usage Notes
-----------

Expand Down
93 changes: 91 additions & 2 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ class Loader
*/
protected $immutable;

/**
* Array of config variables
*
* @var array
*/
protected $configFlags = array();

/**
* Have we set an environment variable yet?
*
* @var bool
*/
protected $hasSetEnvironmentVariable = false;

/**
* Create a new loader instance.
*
Expand All @@ -54,6 +68,12 @@ public function load()
$filePath = $this->filePath;
$lines = $this->readLinesFromFile($filePath);
foreach ($lines as $line) {
if (!$this->isComment($line) && $this->looksLikeConfigFlag($line)) {
if ($this->hasSetEnvironmentVariable) {
throw new InvalidFileException('PHPDOTENV_ flags must appear before any environment variables are set');
}
$this->setConfigFlag($line);
}
if (!$this->isComment($line) && $this->looksLikeSetter($line)) {
$this->setEnvironmentVariable($line);
}
Expand Down Expand Up @@ -162,6 +182,18 @@ protected function looksLikeSetter($line)
return strpos($line, '=') !== false;
}

/**
* Determine if the given line looks like a config flag
*
* @param string $line
*
* @return bool
*/
protected function looksLikeConfigFlag($line)
{
return strpos(ltrim($line), 'PHPDOTENV_') === 0;
}

/**
* Split the compound string into parts.
*
Expand Down Expand Up @@ -278,6 +310,23 @@ protected function sanitiseVariableName($name, $value)
return array($name, $value);
}

/**
* Removes EOL comments and santises config flag values
*
* @param string $name
* @return string
*/
protected function sanitiseConfigFlag($name)
{
// Remove EOL comments from flags
$parts = explode(' #', $name, 2);
$name = trim($parts[0]);

$name = trim(str_replace(array('export ', '\'', '"'), '', $name));

return $name;
}

/**
* Determine if the given string begins with a quote.
*
Expand Down Expand Up @@ -327,6 +376,10 @@ public function getEnvironmentVariable($name)
*/
public function setEnvironmentVariable($name, $value = null)
{
if (!$this->hasSetEnvironmentVariable) {
$this->hasSetEnvironmentVariable = true;
}

list($name, $value) = $this->normaliseEnvironmentVariable($name, $value);

// Don't overwrite existing environment variables if we're immutable
Expand All @@ -345,8 +398,15 @@ public function setEnvironmentVariable($name, $value = null)
putenv("$name=$value");
}

$_ENV[$name] = $value;
$_SERVER[$name] = $value;
// Set the $_ENV superglobal if NO_SET_ENV flag has not been set
if (!$this->hasConfigFlag('NO_SET_ENV')) {
$_ENV[$name] = $value;
}

// Set the $_SERVER superglobal if NO_SET_SERVER flag has not been set
if (!$this->hasConfigFlag('NO_SET_SERVER')) {
$_SERVER[$name] = $value;
}
}

/**
Expand Down Expand Up @@ -378,4 +438,33 @@ public function clearEnvironmentVariable($name)

unset($_ENV[$name], $_SERVER[$name]);
}

/**
* Set a config variable to the $config array
*
* @param string $name
*
* @return void
*/
public function setConfigFlag($name)
{
$name = $this->sanitiseConfigFlag($name);

// Remove the PHPDOTENV_ prefix
$name = substr($name, 10);

$this->configFlags[$name] = true;
}

/**
* Utility method to check for config flag being set
*
* @param string $name
*
* @return boolean
*/
public function hasConfigFlag($name)
{
return (isset($this->configFlags[$name]));
}
}
30 changes: 30 additions & 0 deletions tests/Dotenv/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,34 @@ public function testDotenvRequiredCanBeUsedWithoutLoadingFile()
$dotenv->required('REQUIRED_VAR')->notEmpty();
$this->assertTrue(true);
}

public function testDotenvConfigFlagSets()
{
$dotenv = new Dotenv($this->fixturesFolder, 'flag.env');
$dotenv->load();

$this->assertEquals($_SERVER['CONFIG_SET'], 'true');
$this->assertEquals(getenv('CONFIG_SET'), 'true');
$this->assertTrue(!isset($_ENV['CONFIG_SET']));
}

public function testDotenvConfigFlagSetsWithEOLComment()
{
$dotenv = new Dotenv($this->fixturesFolder, 'flag-comment.env');
$dotenv->load();

$this->assertEquals($_SERVER['CONFIG_SET'], 'true');
$this->assertEquals(getenv('CONFIG_SET'), 'true');
$this->assertTrue(!isset($_ENV['CONFIG_SET']));
}

/**
* @expectedException \Dotenv\Exception\InvalidFileException
* @expectedExceptionMessage PHPDOTENV_ flags must appear before any environment variables are set
*/
public function testDotenvFailsWhenConfigFlagOutOfOrder()
{
$dotenv = new Dotenv(dirname(__DIR__).'/fixtures/env-wrong', 'flag-wrong.env');
$dotenv->load();
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/env-wrong/flag-wrong.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FOO=bar
BAR=baz
SPACED="with spaces"

NULL=
PHPDOTENV_NO_SET_ENV

CONFIG_SET=true
8 changes: 8 additions & 0 deletions tests/fixtures/env/flag-comment.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PHPDOTENV_NO_SET_ENV # Comment
FOO=bar
BAR=baz
SPACED="with spaces"

NULL=

CONFIG_SET=true
8 changes: 8 additions & 0 deletions tests/fixtures/env/flag.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PHPDOTENV_NO_SET_ENV
FOO=bar
BAR=baz
SPACED="with spaces"

NULL=

CONFIG_SET=true
0