9use Ubiquity\utils\base\UArray;
18 private const CONFIG_CACHE_LOCATION=
'cache/config/';
20 private static function addArrayToConfig(array $config,
string $env): ?array {
21 return \array_replace_recursive($config, self::loadConfig($env));
24 private static function generateConfig(): array {
25 $app_env=self::loadActiveEnv();
26 $config=self::loadMainConfig();
27 $config[
'app.env']=$app_env;
28 return self::addArrayToConfig($config, $app_env);
31 public static function generateCache(
bool $silent=
false) {
32 $config = self::saveConfig(self::generateConfig(),
'config.cache');
34 $folder = \realpath(\ROOT.self::CONFIG_CACHE_LOCATION.
'config.cache.php');
35 echo
"Config cache generated in <b>$folder</b>";
40 public static function loadMainConfig() {
41 return include \ROOT.
'config'.DS.
'config.php';
44 public static function loadConfigCache() {
45 return include \ROOT.self::CONFIG_CACHE_LOCATION.
'config.cache.php';
48 public static function loadConfig($env): array {
49 $filename=\ROOT.
'config'.\DS.
'config-'.$env.
'.php';
50 if(\file_exists($filename)){
51 return include $filename;
56 public static function loadActiveEnv(): string {
57 $envRoot=EnvFile::$ENV_ROOT;
58 EnvFile::load($envRoot);
59 EnvFile::load($envRoot,
'.env.local');
60 $app_env=$_ENV[
'APP_ENV']??
'dev';
61 self::loadEnv($app_env);
65 public static function hasEnvChanged(): bool {
66 return Framework::getEnv()!==self::loadActiveEnv();
69 public static function isConfigUpdated(): bool {
70 $cachedConfig=self::loadConfigCache();
71 $newConfig=self::generateConfig();
72 return UArray::asPhpArray($cachedConfig)!=UArray::asPhpArray($newConfig);
75 public static function getEnvFiles(): array{
76 return UFileSystem::glob_recursive(EnvFile::$ENV_ROOT.
'.env*');
79 public static function getConfigFiles(): array{
80 return UFileSystem::glob_recursive(\ROOT.
'config'.\DS.
'config*.php');
83 public static function loadEnv($appEnv=
'dev'): void {
84 $envRoot=EnvFile::$ENV_ROOT;
85 EnvFile::load($envRoot,
".env.$appEnv");
86 EnvFile::load($envRoot,
".env.$appEnv.local");
89 public static function getTheoreticalLoadedConfigFiles($appEnv):array{
90 $envRoot=EnvFile::$ENV_ROOT;
91 return \array_map(
'realpath',[
93 $envRoot.
'.env.local',
94 $envRoot.
'.env.'.$appEnv,
95 $envRoot.
'.env.'.$appEnv.
'.local',
96 \ROOT.
'config'.DS.
'config.php',
97 \ROOT.
'config'.\DS.
'config-'.$appEnv.
'.php'
101 public static function loadConfigWithoutEval(
string $filename=
'config'): array{
102 if (file_exists(\ROOT.
"config/$filename.php")) {
103 $origContent = \file_get_contents(\ROOT .
"config/$filename.php");
104 $result = \preg_replace(
'/getenv\(\'(.*?)\'\)/',
'"getenv(\'$1\')"', $origContent);
105 $result = \preg_replace(
'/getenv\(\"(.*?)\"\)/',
"'getenv(\"\$1\")'", $result);
106 $tmpFilename = \ROOT .
'cache/config/tmp.cache.php';
107 if (\file_put_contents($tmpFilename, $result)) {
108 return include $tmpFilename;
110 return self::loadMainConfig();
118 public static function saveConfig(array $contentArray,
string $configFilename=
'config') {
119 $filename = \ROOT .static::CONFIG_CACHE_LOCATION.
"$configFilename.php";
120 $dir=\dirname($filename);
121 UFileSystem::safeMkdir($dir);
122 $content =
"<?php\nreturn " . UArray::asPhpArray($contentArray,
'array', 1,
true) .
";";
123 if (CodeUtils::isValidCode($content)) {
124 return UFileSystem::save($filename, $content);
126 throw new InvalidCodeException(
'Config contains invalid code');
Exceptions for code checking.
Ubiquity\utils\base$CodeUtils This class is part of Ubiquity.
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.