Skip to content

Commit

Permalink
Merge pull request #18 from Sidsector9/feature/GH-11
Browse files Browse the repository at this point in the history
List files included by wp-config.php
  • Loading branch information
danielbachhuber committed Jul 6, 2017
2 parents 0103ead + 14ac86e commit 42502b7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
45 changes: 30 additions & 15 deletions features/config.feature
@@ -1,25 +1,40 @@
Feature: Manage wp-config.php file

Background:
Given a WP install
Scenario: List constants, globals and files included from wp-config.php
Given an empty directory
And WP files
And a wp-config-extra.php file:
"""
require_once 'custom-include.php';
"""
And a custom-include.php file:
"""
<?php // This won't work without this file being empty. ?>
"""
When I run `wp core config {CORE_CONFIG_SETTINGS} --extra-php < wp-config-extra.php`
Then STDOUT should contain:
"""
Generated 'wp-config.php' file.
"""
When I run `wp config get --fields=key,type`
Then STDOUT should be a table containing rows:
| key | type |
| DB_NAME | constant |
| DB_USER | constant |
| DB_PASSWORD | constant |
| DB_HOST | constant |
| custom-include.php | includes |
When I try `wp config get`
Then STDOUT should be a table containing rows:
| key | value | type |
Scenario: Get a wp-config.php file path
Given a WP install
When I try `wp config path`
And STDOUT should contain:
"""
wp-config.php
"""
And STDERR should be empty

Scenario: Get configurations defined in a wp-config.php file
When I try `wp config get`
Then STDOUT should be a table containing rows:
| key | value | type |

When I try `wp config get --fields=key,type`
Then STDOUT should be a table containing rows:
| key | type |
| DB_NAME | constant |
| DB_USER | constant |
| DB_PASSWORD | constant |
| DB_HOST | constant |
28 changes: 24 additions & 4 deletions src/Config_Command.php
Expand Up @@ -224,14 +224,35 @@ public function get( $_, $assoc_args ) {

$wp_cli_original_defined_constants = get_defined_constants();
$wp_cli_original_defined_vars = get_defined_vars();
$wp_cli_original_includes = get_included_files();

eval( WP_CLI::get_runner()->get_wp_config_code() );

$wp_config_vars = self::get_wp_config_vars( get_defined_vars(), $wp_cli_original_defined_vars, 'variable', array( 'wp_cli_original_defined_vars' ) );
$wp_config_constants = self::get_wp_config_vars( get_defined_constants(), $wp_cli_original_defined_constants, 'constant' );

$get_constant = ! empty( $assoc_args['constant'] );
$get_global = ! empty( $assoc_args['global'] );
foreach ( $wp_config_vars as $key => $value ) {
if ( 'wp_cli_original_includes' === $value['key'] ) {
$key_backup = $key;
break;
}
}

unset( $wp_config_vars[ $key_backup ] );
$wp_config_vars = array_values( $wp_config_vars );
$wp_config_includes = array_diff( get_included_files(), $wp_cli_original_includes );
$wp_config_includes_array = array();

foreach ( $wp_config_includes as $key => $value ) {
$wp_config_includes_array[] = array(
'key' => basename( $value ),
'value' => $value,
'type' => 'includes',
);
}

$get_constant = ! empty( $assoc_args['constant'] );
$get_global = ! empty( $assoc_args['global'] );

if ( $get_constant && $get_global ) {
WP_CLI::error( 'Cannot request the value of a constant and a global at the same time.' );
Expand All @@ -244,8 +265,7 @@ public function get( $_, $assoc_args ) {
return;
}


WP_CLI\Utils\format_items( $assoc_args['format'], array_merge( $wp_config_vars, $wp_config_constants ), $assoc_args['fields'] );
WP_CLI\Utils\format_items( $assoc_args['format'], array_merge( $wp_config_vars, $wp_config_constants, $wp_config_includes_array ), $assoc_args['fields'] );
}

/**
Expand Down

0 comments on commit 42502b7

Please sign in to comment.