From 0f126886f5631d4292554b4ee3589155e7150e73 Mon Sep 17 00:00:00 2001 From: Miguel Nogueira Date: Fri, 10 Jul 2020 05:00:41 +0100 Subject: [PATCH] Use package for .env editing The solution provided by this package is similar to what was previously implemented (see #1), but it's nicely packaged and made available in a Facade. --- app/Console/Commands/Install.php | 2 +- app/Console/Commands/SetEnv.php | 28 ++------------- composer.json | 1 + composer.lock | 61 +++++++++++++++++++++++++++++++- 4 files changed, 64 insertions(+), 28 deletions(-) diff --git a/app/Console/Commands/Install.php b/app/Console/Commands/Install.php index c393779..21568f8 100644 --- a/app/Console/Commands/Install.php +++ b/app/Console/Commands/Install.php @@ -110,7 +110,7 @@ class Install extends Command foreach($settings as $keyname => $value) { - $this->callSilent('environment:modify', [ + $this->call('environment:modify', [ 'key' => $keyname, 'value' => $value ]); diff --git a/app/Console/Commands/SetEnv.php b/app/Console/Commands/SetEnv.php index f4ef7cc..9125ffa 100644 --- a/app/Console/Commands/SetEnv.php +++ b/app/Console/Commands/SetEnv.php @@ -3,6 +3,7 @@ namespace App\Console\Commands; use Illuminate\Console\Command; +use GeoSot\EnvEditor\Facades\EnvEditor; class SetEnv extends Command { @@ -38,43 +39,18 @@ class SetEnv extends Command { $path = base_path('/.env'); $key = $this->argument('key'); - $value = $this->argument('value'); - $originalValue = env($key); - if (is_null($originalValue)) - { - // Attempt to silently fix issue - $this->callSilent('cache:clear'); - $originalValue = env($key); - // Still fails? Let the user know - if (is_null($originalValue)) - { - $this->error('[!!] Cannot update requested configuration value! This is a known Laravel issue. If you report a bug, keep that in mind.'); - - return false; - } - } if (file_exists($path)) { - $file = file_get_contents($path); - $newConfig = str_replace($key . '=' . $originalValue, $key . '=' . $value, $file); - - file_put_contents( - $path, - $newConfig - ); - + EnvEditor::editKey($key, $value); } else { $this->error('Cannot update a file that doesn\'t exist! Please create .env first.'); return false; } - - - $this->info('>> Changed value! It may now be accessed via env() or config() if there\'s a file for it.'); } } diff --git a/composer.json b/composer.json index 3c5fd2c..060fcea 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "doctrine/dbal": "^2.10", "fideloper/proxy": "^4.2", "fruitcake/laravel-cors": "^1.0", + "geo-sot/laravel-env-editor": "^0.9.9", "guzzlehttp/guzzle": "^6.5", "jeroennoten/laravel-adminlte": "^3.2", "laravel/framework": "^7.0", diff --git a/composer.lock b/composer.lock index ffd5909..bbe774f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "fd14fa762c6ac8136b171abd8437e7d7", + "content-hash": "16e8efbacd91ef3417b21bd2648e1629", "packages": [ { "name": "almasaeed2010/adminlte", @@ -1032,6 +1032,65 @@ ], "time": "2020-04-28T08:47:37+00:00" }, + { + "name": "geo-sot/laravel-env-editor", + "version": "v0.9.9", + "source": { + "type": "git", + "url": "https://github.com/GeoSot/Laravel-EnvEditor.git", + "reference": "e828d3d3310890286d0b53045de9381187258605" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/GeoSot/Laravel-EnvEditor/zipball/e828d3d3310890286d0b53045de9381187258605", + "reference": "e828d3d3310890286d0b53045de9381187258605", + "shasum": "" + }, + "require": { + "laravel/framework": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0|~7.0", + "php": "^7.1" + }, + "require-dev": { + "orchestra/testbench": "^3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "0.9.*-dev" + }, + "laravel": { + "providers": [ + "GeoSot\\EnvEditor\\ServiceProvider" + ], + "aliases": { + "EnvEditor": "GeoSot\\EnvEditor\\Facades\\EnvEditor" + } + } + }, + "autoload": { + "psr-4": { + "GeoSot\\EnvEditor\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Geo Sot", + "email": "geo.sotis@gmail.com" + } + ], + "description": "A laravel Package that Supports .Env File editing and backup ", + "keywords": [ + "EnvEditor", + "geo-sot", + "laravel", + "laravel-env-editor" + ], + "time": "2020-04-17T23:33:36+00:00" + }, { "name": "guzzlehttp/guzzle", "version": "6.5.4",