public function config($_, $assoc_args)
{
global $wp_version;
if (Utils\locate_wp_config()) {
WP_CLI::error("The 'wp-config.php' file already exists.");
}
$versions_path = ABSPATH . 'wp-includes/version.php';
include $versions_path;
$defaults = array('dbhost' => 'localhost', 'dbpass' => '', 'dbprefix' => 'wp_', 'dbcharset' => 'utf8', 'dbcollate' => '', 'locale' => self::get_initial_locale());
$assoc_args = array_merge($defaults, $assoc_args);
if (preg_match('|[^a-z0-9_]|i', $assoc_args['dbprefix'])) {
WP_CLI::error('--dbprefix can only contain numbers, letters, and underscores.');
}
// Check DB connection
if (!\WP_CLI\Utils\get_flag_value($assoc_args, 'skip-check')) {
Utils\run_mysql_command('mysql --no-defaults', array('execute' => ';', 'host' => $assoc_args['dbhost'], 'user' => $assoc_args['dbuser'], 'pass' => $assoc_args['dbpass']));
}
if (\WP_CLI\Utils\get_flag_value($assoc_args, 'extra-php') === true) {
$assoc_args['extra-php'] = file_get_contents('php://stdin');
}
// TODO: adapt more resilient code from wp-admin/setup-config.php
if (!\WP_CLI\Utils\get_flag_value($assoc_args, 'skip-salts')) {
$assoc_args['keys-and-salts'] = self::_read('https://api.wordpress.org/secret-key/1.1/salt/');
}
if (\WP_CLI\Utils\wp_version_compare('4.0', '<')) {
$assoc_args['add-wplang'] = true;
} else {
$assoc_args['add-wplang'] = false;
}
$out = Utils\mustache_render('wp-config.mustache', $assoc_args);
$bytes_written = file_put_contents(ABSPATH . 'wp-config.php', $out);
if (!$bytes_written) {
WP_CLI::error("Could not create new 'wp-config.php' file.");
} else {
WP_CLI::success("Generated 'wp-config.php' file.");
}
}