1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Framework |
5
|
|
|
* |
6
|
|
|
* Platine Framework is a lightweight, high-performance, simple and elegant |
7
|
|
|
* PHP Web framework |
8
|
|
|
* |
9
|
|
|
* This content is released under the MIT License (MIT) |
10
|
|
|
* |
11
|
|
|
* Copyright (c) 2020 Platine Framework |
12
|
|
|
* Copyright (c) 2017 Jitendra Adhikari |
13
|
|
|
* |
14
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
15
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
16
|
|
|
* in the Software without restriction, including without limitation the rights |
17
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
18
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
19
|
|
|
* furnished to do so, subject to the following conditions: |
20
|
|
|
* |
21
|
|
|
* The above copyright notice and this permission notice shall be included in all |
22
|
|
|
* copies or substantial portions of the Software. |
23
|
|
|
* |
24
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
25
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
26
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
27
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
28
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
29
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
30
|
|
|
* SOFTWARE. |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @file Env.php |
35
|
|
|
* |
36
|
|
|
* The Environment management class |
37
|
|
|
* |
38
|
|
|
* @package Platine\Framework\Env |
39
|
|
|
* @author Platine Developers team |
40
|
|
|
* @copyright Copyright (c) 2020 |
41
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
42
|
|
|
* @link https://www.platine-php.com |
43
|
|
|
* @version 1.0.0 |
44
|
|
|
* @filesource |
45
|
|
|
*/ |
46
|
|
|
|
47
|
|
|
declare(strict_types=1); |
48
|
|
|
|
49
|
|
|
namespace Platine\Framework\Env; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @class Env |
53
|
|
|
* @package Platine\Framework\Env |
54
|
|
|
*/ |
55
|
|
|
class Env |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* Get the environment variable by its key/name. |
59
|
|
|
* @param string $key |
60
|
|
|
* @param mixed $default |
61
|
|
|
* @param string|null $filter |
62
|
|
|
* @param int|array<string, mixed> $options Additional options to filter. |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public static function get( |
66
|
|
|
string $key, |
67
|
|
|
mixed $default = null, |
68
|
|
|
?string $filter = null, |
69
|
|
|
int|array $options = 0 |
70
|
|
|
): mixed { |
71
|
|
|
if ($filter !== null) { |
72
|
|
|
$filterMaps = [ |
73
|
|
|
'bool' => FILTER_VALIDATE_BOOLEAN, |
74
|
|
|
'int' => FILTER_VALIDATE_INT, |
75
|
|
|
'float' => FILTER_VALIDATE_FLOAT, |
76
|
|
|
'email' => FILTER_VALIDATE_EMAIL, |
77
|
|
|
'ip' => FILTER_VALIDATE_IP, |
78
|
|
|
'url' => FILTER_VALIDATE_URL, |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
if (isset($filterMaps[$filter])) { |
82
|
|
|
$filter = $filterMaps[$filter]; |
83
|
|
|
} else { |
84
|
|
|
$filter = null; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
$value = getenv($key); |
88
|
|
|
if ($value !== false) { |
|
|
|
|
89
|
|
|
return static::prepareValue($value, $filter, $options); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (isset($_ENV[$key])) { |
93
|
|
|
return static::prepareValue($_ENV[$key], $filter, $options); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (isset($_SERVER[$key])) { |
97
|
|
|
return static::prepareValue($_SERVER[$key], $filter, $options); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Default is not passed through filter! |
101
|
|
|
|
102
|
|
|
return $default; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Prepare the environment value |
107
|
|
|
* @param string $value |
108
|
|
|
* @param int|null $filter See http://php.net/filter_var |
109
|
|
|
* @param array<string, mixed>|int $options |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
protected static function prepareValue( |
113
|
|
|
string $value, |
114
|
|
|
?int $filter = null, |
115
|
|
|
int|array $options = 0 |
116
|
|
|
): mixed { |
117
|
|
|
static $special = [ |
118
|
|
|
'true' => true, |
119
|
|
|
'TRUE' => true, |
120
|
|
|
'false' => false, |
121
|
|
|
'FALSE' => false, |
122
|
|
|
'null' => null, |
123
|
|
|
'NULL' => null, |
124
|
|
|
]; |
125
|
|
|
|
126
|
|
|
$valueResolved = static::resolveReference($value); |
127
|
|
|
|
128
|
|
|
// strlen($valueResolved) < 6. |
129
|
|
|
if (!isset($valueResolved[5]) && array_key_exists($valueResolved, $special)) { |
130
|
|
|
return $special[$value]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($filter === null || function_exists('filter_var') === false) { |
134
|
|
|
return $valueResolved; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return filter_var($valueResolved, $filter, $options); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Resolve variable reference like ${VAR_NAME} |
142
|
|
|
* @param string $value |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
protected static function resolveReference(string $value): string |
146
|
|
|
{ |
147
|
|
|
if (empty($value) || strpos($value, '${') === false) { |
148
|
|
|
return $value; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$valueResolved = preg_replace_callback('~\$\{(\w+)\}~', function ($m) { |
152
|
|
|
return (null === $ref = static::get($m[1], null)) ? $m[0] : $ref; |
153
|
|
|
}, $value); |
154
|
|
|
|
155
|
|
|
if ($valueResolved !== null) { |
156
|
|
|
return $valueResolved; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $value; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|