|
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 http://www.iacademy.cf |
|
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
|
|
|
/** |
|
59
|
|
|
* Get the environment variable by its key/name. |
|
60
|
|
|
* @param string $key |
|
61
|
|
|
* @param mixed $default |
|
62
|
|
|
* @param string|null $filter |
|
63
|
|
|
* @param int|array<string, mixed> $options Additional options to filter. |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function get( |
|
67
|
|
|
string $key, |
|
68
|
|
|
$default = null, |
|
69
|
|
|
?string $filter = null, |
|
70
|
|
|
$options = 0 |
|
71
|
|
|
) { |
|
72
|
|
|
if ($filter !== null) { |
|
73
|
|
|
$filterMaps = [ |
|
74
|
|
|
'bool' => FILTER_VALIDATE_BOOLEAN, |
|
75
|
|
|
'int' => FILTER_VALIDATE_INT, |
|
76
|
|
|
'float' => FILTER_VALIDATE_FLOAT, |
|
77
|
|
|
'email' => FILTER_VALIDATE_EMAIL, |
|
78
|
|
|
'ip' => FILTER_VALIDATE_IP, |
|
79
|
|
|
'url' => FILTER_VALIDATE_URL, |
|
80
|
|
|
]; |
|
81
|
|
|
|
|
82
|
|
|
if (isset($filterMaps[$filter])) { |
|
83
|
|
|
$filter = $filterMaps[$filter]; |
|
84
|
|
|
} else { |
|
85
|
|
|
$filter = null; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
$value = getenv($key); |
|
89
|
|
|
if ($value !== false) { |
|
|
|
|
|
|
90
|
|
|
return static::prepareValue($value, $filter, $options); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (isset($_ENV[$key])) { |
|
94
|
|
|
return static::prepareValue($_ENV[$key], $filter, $options); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (isset($_SERVER[$key])) { |
|
98
|
|
|
return static::prepareValue($_SERVER[$key], $filter, $options); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// Default is not passed through filter! |
|
102
|
|
|
|
|
103
|
|
|
return $default; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Prepare the environment value |
|
108
|
|
|
* @param string $value |
|
109
|
|
|
* @param int|null $filter See http://php.net/filter_var |
|
110
|
|
|
* @param array<string, mixed>|int $options |
|
111
|
|
|
* @return mixed |
|
112
|
|
|
*/ |
|
113
|
|
|
protected static function prepareValue( |
|
114
|
|
|
string $value, |
|
115
|
|
|
?int $filter = null, |
|
116
|
|
|
$options = 0 |
|
117
|
|
|
) { |
|
118
|
|
|
static $special = [ |
|
119
|
|
|
'true' => true, |
|
120
|
|
|
'TRUE' => true, |
|
121
|
|
|
'false' => false, |
|
122
|
|
|
'FALSE' => false, |
|
123
|
|
|
'null' => null, |
|
124
|
|
|
'NULL' => null, |
|
125
|
|
|
]; |
|
126
|
|
|
|
|
127
|
|
|
// strlen($value) < 6. |
|
128
|
|
|
if (!isset($value[5]) && array_key_exists($value, $special)) { |
|
129
|
|
|
return $special[$value]; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ($filter === null || !function_exists('filter_var')) { |
|
133
|
|
|
return $value; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return filter_var($value, $filter, $options); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|