NanoFramework
1.5.2
A tiny PHP Framework
Loading...
Searching...
No Matches
lib
DotEnv.php
Go to the documentation of this file.
1
<?php
2
3
namespace
SteeveDroz\NanoFramework;
4
10
abstract
class
DotEnv
11
{
15
protected
static
$root
=
'./'
;
16
20
protected
static
array
$data
= [];
21
27
public
static
function
setRoot
(
string
$root
): void
28
{
29
if
(
'/'
!= substr(
$root
, -1)) {
30
$root
.=
'/'
;
31
}
32
self::$root =
$root
;
33
}
34
40
public
static
function
getRoot
(): string
41
{
42
return
self::$root;
43
}
44
48
public
static
function
load
(): void
49
{
50
self::$data = [];
51
if
(!file_exists(self::$root .
'.env'
)) {
52
return
;
53
}
54
55
$entries = file(self::$root .
'.env'
, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
56
57
foreach
($entries as $line) {
58
$line = preg_replace(
'/#.*$/'
,
''
, $line);
59
$elements = [];
60
preg_match(
'/^\s*(\w+)\s*=\s*(.*)\s*$/'
, $line, $elements);
61
if
(count($elements) > 2) {
62
self::$data[$elements[1]] = trim($elements[2]);
63
}
64
}
65
}
66
74
public
static
function
getEnv
(
string
$key): ?string
75
{
76
if
(0 == count(self::$data)) {
77
self::load
();
78
}
79
80
return
self::$data[$key] ??
null
;
81
}
82
89
public
static
function
setEnv
(
string
$key,
string
$value): void
90
{
91
self::$data[$key] = $value;
92
}
93
}
SteeveDroz\NanoFramework\DotEnv
Manages constants.
Definition
DotEnv.php:11
SteeveDroz\NanoFramework\DotEnv\$data
static array $data
Definition
DotEnv.php:20
SteeveDroz\NanoFramework\DotEnv\getRoot
static getRoot()
Definition
DotEnv.php:40
SteeveDroz\NanoFramework\DotEnv\getEnv
static getEnv(string $key)
Definition
DotEnv.php:74
SteeveDroz\NanoFramework\DotEnv\load
static load()
Definition
DotEnv.php:48
SteeveDroz\NanoFramework\DotEnv\setEnv
static setEnv(string $key, string $value)
Definition
DotEnv.php:89
SteeveDroz\NanoFramework\DotEnv\$root
static $root
Definition
DotEnv.php:15
SteeveDroz\NanoFramework\DotEnv\setRoot
static setRoot(string $root)
Definition
DotEnv.php:27
Generated by
1.13.2