Skip to content

Commit

Permalink
Fix small details and add some doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi FUSSIEN committed Feb 2, 2017
1 parent bbea6e8 commit 08dcaeb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
8 changes: 5 additions & 3 deletions .gitattributes
Expand Up @@ -4,8 +4,10 @@
# Ignore all test and documentation with "export-ignore".
/.gitattributes export-ignore
/.gitignore export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/docs export-ignore
/gulpfile.js export-ignore
/package.json export-ignore
/phpunit.xml.dist export-ignore
/.scrutinizer.yml export-ignore
/tests export-ignore
/docs export-ignore
/tests export-ignore
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -9,7 +9,10 @@

## Requirements

PHP >= 5.6
PHP >= 5.6 | >= 7.0 | >= 7.1 <br>
:warning:
**HHVM not supported so far**
:warning:

## Install

Expand Down
23 changes: 20 additions & 3 deletions src/Loader.php
Expand Up @@ -14,23 +14,30 @@ class Loader
public function __construct($path, $filename = '.env')
{
$path = rtrim($path, DIRECTORY_SEPARATOR);
$this->file = $path . DIRECTORY_SEPARATOR . $filename;

return $this;
$this->file = $path . DIRECTORY_SEPARATOR . $filename;
}

/**
* parse and set the env
*
* @return self
*/
public function load()
{
$this->parse();

$this->toEnv();

return $this;
}

public function parse(array $options = [])
/**
* Parse the env file
*
* @return self
*/
public function parse()
{
$this->parser = new Parser;

Expand All @@ -41,6 +48,8 @@ public function parse(array $options = [])

/**
* Return the parser
*
* @return Rfussien\Dotenv\Parser
*/
public function getParser()
{
Expand All @@ -60,6 +69,9 @@ public function toEnv()
return $this;
}

/**
* Set an env variable into $_ENV, $_SERVER and putenv
*/
public static function putenv($key, $value)
{
$_ENV[$key] = $value;
Expand All @@ -72,13 +84,18 @@ public static function putenv($key, $value)
putenv("$key=$value");
}

/**
* Return the value of an environment variable
*/
public static function getenv($key, $default = null)
{
switch (true) {
case array_key_exists($key, $_ENV):
return $_ENV[$key];

case array_key_exists($key, $_SERVER):
return $_SERVER[$key];

default:
$value = getenv($key);
return $value === false ? $default : $value;
Expand Down
13 changes: 12 additions & 1 deletion src/Parser.php
Expand Up @@ -32,6 +32,9 @@ public function __construct()
}
}

/**
* Set the scanner_mode used by the parse_ini_file function
*/
public function setScannerMode($scannerMode)
{
$this->scannerMode = $scannerMode;
Expand Down Expand Up @@ -59,9 +62,12 @@ public function parse($file)
return $this;
}

/**
* Transforme the boolean used as string in the env file to real boolean
*/
public function sanitizeValues()
{
array_walk($this->content, function (&$value, $key) {
array_walk($this->content, function (&$value) {
// sanitize boolean values
if (in_array($value, ['true', 'on', 'yes', 'false', 'off', 'no'])) {
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
Expand All @@ -71,6 +77,11 @@ public function sanitizeValues()
return $this;
}

/**
* Return the parsed content
*
* @return array content
*/
public function getContent()
{
return $this->content;
Expand Down
5 changes: 5 additions & 0 deletions tests/HelperTest.php
Expand Up @@ -16,4 +16,9 @@ public function testDefaultValueForEnvHelperFunction()
{
$this->assertEquals('value', env('K21'));
}

public function testTheEnvHelperCanReturnTheDefaultValue()
{
$this->assertEquals('default', env('ITDOESNTEXISTS', 'default'));
}
}

0 comments on commit 08dcaeb

Please sign in to comment.