Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
EnvFile.php
Go to the documentation of this file.
1<?php
2
3
4namespace Ubiquity\config;
5
6
7use Dotenv\Dotenv;
10
15class EnvFile {
16
17 public static string $ENV_ROOT=\ROOT.'../';
18
23 private static function parseValue($v) {
24 if (\is_numeric($v)) {
25 $result = $v;
26 } elseif ($v !== '' && UString::isBooleanStr($v)) {
27 $result = UString::getBooleanStr($v);
28 }else{
29 $result='"'.$v.'"';
30 }
31 return $result;
32 }
33
42 public static function save(array $content,?string $path=null, string $filename='.env') {
43 $result=[];
44 $path??=self::$ENV_ROOT;
45 foreach ($content as $k=>$v){
46 $result[]=$k.'='.self::parseValue($v);
47 }
48 $result= \implode("\n",$result);
49 return UFileSystem::save($path.$filename,$result);
50 }
51
60 public static function saveText(string $textContent,?string $path=null, string $filename='.env') {
61 $path??=self::$ENV_ROOT;
62 return UFileSystem::save($path.$filename,$textContent);
63 }
64
73 public static function addAndSave(array $content,?string $path=null, string $filename='.env') {
74 $path??=self::$ENV_ROOT;
75 $result=self::load($path,$filename);
76 $result=\array_replace_recursive($result,$content);
77 return self::save($result,$path,$filename);
78 }
79
87 public static function load(?string $path=null, string $filename='.env'): array {
88 $path??=self::$ENV_ROOT;
89 if(\file_exists($path.$filename)) {
90 return Dotenv::createUnsafeMutable($path,$filename)->load();
91 }
92 return [];
93 }
94
95 public static function loadContent(?string $path=null, string $filename='.env'): string {
96 $path??=self::$ENV_ROOT;
97 if(\file_exists($path.$filename)) {
98 return \file_get_contents($path.$filename);
99 }
100 return '';
101 }
102
103}
static saveText(string $textContent,?string $path=null, string $filename='.env')
Savec a content text on disk.
Definition EnvFile.php:60
static string $ENV_ROOT
Definition EnvFile.php:17
static parseValue($v)
Definition EnvFile.php:23
static load(?string $path=null, string $filename='.env')
Loads an env file an returns an array of key/value pairs.
Definition EnvFile.php:87
static addAndSave(array $content,?string $path=null, string $filename='.env')
Adds a content array to an existing env file and saves it to disk.
Definition EnvFile.php:73
static save(array $content,?string $path=null, string $filename='.env')
Saves a content array on disk.
Definition EnvFile.php:42
static loadContent(?string $path=null, string $filename='.env')
Definition EnvFile.php:95
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.
String utilities.
Definition UString.php:15