Editing a wp-config.php file programmatically is imprecise and error-prone, when it ideally would be as easy as running a WP-CLI command. Borek Bernard has some suggestions for how to implement: https://github.com/wp-cli/wp-cli/issues/2501