-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathdotenv.php
More file actions
64 lines (58 loc) · 1.25 KB
/
dotenv.php
File metadata and controls
64 lines (58 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
if ( ! function_exists( 'dotenv' ) ) {
/**
* Wrapper for Dotenv class object to cache it to static variable.
*
* @param string|null $config
*
* @return \Dotenv\Dotenv
* @throws Exception
*/
function dotenv( $config = null ) {
static $dotenv;
if ( ! is_null( $config ) ) {
$dotenv = Dotenv\Dotenv::createImmutable( $config );
}
if ( ! $dotenv ) {
throw new \Exception( "dotenv() helper is not initialized. Call dotenv('path to .env') first." );
}
return $dotenv;
}
}
if ( ! function_exists( 'env' ) ) {
/**
* Gets the value of an environment variable. Supports boolean, empty and null.
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
function env( $key, $default = null ) {
$value = getenv( $key );
if ( false === $value ) {
return $default;
}
switch ( strtolower( $value ) ) {
case 'true':
case '(true)':
return true;
case 'false':
case '(false)':
return false;
case 'null':
case '(null)':
return null;
case 'empty':
case '(empty)':
return '';
}
// remove double quotes from value, if wrapped.
if ( 0 === strpos( $value, '"' )
&& '"' === substr( $value, - 1 )
) {
return substr( $value, 1, - 1 );
}
return $value;
}
}