1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Lenevor Framework |
5
|
|
|
* |
6
|
|
|
* LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the new BSD license that is bundled |
9
|
|
|
* with this package in the file license.md. |
10
|
|
|
* It is also available through the world-wide-web at this URL: |
11
|
|
|
* https://lenevor.com/license |
12
|
|
|
* If you did not receive a copy of the license and are unable to |
13
|
|
|
* obtain it through the world-wide-web, please send an email |
14
|
|
|
* to [email protected] so we can send you a copy immediately. |
15
|
|
|
* |
16
|
|
|
* @package Lenevor |
17
|
|
|
* @subpackage Base |
18
|
|
|
* @link https://lenevor.com |
19
|
|
|
* @copyright Copyright (c) 2019 - 2021 Alexander Campo <[email protected]> |
20
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Syscodes\Support; |
24
|
|
|
|
25
|
|
|
use Syscodes\Dotenv\Repository\RepositoryCreator; |
26
|
|
|
use Syscodes\Dotenv\Repository\Adapters\PutenvAdapter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Gets the adapter environment and value of an environment variable. |
30
|
|
|
* |
31
|
|
|
* @author Alexander Campo <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class Environment |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Indicates if the putenv adapter is enabled. |
37
|
|
|
* |
38
|
|
|
* @var bool $enabledPutenv |
39
|
|
|
*/ |
40
|
|
|
protected static $enabledPutenv = true; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The environment repository instance. |
44
|
|
|
* |
45
|
|
|
* @var Syscodes\Dotenv\Repository\RepositoryCreator|null $repository |
|
|
|
|
46
|
|
|
*/ |
47
|
|
|
protected static $repository; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Gets the value of an environment variable. |
51
|
|
|
* |
52
|
|
|
* @param string $key |
53
|
|
|
* @param mixed $default |
54
|
|
|
* |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
|
|
public static function get($key, $default = null) |
58
|
|
|
{ |
59
|
|
|
$value = static::getRepositoryCreator()->get($key); |
60
|
|
|
|
61
|
|
|
if ($value === false) |
62
|
|
|
{ |
63
|
|
|
return value($default); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Handle any boolean values |
67
|
|
|
switch (strtolower($value)) |
68
|
|
|
{ |
69
|
|
|
case 'true': |
70
|
|
|
case '(true)': |
71
|
|
|
return true; |
72
|
|
|
case 'false': |
73
|
|
|
case '(false)': |
74
|
|
|
return false; |
75
|
|
|
case 'empty': |
76
|
|
|
case '(empty)': |
77
|
|
|
return ''; |
78
|
|
|
case 'null': |
79
|
|
|
case '(null)': |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $value; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the environment repository instance. |
88
|
|
|
* |
89
|
|
|
* @return Syscodes\Dotenv\Repository\RepositoryCreator |
90
|
|
|
*/ |
91
|
|
|
public static function getRepositoryCreator() |
92
|
|
|
{ |
93
|
|
|
if (null === static::$repository) { |
94
|
|
|
$repository = RepositoryCreator::createDefaultAdapters(); |
95
|
|
|
|
96
|
|
|
if (static::$enabledPutenv) { |
97
|
|
|
$repository = $repository->addAdapter(PutenvAdapter::class); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
static::$repository = $repository->make(); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return static::$repository; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
} |