Skip to content

Commit

Permalink
Merge pull request #40 from antonioribeiro/allowextension
Browse files Browse the repository at this point in the history
Allow extensions to override methods and the property
  • Loading branch information
vlucas committed Oct 21, 2014
2 parents 56c252d + e36110b commit d33518e
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/Dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Dotenv
* If true, then environment variables will not be overwritten
* @var bool
*/
private static $immutable = true;
protected static $immutable = true;

/**
* Load `.env` file in given directory
Expand Down Expand Up @@ -45,7 +45,7 @@ public static function load($path, $file = '.env')
}
// Only use non-empty lines that look like setters
if (strpos($line, '=') !== false) {
self::setEnvironmentVariable($line);
static::setEnvironmentVariable($line);
}
}
}
Expand All @@ -63,11 +63,11 @@ public static function load($path, $file = '.env')
*/
public static function setEnvironmentVariable($name, $value = null)
{
list($name, $value) = self::normaliseEnvironmentVariable($name, $value);
list($name, $value) = static::normaliseEnvironmentVariable($name, $value);

// Don't overwrite existing environment variables if we're immutable
// Ruby's dotenv does this with `ENV[key] ||= value`.
if (self::$immutable === true && !is_null(self::findEnvironmentVariable($name))) {
if (static::$immutable === true && !is_null(static::findEnvironmentVariable($name))) {
return;
}

Expand All @@ -91,7 +91,7 @@ public static function required($environmentVariables, array $allowedValues = ar
$missingEnvironmentVariables = array();

foreach ($environmentVariables as $environmentVariable) {
$value = self::findEnvironmentVariable($environmentVariable);
$value = static::findEnvironmentVariable($environmentVariable);
if (is_null($value)) {
$missingEnvironmentVariables[] = $environmentVariable;
} elseif ($allowedValues) {
Expand Down Expand Up @@ -125,12 +125,12 @@ public static function required($environmentVariables, array $allowedValues = ar
* @param $value
* @return array
*/
private static function normaliseEnvironmentVariable($name, $value)
protected static function normaliseEnvironmentVariable($name, $value)
{
list($name, $value) = self::splitCompoundStringIntoParts($name, $value);
$name = self::sanitiseVariableName($name);
$value = self::sanitiseVariableValue($value);
$value = self::resolveNestedVariables($value);
list($name, $value) = static::splitCompoundStringIntoParts($name, $value);
$name = static::sanitiseVariableName($name);
$value = static::sanitiseVariableValue($value);
$value = static::resolveNestedVariables($value);

return array($name, $value);
}
Expand All @@ -142,7 +142,7 @@ private static function normaliseEnvironmentVariable($name, $value)
* @param $value
* @return array
*/
private static function splitCompoundStringIntoParts($name, $value)
protected static function splitCompoundStringIntoParts($name, $value)
{
if (strpos($name, '=') !== false) {
list($name, $value) = array_map('trim', explode('=', $name, 2));
Expand All @@ -157,7 +157,7 @@ private static function splitCompoundStringIntoParts($name, $value)
* @param $value
* @return string
*/
private static function sanitiseVariableValue($value)
protected static function sanitiseVariableValue($value)
{
$value = trim($value);
if (!$value) return '';
Expand Down Expand Up @@ -191,7 +191,7 @@ private static function sanitiseVariableValue($value)
* @param $name
* @return string
*/
private static function sanitiseVariableName($name)
protected static function sanitiseVariableName($name)
{
return trim(str_replace(array('export ', '\'', '"'), '', $name));
}
Expand All @@ -203,7 +203,7 @@ private static function sanitiseVariableName($name)
* @param $value
* @return mixed
*/
private static function resolveNestedVariables($value)
protected static function resolveNestedVariables($value)
{
if (strpos($value, '$') !== false) {
$value = preg_replace_callback(
Expand Down Expand Up @@ -247,14 +247,14 @@ public static function findEnvironmentVariable($name)
*/
public static function makeImmutable()
{
self::$immutable = true;
static::$immutable = true;
}

/**
* Make Dotenv mutable. Environment variables will act as, well, variables.
*/
public static function makeMutable()
{
self::$immutable = false;
static::$immutable = false;
}
}

0 comments on commit d33518e

Please sign in to comment.