Test Failed
Push — master ( ffc597...4abc3b )
by Julien
12:58 queued 09:01
created

Env::getCurrentPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 5
c 1
b 1
f 0
dl 0
loc 11
ccs 4
cts 6
cp 0.6667
rs 10
cc 3
nc 3
nop 0
crap 3.3332
1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Utils;
12
13
use Dotenv\Dotenv;
14
15
/**
16
 * Class Env
17
 * Allow to access environment variable easily
18
 *
19
 * Example usage:
20
 * ```php
21
 * $this->SET_APPLICATION_ENV('production'); // SET APPLICATION_ENV to 'production'
22
 * self::SET_APPLICATION_ENV('production'); // SET APPLICATION_ENV to 'production'
23
 * $this->GET_APPLICATION_ENV('default'); // GET APPLICATION_ENV value or 'default'
24
 * self::GET_APPLICATION_ENV('default'); // GET APPLICATION_ENV value or 'default'
25
 * ```
26
 *
27
 * @todo fix dotenv mandatory file
28
 * @author Julien Turbide <[email protected]>
29
 * @copyright Zemit Team <[email protected]>
30
 *
31
 * @since 1.0
32
 * @version 1.0
33
 *
34
 * @package Zemit\Utils
35
 */
36
class Env
37
{
38
    /**
39
     * @var array
40
     */
41
    public static $vars;
42
    
43
    /**
44
     * Dotenv loader to manage the environment varialbe
45
     * @var \Dotenv\Loader
46
     */
47
    public static $dotenv;
48
    
49
    /**
50
     * Get the dotenv loader
51
     * @return \Dotenv\Loader
52
     */
53 4
    public static function getDotenv(array $filePath = null)
54
    {
55 4
        $filePath ??= self::getCurrentPath();
56 4
        $envFilePath = $filePath . '/.env';
57
        
58 4
        if (is_readable($envFilePath)) {
59
            self::$dotenv ??= Dotenv::create($filePath);
0 ignored issues
show
Bug introduced by
It seems like $filePath can also be of type false; however, parameter $paths of Dotenv\Dotenv::create() does only seem to accept string|string[], maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
            self::$dotenv ??= Dotenv::create(/** @scrutinizer ignore-type */ $filePath);
Loading history...
60
            self::$vars ??= self::$dotenv->load();
61
        }
62
    
63 4
        self::$vars ??= getenv();
64 4
        return self::$dotenv;
65
    }
66
    
67
    /**
68
     * @return false|mixed|string
69
     */
70 4
    public static function getCurrentPath() {
71
        
72 4
        if (!empty($_SERVER['DOCUMENT_ROOT'])) {
73
            return dirname($_SERVER['DOCUMENT_ROOT']);
74
        }
75
        
76 4
        if (defined('ENV_PATH')) {
77
            return constant('ENV_PATH');
78
        }
79
        
80 4
        return getcwd();
81
    }
82
    
83
    /**
84
     * Get or set the environment variable
85
     * Ex. (SET): $this->SET_APPLICATION_ENV('production');
86
     * Ex. (SET): self::SET_APPLICATION_ENV('production');
87
     * Ex. (SET): Env::SET_APPLICATION_ENV('production');
88
     * Ex. (GET): $this->GET_APPLICATION_ENV('development');
89
     * Ex. (GET): self::GET_APPLICATION_ENV('development');
90
     * Ex. (GET): Env::GET_APPLICATION_ENV('development');
91
     * @param string $name key to get/set
92
     * @param mixed $arguments Default fallback value for get, or value to set for set
93
     * @return mixed Return void for set, and return the environment variable value, or default value for get
94
     */
95
    public static function call($name, $arguments)
96
    {
97
        $ret = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
98
        $getSet = 'set';
99
        if (strpos($name, 'SET_') === 0) {
100
            $name = substr($name, 0, 4);
101
        } elseif (strpos($name, 'set') === 0) {
102
            $name = substr($name, 0, 3);
103
        } elseif (strpos($name, 'GET_') === 0) {
104
            $getSet = 'get';
105
            $name = substr($name, 0, 4);
106
        } elseif (strpos($name, 'get') === 0) {
107
            $getSet = 'get';
108
            $name = substr($name, 0, 3);
109
        }
110
        return self::$getSet($name, array_pop($arguments));
111
    }
112
    
113
    /**
114
     * Gets the value of an environment variable. Passe the $default for fallback value.
115
     * @param string $key Key to get
116
     * @param mixed $default Value to fallback if the key can't be found
117
     * @return mixed Return the environment variable value or the default value
118
     */
119 4
    public static function get($key, $default = null)
120
    {
121 4
        self::getDotenv();
122
        
123 4
        $ret = self::$vars[$key] ?? null;
124 4
        $ret ??= ($default instanceof Closure) ? $default() : $default;
0 ignored issues
show
Bug introduced by
The type Zemit\Utils\Closure was not found. Did you mean Closure? If so, make sure to prefix the type with \.
Loading history...
125
        
126 4
        if (is_string($ret)) {
127 4
            switch (strtolower($ret)) {
128 4
                case 'true':
129
                    $ret = true;
130
                    break;
131 4
                case 'false':
132
                    $ret = false;
133
                    break;
134 4
                case 'empty':
135
                    $ret = '';
136
                    break;
137 4
                case 'null':
138
                    $ret = null;
139
                    break;
140
            }
141
        }
142
    
143 4
        return $ret;
144
    }
145
    
146
    /**
147
     * Set an environment variable
148
     * @param $key string Key to set
149
     * @param $value mixed Value to set
150
     */
151
    public static function set($key, $value)
152
    {
153
        $dotenv = self::getDotenv();
154
        $dotenv? $dotenv->setEnvironmentVariable($key, $value) : $_ENV[$key] = $value;
0 ignored issues
show
introduced by
$dotenv is of type Dotenv\Loader, thus it always evaluated to true.
Loading history...
155
    }
156
    
157
    /**
158
     * Return the environnement variable
159
     * @param $name Env key to fetch
160
     * @return mixed Env value
161
     */
162
    public function __get($name)
163
    {
164
        return self::get($name);
165
    }
166
    
167
    /**
168
     * Set the environnement variable
169
     * @param $name string Env key to set
170
     * @param $value mixed Value to set
171
     */
172
    public function __set(string $name, $value)
173
    {
174
        self::set($name, $value);
175
    }
176
    
177
    /**
178
     * Get or set the environment variable
179
     * Ex. (SET): self::SET_APPLICATION_ENV('production');
180
     * Ex. (SET): Env::SET_APPLICATION_ENV('production');
181
     * Ex. (GET): self::GET_APPLICATION_ENV('development');
182
     * Ex. (GET): Env::GET_APPLICATION_ENV('development');
183
     * @param $name string Key to get/set
184
     * @param $arguments array Default fallback value for get, or value to set for set
185
     * @return mixed Return void for set, and return the environment variable value, or default value for get
186
     */
187
    public static function __callStatic(string $name, array $arguments)
188
    {
189
        return self::call($name, $arguments);
190
    }
191
    
192
    /**
193
     * Get or set the environment variable
194
     * Ex. (SET): $this->SET_APPLICATION_ENV('production');
195
     * Ex. (GET): $this->GET_APPLICATION_ENV('development');
196
     * @param $name string Key to get/set
197
     * @param $arguments array Default fallback value for get, or value to set for set
198
     * @return mixed Return void for set, and return the environment variable value, or default value for get
199
     */
200
    public function __call(string $name, array $arguments)
201
    {
202
        return self::call($name, $arguments);
203
    }
204
}
205