Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
Configuration.php
Go to the documentation of this file.
1<?php
2
3
5
9use Ubiquity\utils\base\UArray;
11
16class Configuration {
17
18 private const CONFIG_CACHE_LOCATION='cache/config/';
19
20 private static function addArrayToConfig(array $config, string $env): ?array {
21 return \array_replace_recursive($config, self::loadConfig($env));
22 }
23
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);
29 }
30
31 public static function generateCache(bool $silent=false) {
32 $config = self::saveConfig(self::generateConfig(),'config.cache');
33 if (!$silent) {
34 $folder = \realpath(\ROOT.self::CONFIG_CACHE_LOCATION.'config.cache.php');
35 echo "Config cache generated in <b>$folder</b>";
36 }
37 return $config;
38 }
39
40 public static function loadMainConfig() {
41 return include \ROOT.'config'.DS.'config.php';
42 }
43
44 public static function loadConfigCache() {
45 return include \ROOT.self::CONFIG_CACHE_LOCATION.'config.cache.php';
46 }
47
48 public static function loadConfig($env): array {
49 $filename=\ROOT.'config'.\DS.'config-'.$env.'.php';
50 if(\file_exists($filename)){
51 return include $filename;
52 }
53 return [];
54 }
55
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);
62 return $app_env;
63 }
64
65 public static function hasEnvChanged(): bool {
66 return Framework::getEnv()!==self::loadActiveEnv();
67 }
68
69 public static function isConfigUpdated(): bool {
70 $cachedConfig=self::loadConfigCache();
71 $newConfig=self::generateConfig();
72 return UArray::asPhpArray($cachedConfig)!=UArray::asPhpArray($newConfig);
73 }
74
75 public static function getEnvFiles(): array{
76 return UFileSystem::glob_recursive(EnvFile::$ENV_ROOT.'.env*');
77 }
78
79 public static function getConfigFiles(): array{
80 return UFileSystem::glob_recursive(\ROOT.'config'.\DS.'config*.php');
81 }
82
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");
87 }
88
89 public static function getTheoreticalLoadedConfigFiles($appEnv):array{
90 $envRoot=EnvFile::$ENV_ROOT;
91 return \array_map('realpath',[
92 $envRoot.'.env',
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'
98 ]);
99 }
100
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;
109 }
110 return self::loadMainConfig();
111 }
112 return [];
113 }
114
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);
125 }
126 throw new InvalidCodeException('Config contains invalid code');
127 }
128
129}
Ubiquity\utils\base$CodeUtils This class is part of Ubiquity.
Definition CodeUtils.php:13
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.