Skip to content

Commit cf7da88

Browse files
Merge pull request wp-cli#9 from rahul3883/issue/wp-cliGH-2
Create `wp config get` to list configurations of `wp-config.php`
2 parents 51b2fe7 + 1c77910 commit cf7da88

2 files changed

Lines changed: 99 additions & 1 deletion

File tree

features/config.feature

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
Feature: Manage wp-config.php file
22

3-
Scenario: Get a wp-config.php file path
3+
Background:
44
Given a WP install
55

6+
Scenario: Get a wp-config.php file path
67
When I try `wp config path`
78
And STDOUT should contain:
89
"""
910
wp-config.php
1011
"""
1112
And STDERR should be empty
13+
14+
Scenario: Get configurations defined in a wp-config.php file
15+
When I try `wp config get`
16+
Then STDOUT should be a table containing rows:
17+
| key | value | type |
18+
19+
When I try `wp config get --fields=key,type`
20+
Then STDOUT should be a table containing rows:
21+
| key | type |
22+
| DB_NAME | constant |
23+
| DB_USER | constant |
24+
| DB_PASSWORD | constant |
25+
| DB_HOST | constant |

src/Config_Command.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,90 @@ public function path() {
166166
}
167167
}
168168

169+
/**
170+
* Get variables and constants defined in wp-config.php file.
171+
*
172+
* ## OPTIONS
173+
*
174+
* [--fields=<fields>]
175+
* : Limit the output to specific fields. Defaults to all fields.
176+
*
177+
* [--format=<format>]
178+
* : Render output in a particular format.
179+
* ---
180+
* default: table
181+
* options:
182+
* - table
183+
* - csv
184+
* - json
185+
* - yaml
186+
* ---
187+
*
188+
* ## EXAMPLES
189+
*
190+
* # List variables and constants defined in wp-config.php file.
191+
* $ wp config get --format=table
192+
* +------------------+------------------------------------------------------------------+----------+
193+
* | key | value | type |
194+
* +------------------+------------------------------------------------------------------+----------+
195+
* | table_prefix | wp_ | variable |
196+
* | DB_NAME | wp_cli_test | constant |
197+
* | DB_USER | root | constant |
198+
* | DB_PASSWORD | root | constant |
199+
* | AUTH_KEY | r6+@shP1yO&$)1gdu.hl[/j;7Zrvmt~o;#WxSsa0mlQOi24j2cR,7i+QM/#7S:o^ | constant |
200+
* | SECURE_AUTH_KEY | iO-z!_m--YH$Tx2tf/&V,YW*13Z_HiRLqi)d?$o-tMdY+82pK$`T.NYW~iTLW;xp | constant |
201+
* +------------------+------------------------------------------------------------------+----------+
202+
*
203+
* @when before_wp_load
204+
*/
205+
public function get( $_, $assoc_args ) {
206+
$default_fields = array(
207+
'key',
208+
'value',
209+
'type',
210+
);
211+
212+
$defaults = array(
213+
'fields' => implode( ',', $default_fields ),
214+
'format' => 'table',
215+
);
216+
217+
$assoc_args = array_merge( $defaults, $assoc_args );
218+
219+
$wp_cli_original_defined_constants = get_defined_constants();
220+
$wp_cli_original_defined_vars = get_defined_vars();
221+
222+
eval( WP_CLI::get_runner()->get_wp_config_code() );
223+
224+
$wp_config_vars = self::get_wp_config_vars( get_defined_vars(), $wp_cli_original_defined_vars, 'variable', array( 'wp_cli_original_defined_vars' ) );
225+
$wp_config_constants = self::get_wp_config_vars( get_defined_constants(), $wp_cli_original_defined_constants, 'constant' );
226+
227+
WP_CLI\Utils\format_items( $assoc_args['format'], array_merge( $wp_config_vars, $wp_config_constants ), $assoc_args['fields'] );
228+
}
229+
230+
/**
231+
* Filter wp-config.php file configurations.
232+
*
233+
* @param array $list
234+
* @param array $previous_list
235+
* @param array $exclude_list
236+
* @return array
237+
*/
238+
private static function get_wp_config_vars( $list, $previous_list, $type, $exclude_list = array() ) {
239+
$result = array();
240+
foreach ( $list as $key => $val ) {
241+
if ( array_key_exists( $key, $previous_list ) || in_array( $key, $exclude_list ) ) {
242+
continue;
243+
}
244+
$out = array();
245+
$out['key'] = $key;
246+
$out['value'] = $val;
247+
$out['type'] = $type;
248+
$result[] = $out;
249+
}
250+
return $result;
251+
}
252+
169253
private static function _read( $url ) {
170254
$headers = array('Accept' => 'application/json');
171255
$response = Utils\http_request( 'GET', $url, null, $headers, array( 'timeout' => 30 ) );

0 commit comments

Comments
 (0)