diff options
| author | Peter Nguyen <peteralistairnguyen@gmail.com> | 2025-02-05 00:44:12 -0600 |
|---|---|---|
| committer | Peter Nguyen <peteralistairnguyen@gmail.com> | 2025-02-05 00:44:12 -0600 |
| commit | 110dc2831488937c1afb70c11657a341912fc8cd (patch) | |
| tree | 16b35df40e150d68344337b17c800b04b1435355 /vendor/vlucas/phpdotenv/src/Loader/Resolver.php | |
Initial commit 2/25/2025
Diffstat (limited to 'vendor/vlucas/phpdotenv/src/Loader/Resolver.php')
| -rw-r--r-- | vendor/vlucas/phpdotenv/src/Loader/Resolver.php | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/vendor/vlucas/phpdotenv/src/Loader/Resolver.php b/vendor/vlucas/phpdotenv/src/Loader/Resolver.php new file mode 100644 index 0000000..36d7a4b --- /dev/null +++ b/vendor/vlucas/phpdotenv/src/Loader/Resolver.php @@ -0,0 +1,65 @@ +<?php + +declare(strict_types=1); + +namespace Dotenv\Loader; + +use Dotenv\Parser\Value; +use Dotenv\Repository\RepositoryInterface; +use Dotenv\Util\Regex; +use Dotenv\Util\Str; +use PhpOption\Option; + +final class Resolver +{ + /** + * This class is a singleton. + * + * @codeCoverageIgnore + * + * @return void + */ + private function __construct() + { + // + } + + /** + * Resolve the nested variables in the given value. + * + * Replaces ${varname} patterns in the allowed positions in the variable + * value by an existing environment variable. + * + * @param \Dotenv\Repository\RepositoryInterface $repository + * @param \Dotenv\Parser\Value $value + * + * @return string + */ + public static function resolve(RepositoryInterface $repository, Value $value) + { + return \array_reduce($value->getVars(), static function (string $s, int $i) use ($repository) { + return Str::substr($s, 0, $i).self::resolveVariable($repository, Str::substr($s, $i)); + }, $value->getChars()); + } + + /** + * Resolve a single nested variable. + * + * @param \Dotenv\Repository\RepositoryInterface $repository + * @param string $str + * + * @return string + */ + private static function resolveVariable(RepositoryInterface $repository, string $str) + { + return Regex::replaceCallback( + '/\A\${([a-zA-Z0-9_.]+)}/', + static function (array $matches) use ($repository) { + return Option::fromValue($repository->get($matches[1])) + ->getOrElse($matches[0]); + }, + $str, + 1 + )->success()->getOrElse($str); + } +} |
