@@ -193,16 +193,6 @@ public function create( $_, $assoc_args ) {
193193 }
194194 }
195195
196- $ defaults = [
197- 'dbhost ' => 'localhost ' ,
198- 'dbpass ' => '' ,
199- 'dbprefix ' => 'wp_ ' ,
200- 'dbcharset ' => 'utf8 ' ,
201- 'dbcollate ' => '' ,
202- 'locale ' => self ::get_initial_locale (),
203- 'config-file ' => rtrim ( ABSPATH , '/ \\' ) . '/wp-config.php ' ,
204- ];
205- $ assoc_args = array_merge ( $ defaults , $ assoc_args );
206196 if ( empty ( $ assoc_args ['dbprefix ' ] ) ) {
207197 WP_CLI ::error ( '--dbprefix cannot be empty ' );
208198 }
@@ -239,52 +229,131 @@ public function create( $_, $assoc_args ) {
239229 // phpcs:enable WordPress.DB.RestrictedFunctions
240230 }
241231
232+ $ defaults = [
233+ 'dbhost ' => 'localhost ' ,
234+ 'dbpass ' => '' ,
235+ 'dbprefix ' => 'wp_ ' ,
236+ 'dbcharset ' => 'utf8 ' ,
237+ 'dbcollate ' => '' ,
238+ 'locale ' => self ::get_initial_locale (),
239+ 'config-file ' => rtrim ( ABSPATH , '/ \\' ) . '/wp-config.php ' ,
240+ ];
241+
242242 if ( ! Utils \get_flag_value ( $ assoc_args , 'skip-salts ' ) ) {
243243 try {
244- $ assoc_args ['keys-and-salts ' ] = true ;
245- $ assoc_args ['auth-key ' ] = self ::unique_key ();
246- $ assoc_args ['secure-auth-key ' ] = self ::unique_key ();
247- $ assoc_args ['logged-in-key ' ] = self ::unique_key ();
248- $ assoc_args ['nonce-key ' ] = self ::unique_key ();
249- $ assoc_args ['auth-salt ' ] = self ::unique_key ();
250- $ assoc_args ['secure-auth-salt ' ] = self ::unique_key ();
251- $ assoc_args ['logged-in-salt ' ] = self ::unique_key ();
252- $ assoc_args ['nonce-salt ' ] = self ::unique_key ();
253- $ assoc_args ['wp-cache-key-salt ' ] = self ::unique_key ();
244+ $ defaults ['keys-and-salts ' ] = true ;
245+ $ defaults ['auth-key ' ] = self ::unique_key ();
246+ $ defaults ['secure-auth-key ' ] = self ::unique_key ();
247+ $ defaults ['logged-in-key ' ] = self ::unique_key ();
248+ $ defaults ['nonce-key ' ] = self ::unique_key ();
249+ $ defaults ['auth-salt ' ] = self ::unique_key ();
250+ $ defaults ['secure-auth-salt ' ] = self ::unique_key ();
251+ $ defaults ['logged-in-salt ' ] = self ::unique_key ();
252+ $ defaults ['nonce-salt ' ] = self ::unique_key ();
253+ $ defaults ['wp-cache-key-salt ' ] = self ::unique_key ();
254254 } catch ( Exception $ e ) {
255- $ assoc_args ['keys-and-salts ' ] = false ;
256- $ assoc_args ['keys-and-salts-alt ' ] = self ::fetch_remote_salts (
255+ $ defaults ['keys-and-salts ' ] = false ;
256+ $ defaults ['keys-and-salts-alt ' ] = self ::fetch_remote_salts (
257257 (bool ) Utils \get_flag_value ( $ assoc_args , 'insecure ' , false )
258258 );
259259 }
260260 }
261261
262262 if ( Utils \wp_version_compare ( '4.0 ' , '< ' ) ) {
263- $ assoc_args ['add-wplang ' ] = true ;
263+ $ defaults ['add-wplang ' ] = true ;
264264 } else {
265- $ assoc_args ['add-wplang ' ] = false ;
265+ $ defaults ['add-wplang ' ] = false ;
266266 }
267267
268- foreach ( $ assoc_args as $ key => $ value ) {
269- $ assoc_args [ $ key ] = $ this ->escape_config_value ( $ key , $ value );
268+ $ path = $ defaults ['config-file ' ];
269+ if ( ! empty ( $ assoc_args ['config-file ' ] ) ) {
270+ $ path = $ assoc_args ['config-file ' ];
270271 }
271272
273+ if ( ! empty ( $ assoc_args ['extra-php ' ] ) ) {
274+ $ defaults ['extra-php ' ] = $ this ->escape_config_value ( 'extra-php ' , $ assoc_args ['extra-php ' ] );
275+ }
276+
277+ $ command_root = Utils \phar_safe_path ( dirname ( __DIR__ ) );
278+ $ out = Utils \mustache_render ( "{$ command_root }/templates/wp-config.mustache " , $ defaults );
279+
280+ // Output the default config file at path specified in assoc args.
281+ $ wp_config_file_name = basename ( $ path );
282+ $ bytes_written = file_put_contents ( $ path , $ out );
283+
284+ if ( ! $ bytes_written ) {
285+ WP_CLI ::error ( "Could not create new ' {$ wp_config_file_name }' file. " );
286+ }
287+
288+ $ assoc_args = array_merge ( $ defaults , $ assoc_args );
289+
272290 // 'extra-php' from STDIN is retrieved after escaping to avoid breaking
273291 // the PHP code.
274292 if ( Utils \get_flag_value ( $ assoc_args , 'extra-php ' ) === true ) {
275293 $ assoc_args ['extra-php ' ] = file_get_contents ( 'php://stdin ' );
276294 }
277295
278- $ command_root = Utils \phar_safe_path ( dirname ( __DIR__ ) );
279- $ out = Utils \mustache_render ( "{$ command_root }/templates/wp-config.mustache " , $ assoc_args );
296+ $ options = [
297+ 'raw ' => false ,
298+ 'add ' => true ,
299+ 'normalize ' => true ,
300+ ];
280301
281- $ wp_config_file_name = basename ( $ assoc_args ['config-file ' ] );
282- $ bytes_written = file_put_contents ( $ assoc_args ['config-file ' ], $ out );
283- if ( ! $ bytes_written ) {
284- WP_CLI ::error ( "Could not create new ' {$ wp_config_file_name }' file. " );
285- } else {
286- WP_CLI ::success ( "Generated ' {$ wp_config_file_name }' file. " );
302+ $ config_keys = [
303+ 'dbhost ' => array (
304+ 'name ' => 'DB_HOST ' ,
305+ 'type ' => 'constant ' ,
306+ ),
307+ 'dbpass ' => array (
308+ 'name ' => 'DB_PASSWORD ' ,
309+ 'type ' => 'constant ' ,
310+ ),
311+ 'dbprefix ' => array (
312+ 'name ' => 'table_prefix ' ,
313+ 'type ' => 'variable ' ,
314+ ),
315+ 'dbcharset ' => array (
316+ 'name ' => 'DB_CHARSET ' ,
317+ 'type ' => 'constant ' ,
318+ ),
319+ 'dbcollate ' => array (
320+ 'name ' => 'DB_COLLATE ' ,
321+ 'type ' => 'constant ' ,
322+ ),
323+ 'locale ' => array (
324+ 'name ' => 'WPLANG ' ,
325+ 'type ' => 'constant ' ,
326+ ),
327+ 'dbname ' => array (
328+ 'name ' => 'DB_NAME ' ,
329+ 'type ' => 'constant ' ,
330+ ),
331+ 'dbuser ' => array (
332+ 'name ' => 'DB_USER ' ,
333+ 'type ' => 'constant ' ,
334+ ),
335+ ];
336+
337+ try {
338+ $ config_transformer = new WPConfigTransformer ( $ path );
339+
340+ foreach ( $ config_keys as $ key => $ const ) {
341+
342+ $ value = $ assoc_args [ $ key ];
343+ if ( ! empty ( $ value ) ) {
344+ $ config_transformer ->update ( $ const ['type ' ], $ const ['name ' ], $ value , $ options );
345+ }
346+ }
347+ } catch ( Exception $ exception ) {
348+ //Remove the default moustache wp-config.php template file.
349+ if ( file_exists ( $ assoc_args ['config-file ' ] ) ) {
350+ unlink ( $ path );
351+ }
352+
353+ WP_CLI ::error ( "Could not create new ' {$ wp_config_file_name }' file. \nReason: {$ exception ->getMessage ()}" );
287354 }
355+
356+ WP_CLI ::success ( "Generated ' {$ wp_config_file_name }' file. " );
288357 }
289358
290359 /**
0 commit comments