Skip to content

[3.1] Minor preg refactoring and added more nesting tests #315

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

Merged
merged 1 commit into from
Jan 4, 2019
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
39 changes: 16 additions & 23 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Dotenv\Environment\FactoryInterface;
use Dotenv\Exception\InvalidPathException;
use Dotenv\Regex\Regex;
use PhpOption\Option;

/**
Expand Down Expand Up @@ -170,29 +171,21 @@ private function processEntries(array $entries)
*/
private function resolveNestedVariables($value = null)
{
if ($value === null || strpos($value, '$') === false) {
return $value;
}

return preg_replace_callback(
'/\${([a-zA-Z0-9_.]+)}/',
[$this, 'nestedVariableResolver'],
$value
);
}

/**
* Resolve the matched variable to its value, if possible.
*
* @param string[] $matches
*
* @return string
*/
private function nestedVariableResolver(array $matches)
{
$nested = $this->getEnvironmentVariable($matches[1]);

return $nested === null ? $matches[0] : $nested;
return Option::fromValue($value)
->filter(function ($str) {
return strpos($str, '$') !== false;
})
->flatMap(function ($str) {
return Regex::replaceCallback(
'/\${([a-zA-Z0-9_.]+)}/',
function (array $matches) {
return Option::fromValue($this->getEnvironmentVariable($matches[1]))
->getOrElse($matches[0]);
},
$str
)->success();
})
->getOrElse($value);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private static function processQuotedValue($value)
$quote
);

return Regex::pregReplace($pattern, '$1', $value)
return Regex::replace($pattern, '$1', $value)
->mapSuccess(function ($str) use ($quote) {
return str_replace('\\\\', '\\', str_replace("\\$quote", $quote, $str));
})
Expand Down
39 changes: 35 additions & 4 deletions src/Regex/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,48 @@
class Regex
{
/**
* Perform a preg replace, failing with an exception.
* Perform a preg replace, wrapping up the result.
*
* @param string $pattern
* @param string $repalcement
* @param string $replacement
* @param string $subject
*
* @return \Dotenv\Regex\Result
*/
public static function pregReplace($pattern, $replacement, $subject)
public static function replace($pattern, $replacement, $subject)
{
$result = (string) @preg_replace($pattern, $replacement, $subject);
return self::pregAndWrap(function ($subject) use ($pattern, $replacement) {
return preg_replace($pattern, $replacement, $subject);
}, $subject);
}

/**
* Perform a preg replace callback, wrapping up the result.
*
* @param string $pattern
* @param callable $callback
* @param string $subject
*
* @return \Dotenv\Regex\Result
*/
public static function replaceCallback($pattern, callable $callback, $subject)
{
return self::pregAndWrap(function ($subject) use ($pattern, $callback) {
return preg_replace_callback($pattern, $callback, $subject);
}, $subject);
}

/**
* Perform a preg operation, wrapping up the result.
*
* @param callable $operation
* @param string $subject
*
* @return \Dotenv\Regex\Result
*/
private static function pregAndWrap(callable $operation, $subject)
{
$result = (string) @$operation($subject);

if (($e = preg_last_error()) !== PREG_NO_ERROR) {
return Error::create(self::lookupError($e));
Expand Down
3 changes: 3 additions & 0 deletions tests/Dotenv/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ public function testDotenvNestedEnvironmentVars()
$this->assertSame('$NVAR1 {NVAR2}', $_ENV['NVAR5']); // not resolved
$this->assertSame('Special Value', $_ENV['N.VAR6']); // new '.' (dot) in var name
$this->assertSame('Special Value', $_ENV['NVAR7']); // nested '.' (dot) variable
$this->assertSame('', $_ENV['NVAR8']);
$this->assertSame('', $_ENV['NVAR9']); // nested variable is empty string
$this->assertSame('${NVAR888}', $_ENV['NVAR10']); // nested variable is not set
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/env/nested.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ NVAR4="${NVAR1} ${NVAR2}"
NVAR5="$NVAR1 {NVAR2}"
N.VAR6="Special Value"
NVAR7="${N.VAR6}"
NVAR8=""
NVAR9="${NVAR8}"
NVAR10="${NVAR888}"