Skip to content

Commit 57cbc25

Browse files
committed
Update Configuration.php
1 parent 502b465 commit 57cbc25

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/Ubiquity/config/Configuration.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,30 @@ class Configuration {
1818
private const CONFIG_CACHE_LOCATION='cache/config/';
1919

2020
private static function addArrayToConfig(array $config, string $env): ?array {
21-
return \array_replace_recursive($config,self::loadConfig($env));
21+
return \array_replace_recursive($config, self::loadConfig($env));
2222
}
2323

2424
private static function generateConfig(): array {
2525
$app_env=self::loadActiveEnv();
2626
$config=self::loadMainConfig();
2727
$config['app.env']=$app_env;
28-
return self::addArrayToConfig($config,$app_env);
28+
return self::addArrayToConfig($config, $app_env);
2929
}
3030

31-
public static function generateCache(){
32-
return self::saveConfig(self::generateConfig(),'config.cache');
31+
public static function generateCache(bool $silent=false) {
32+
$config = self::saveConfig(self::generateConfig(),'config.cache');
33+
if (!$silent) {
34+
$folder = \basename(\ROOT.self::CONFIG_CACHE_LOCATION.'config.cache.php');
35+
echo "Config cache generated in <b>$folder</b>";
36+
}
37+
return $config;
3338
}
3439

35-
public static function loadMainConfig(){
40+
public static function loadMainConfig() {
3641
return include \ROOT.'config'.DS.'config.php';
3742
}
3843

39-
public static function loadConfigCache(){
44+
public static function loadConfigCache() {
4045
return include \ROOT.self::CONFIG_CACHE_LOCATION.'config.cache.php';
4146
}
4247

@@ -75,7 +80,7 @@ public static function getConfigFiles(): array{
7580
return UFileSystem::glob_recursive(\ROOT.'config'.\DS.'config*.php');
7681
}
7782

78-
public static function loadEnv($appEnv='dev'){
83+
public static function loadEnv($appEnv='dev'): void {
7984
$envRoot=EnvFile::$ENV_ROOT;
8085
EnvFile::load($envRoot,".env.$appEnv");
8186
EnvFile::load($envRoot,".env.$appEnv.local");
@@ -94,7 +99,7 @@ public static function getTheoreticalLoadedConfigFiles($appEnv):array{
9499
}
95100

96101
public static function loadConfigWithoutEval(string $filename='config'): array{
97-
if(file_exists(\ROOT."config/$filename.php")) {
102+
if (file_exists(\ROOT."config/$filename.php")) {
98103
$origContent = \file_get_contents(\ROOT . "config/$filename.php");
99104
$result = \preg_replace('/getenv\(\'(.*?)\'\)/', '"getenv(\'$1\')"', $origContent);
100105
$result = \preg_replace('/getenv\(\"(.*?)\"\)/', "'getenv(\"\$1\")'", $result);
@@ -107,15 +112,18 @@ public static function loadConfigWithoutEval(string $filename='config'): array{
107112
return [];
108113
}
109114

110-
public static function saveConfig(array $contentArray,string $configFilename='config') {
115+
/**
116+
* @throws InvalidCodeException
117+
*/
118+
public static function saveConfig(array $contentArray, string $configFilename='config') {
111119
$filename = \ROOT .static::CONFIG_CACHE_LOCATION. "$configFilename.php";
112120
$dir=\dirname($filename);
113121
UFileSystem::safeMkdir($dir);
114-
$content = "<?php\nreturn " . UArray::asPhpArray ( $contentArray, 'array', 1, true ) . ";";
115-
if (CodeUtils::isValidCode ( $content )) {
116-
return UFileSystem::save ( $filename, $content );
122+
$content = "<?php\nreturn " . UArray::asPhpArray($contentArray, 'array', 1, true) . ";";
123+
if (CodeUtils::isValidCode($content)) {
124+
return UFileSystem::save($filename, $content);
117125
}
118-
throw new InvalidCodeException ( 'Config contains invalid code' );
126+
throw new InvalidCodeException('Config contains invalid code');
119127
}
120128

121129
}

0 commit comments

Comments
 (0)