33namespace Configurator ;
44
55
6- class Configurator {
7-
6+ class Configurator
7+ {
88 private $ config = array ();
99 private $ configDefault = array ();
1010 private $ configEnvironment = array ();
@@ -23,49 +23,70 @@ class Configurator {
2323 /**
2424 * @var
2525 */
26- private $ environment ;
27-
28- public function run ($ input ,
29- $ output ,
30- $ environment ,
31- $ jssettings = [],
32- $ phpsettings = []) {
33-
26+ private $ environmentList ;
27+
28+ private $ jssettings ;
29+
30+ private $ phpsettings ;
31+
32+ public function __construct (
33+ $ input ,
34+ $ output ,
35+ $ environment ,
36+ $ jssettings = [],
37+ $ phpsettings = []
38+ ) {
3439 if (count ($ jssettings ) == 0 && count ($ phpsettings ) == 0 ) {
3540 throw new ConfiguratorException ("One of PHP setting or JS settings must be set. " );
3641 }
3742
38- $ this ->environment = $ environment ;
3943 $ this ->inputFilename = $ input ;
4044 $ this ->outputFilename = $ output ;
45+ $ this ->environmentList = explode (', ' , $ environment );
46+ $ this ->jssettings = $ jssettings ;
47+ $ this ->phpsettings = $ phpsettings ;
4148
42- if ($ phpsettings !== null ) {
43- $ phpsettings = explode (', ' , $ phpsettings );
49+ if ($ this -> phpsettings !== null ) {
50+ $ phpsettings = explode (', ' , $ this -> phpsettings );
4451 foreach ($ phpsettings as $ phpSetting ) {
45- $ this ->addPHPConfig ($ environment , $ phpSetting );
52+ $ this ->addPHPConfig ($ phpSetting );
4653 }
4754 }
4855
49- if ($ jssettings !== null ) {
50- $ jssettings = explode (', ' , $ jssettings );
56+ if ($ this -> jssettings !== null ) {
57+ $ jssettings = explode (', ' , $ this -> jssettings );
5158 foreach ($ jssettings as $ jsSetting ) {
52- $ this ->addJSConfig ($ environment , $ jsSetting );
59+ $ this ->addJSConfig ($ jsSetting );
5360 }
5461 }
62+ }
5563
64+ public function writeConfigFile ()
65+ {
5666 $ config = $ this ->configurate ($ this ->inputFilename );
5767 $ written = @file_put_contents ($ this ->outputFilename , $ config );
5868 if (!$ written ) {
5969 throw new \Exception ("Failed to write config to file ` $ this ->outputFilename ` " );
6070 }
6171 }
6272
73+
74+ public function writeEnvironmentFile ()
75+ {
76+ $ output = $ this ->genEnvironmentFile ();
77+ $ written = @file_put_contents ($ this ->outputFilename , $ output );
78+ if (!$ written ) {
79+ throw new \Exception ("Failed to write config to file ` $ this ->outputFilename ` " );
80+ }
81+ }
82+
6383
6484 /**
6585 * @param $environment
6686 * @param $filename
6787 */
68- function addJSConfig ($ environment , $ filename ) {
88+ function addJSConfig ($ filename )
89+ {
6990 $ contents = @file_get_contents ($ filename );
7091 if ($ contents == false ) {
7192 throw new ConfiguratorException ("Could not read file $ filename. " );
@@ -81,8 +102,10 @@ function addJSConfig($environment, $filename) {
81102 $ this ->addConfigDefault ($ data ['default ' ]);
82103 }
83104
84- if (array_key_exists ($ environment , $ data ) == true ) {
85- $ this ->addConfigEnvironment ($ data [$ environment ]);
105+ foreach ($ this ->environmentList as $ environment ) {
106+ if (array_key_exists ($ environment , $ data ) == true ) {
107+ $ this ->addConfigEnvironment ($ data [$ environment ]);
108+ }
86109 }
87110
88111 if (array_key_exists ('override ' , $ data ) == true ) {
@@ -108,14 +131,14 @@ private function addConfigOverride($data) {
108131 * @param $environment
109132 * @param $filename
110133 */
111- public function addPHPConfig ($ environment , $ filename ) {
134+ public function addPHPConfig ($ filename )
135+ {
112136 ob_start ();
113137 require ($ filename );
114138 $ contents = ob_get_contents ();
115139 ob_end_clean ();
116140
117141 if (strlen ($ contents ) != 0 ) {
118-
119142 $ message = sprintf (
120143 "Filename `%s` output some characters. Please check it is a valid PHP file. \n" ,
121144 $ filename
@@ -128,8 +151,10 @@ public function addPHPConfig($environment, $filename) {
128151 $ this ->addConfigDefault ($ default );
129152 }
130153
131- if (isset ($ $ environment ) == true ) {
132- $ this ->addConfigEnvironment ($ $ environment );
154+ foreach ($ this ->environmentList as $ environment ) {
155+ if (isset ($ $ environment ) == true ) {
156+ $ this ->addConfigEnvironment ($ $ environment );
157+ }
133158 }
134159
135160 if (isset ($ override ) == true ) {
@@ -187,6 +212,42 @@ public function configurate($inputFilename) {
187212 return $ configuration ;
188213 }
189214
215+
216+ public function genEnvironmentFile ()
217+ {
218+ $ config = $ this ->getConfig ();
219+
220+ $ envRequired = require $ this ->inputFilename ;
221+
222+ if (is_array ($ envRequired ) == false ) {
223+ throw new ConfiguratorException ("Failed to get array from " .$ this ->inputFilename );
224+ }
225+
226+ $ envOutput = "<?php \n" ;
227+ $ envOutput .= "\n" ;
228+
229+ $ envOutput .= "function getAppEnv() { \n" ;
230+ $ envOutput .= " static \$env = [ \n" ;
231+
232+ foreach ($ envRequired as $ env ) {
233+ if (array_key_exists ($ env , $ config ) == false ) {
234+ throw new ConfiguratorException ("App needs $ env but not found in config " );
235+ }
236+
237+ $ envOutput .= sprintf (
238+ " '%s' => '%s', \n" ,
239+ $ env ,
240+ $ config [$ env ]
241+ );
242+ }
243+ $ envOutput .= " ]; \n" ;
244+ $ envOutput .= "\n" ;
245+ $ envOutput .= " return \$env; \n" ;
246+ $ envOutput .= "} \n" ;
247+
248+ return $ envOutput ;
249+ }
250+
190251 public function getConfig ()
191252 {
192253 $ config = $ this ->config ;
0 commit comments