NanoFramework 1.4.0
A tiny PHP Framework
DotEnv.php
Go to the documentation of this file.
1<?php
2
4
10abstract 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 $dotEnvContent = file_get_contents(self::$root.'.env');
56 $entries = explode(PHP_EOL, $dotEnvContent);
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}
Manages constants.
Definition: DotEnv.php:11
static getEnv(string $key)
Definition: DotEnv.php:74
static setEnv(string $key, string $value)
Definition: DotEnv.php:89
static setRoot(string $root)
Definition: DotEnv.php:27