From fb4d8324895970d40858b7e1203ec828737cd7eb Mon Sep 17 00:00:00 2001 From: Miguel Nogueira Date: Thu, 9 Jul 2020 08:46:45 +0100 Subject: [PATCH] Beautified the search and replace function --- app/Console/Commands/SetEnv.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app/Console/Commands/SetEnv.php b/app/Console/Commands/SetEnv.php index aabb43a..b6fd0e9 100644 --- a/app/Console/Commands/SetEnv.php +++ b/app/Console/Commands/SetEnv.php @@ -32,20 +32,27 @@ class SetEnv extends Command /** * Execute the console command. - * @see https://stackoverflow.com/a/50965881/11540218 * @return mixed */ public function handle() { $path = base_path('/.env'); - $name = $this->argument('key'); + $key = $this->argument('key'); + $value = $this->argument('value'); + $originalValue = env($key); if (file_exists($path)) { - file_put_contents($path, str_replace( - $name . '=' . env($name), $name . '=' . $value, file_get_contents($path) - )); + $file = file_get_contents($path); + $newConfig = str_replace($key . '=' . $originalValue, $key . '=' . $value, $file); + + + file_put_contents( + $path, + $newConfig + ); + } $this->info('>> Changed value! It may now be accessed via env() or config() if there\'s a file for it.');