Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Support;
4
 
5
use Dotenv\Repository\Adapter\PutenvAdapter;
6
use Dotenv\Repository\RepositoryBuilder;
7
use PhpOption\Option;
8
 
9
class Env
10
{
11
    /**
12
     * Indicates if the putenv adapter is enabled.
13
     *
14
     * @var bool
15
     */
16
    protected static $putenv = true;
17
 
18
    /**
19
     * The environment repository instance.
20
     *
21
     * @var \Dotenv\Repository\RepositoryInterface|null
22
     */
23
    protected static $repository;
24
 
25
    /**
26
     * Enable the putenv adapter.
27
     *
28
     * @return void
29
     */
30
    public static function enablePutenv()
31
    {
32
        static::$putenv = true;
33
        static::$repository = null;
34
    }
35
 
36
    /**
37
     * Disable the putenv adapter.
38
     *
39
     * @return void
40
     */
41
    public static function disablePutenv()
42
    {
43
        static::$putenv = false;
44
        static::$repository = null;
45
    }
46
 
47
    /**
48
     * Get the environment repository instance.
49
     *
50
     * @return \Dotenv\Repository\RepositoryInterface
51
     */
52
    public static function getRepository()
53
    {
54
        if (static::$repository === null) {
55
            $builder = RepositoryBuilder::createWithDefaultAdapters();
56
 
57
            if (static::$putenv) {
58
                $builder = $builder->addAdapter(PutenvAdapter::class);
59
            }
60
 
61
            static::$repository = $builder->immutable()->make();
62
        }
63
 
64
        return static::$repository;
65
    }
66
 
67
    /**
68
     * Gets the value of an environment variable.
69
     *
70
     * @param  string  $key
71
     * @param  mixed  $default
72
     * @return mixed
73
     */
74
    public static function get($key, $default = null)
75
    {
76
        return Option::fromValue(static::getRepository()->get($key))
77
            ->map(function ($value) {
78
                switch (strtolower($value)) {
79
                    case 'true':
80
                    case '(true)':
81
                        return true;
82
                    case 'false':
83
                    case '(false)':
84
                        return false;
85
                    case 'empty':
86
                    case '(empty)':
87
                        return '';
88
                    case 'null':
89
                    case '(null)':
90
                        return;
91
                }
92
 
93
                if (preg_match('/\A([\'"])(.*)\1\z/', $value, $matches)) {
94
                    return $matches[2];
95
                }
96
 
97
                return $value;
98
            })
99
            ->getOrCall(fn () => value($default));
100
    }
101
}