@@ -18,25 +18,30 @@ class Configuration {
18
18
private const CONFIG_CACHE_LOCATION ='cache/config/ ' ;
19
19
20
20
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 ));
22
22
}
23
23
24
24
private static function generateConfig (): array {
25
25
$ app_env =self ::loadActiveEnv ();
26
26
$ config =self ::loadMainConfig ();
27
27
$ config ['app.env ' ]=$ app_env ;
28
- return self ::addArrayToConfig ($ config ,$ app_env );
28
+ return self ::addArrayToConfig ($ config , $ app_env );
29
29
}
30
30
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 ;
33
38
}
34
39
35
- public static function loadMainConfig (){
40
+ public static function loadMainConfig () {
36
41
return include \ROOT .'config ' .DS .'config.php ' ;
37
42
}
38
43
39
- public static function loadConfigCache (){
44
+ public static function loadConfigCache () {
40
45
return include \ROOT .self ::CONFIG_CACHE_LOCATION .'config.cache.php ' ;
41
46
}
42
47
@@ -75,7 +80,7 @@ public static function getConfigFiles(): array{
75
80
return UFileSystem::glob_recursive (\ROOT .'config ' .\DS .'config*.php ' );
76
81
}
77
82
78
- public static function loadEnv ($ appEnv ='dev ' ){
83
+ public static function loadEnv ($ appEnv ='dev ' ): void {
79
84
$ envRoot =EnvFile::$ ENV_ROOT ;
80
85
EnvFile::load ($ envRoot ,".env. $ appEnv " );
81
86
EnvFile::load ($ envRoot ,".env. $ appEnv.local " );
@@ -94,7 +99,7 @@ public static function getTheoreticalLoadedConfigFiles($appEnv):array{
94
99
}
95
100
96
101
public static function loadConfigWithoutEval (string $ filename ='config ' ): array {
97
- if (file_exists (\ROOT ."config/ $ filename.php " )) {
102
+ if (file_exists (\ROOT ."config/ $ filename.php " )) {
98
103
$ origContent = \file_get_contents (\ROOT . "config/ $ filename.php " );
99
104
$ result = \preg_replace ('/getenv\( \'(.*?) \'\)/ ' , '"getenv( \'$1 \')" ' , $ origContent );
100
105
$ result = \preg_replace ('/getenv\(\"(.*?)\"\)/ ' , "'getenv( \"\$1 \")' " , $ result );
@@ -107,15 +112,18 @@ public static function loadConfigWithoutEval(string $filename='config'): array{
107
112
return [];
108
113
}
109
114
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 ' ) {
111
119
$ filename = \ROOT .static ::CONFIG_CACHE_LOCATION . "$ configFilename.php " ;
112
120
$ dir =\dirname ($ filename );
113
121
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 );
117
125
}
118
- throw new InvalidCodeException ( 'Config contains invalid code ' );
126
+ throw new InvalidCodeException ( 'Config contains invalid code ' );
119
127
}
120
128
121
129
}
0 commit comments