Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace Dotenv\Loader;
6
 
7
use Dotenv\Parser\Value;
8
use Dotenv\Repository\RepositoryInterface;
9
use Dotenv\Util\Regex;
10
use Dotenv\Util\Str;
11
use PhpOption\Option;
12
 
13
final class Resolver
14
{
15
    /**
16
     * This class is a singleton.
17
     *
18
     * @codeCoverageIgnore
19
     *
20
     * @return void
21
     */
22
    private function __construct()
23
    {
24
        //
25
    }
26
 
27
    /**
28
     * Resolve the nested variables in the given value.
29
     *
30
     * Replaces ${varname} patterns in the allowed positions in the variable
31
     * value by an existing environment variable.
32
     *
33
     * @param \Dotenv\Repository\RepositoryInterface $repository
34
     * @param \Dotenv\Parser\Value                   $value
35
     *
36
     * @return string
37
     */
38
    public static function resolve(RepositoryInterface $repository, Value $value)
39
    {
40
        return \array_reduce($value->getVars(), static function (string $s, int $i) use ($repository) {
41
            return Str::substr($s, 0, $i).self::resolveVariable($repository, Str::substr($s, $i));
42
        }, $value->getChars());
43
    }
44
 
45
    /**
46
     * Resolve a single nested variable.
47
     *
48
     * @param \Dotenv\Repository\RepositoryInterface $repository
49
     * @param string                                 $str
50
     *
51
     * @return string
52
     */
53
    private static function resolveVariable(RepositoryInterface $repository, string $str)
54
    {
55
        return Regex::replaceCallback(
56
            '/\A\${([a-zA-Z0-9_.]+)}/',
57
            static function (array $matches) use ($repository) {
58
                return Option::fromValue($repository->get($matches[1]))
59
                    ->getOrElse($matches[0]);
60
            },
61
            $str,
62
            1
63
        )->success()->getOrElse($str);
64
    }
65
}