|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Linna Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
|
7
|
|
|
* @copyright (c) 2018, Sebastian Rapetti |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types = 1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Linna\DotEnv; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* DotEnv. |
|
16
|
|
|
* |
|
17
|
|
|
* Load variables from a .env file to environment. |
|
18
|
|
|
*/ |
|
19
|
|
|
class DotEnv |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array<mixed> Matches for particula values |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $valuesMatches = [ |
|
25
|
|
|
'true' => true, |
|
26
|
|
|
'(true)' => true, |
|
27
|
|
|
'false' => false, |
|
28
|
|
|
'(false)' => false, |
|
29
|
|
|
'empty' => '', |
|
30
|
|
|
'(empty)' => '', |
|
31
|
|
|
'null' => null, |
|
32
|
|
|
'(null)' => null, |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Get a value from environment. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $key Key name |
|
39
|
|
|
* @param mixed $default Default value if key not found |
|
40
|
|
|
* |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
*/ |
|
43
|
45 |
|
public function get(string $key, $default = null) |
|
44
|
|
|
{ |
|
45
|
45 |
|
if (($value = \getenv($key)) === false) { |
|
46
|
1 |
|
return $default; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
44 |
|
if (\array_key_exists(\strtolower($value), self::$valuesMatches)) { |
|
50
|
16 |
|
return self::$valuesMatches[\strtolower($value)]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
28 |
|
return $value; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Load environment variables from file. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $file Path to .env file |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
46 |
|
public function load(string $file): bool |
|
64
|
|
|
{ |
|
65
|
46 |
|
if (!\file_exists($file)) { |
|
66
|
1 |
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
45 |
|
$content = \file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
|
70
|
|
|
|
|
71
|
45 |
|
foreach ($content as $line) { |
|
72
|
45 |
|
$line = \rtrim(\ltrim($line)); |
|
73
|
|
|
|
|
74
|
|
|
//check if the line contains a key value pair |
|
75
|
45 |
|
if (!\preg_match("/^\s*([\w.-]+)\s*=\s*(.*)?\s*$/", $line)) { |
|
76
|
45 |
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
45 |
|
[$key, $value] = \explode('=', $line, 2); |
|
80
|
|
|
|
|
81
|
|
|
//remove special chars from beninning and end of key values |
|
82
|
45 |
|
$key = \trim($key, " \t\n\r\0\x0B"); |
|
83
|
45 |
|
$value = \trim($value, " \t\n\r\0\x0B"); |
|
84
|
|
|
|
|
85
|
|
|
//set to empty value |
|
86
|
45 |
|
if (\strlen($value) === 0) { |
|
87
|
24 |
|
\putenv("{$key}="); |
|
88
|
24 |
|
continue; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
//remove single and double quotes |
|
92
|
45 |
|
$this->unQuote($value); |
|
93
|
|
|
|
|
94
|
45 |
|
\putenv("{$key}={$value}"); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
45 |
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Remove quotes or double quotes from the begin and the end of a string |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $value |
|
104
|
|
|
* |
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
45 |
|
private function unQuote(string &$value): void |
|
108
|
|
|
{ |
|
109
|
45 |
|
$first = $value[0]; |
|
110
|
45 |
|
$last = $value[-1]; |
|
111
|
45 |
|
$edges = $first.$last; |
|
112
|
|
|
|
|
113
|
|
|
//string begin and end with ' or " |
|
114
|
45 |
|
if ($edges === "''" || $edges === '""') { |
|
115
|
45 |
|
$value = \substr($value, 1, -1); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
//string begin with ' or " |
|
119
|
45 |
|
elseif ($first === "'" || $first === '"') { |
|
120
|
24 |
|
$value = \substr($value, 1); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
//string end with ' or " |
|
124
|
45 |
|
elseif ($last === "'" || $last === '"') { |
|
125
|
24 |
|
$value = \substr($value, 0, -1); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|