From a04064194fa4da7a1d42b096346a197c9a847157 Mon Sep 17 00:00:00 2001 From: GeoSot Date: Sat, 13 Apr 2024 18:22:33 +0300 Subject: [PATCH] feat!: change file manager to auto-discover .env file (#47) --- config/env-editor.php | 5 ----- src/Helpers/EnvFilesManager.php | 6 +++--- tests/Feature/UiTest.php | 4 ++-- tests/Unit/Helpers/EnvKeysManagerTest.php | 18 +++++++----------- tests/Unit/Helpers/FilesManagerTest.php | 5 +++-- 5 files changed, 15 insertions(+), 23 deletions(-) diff --git a/config/env-editor.php b/config/env-editor.php index 38d2d9d..00d0b82 100644 --- a/config/env-editor.php +++ b/config/env-editor.php @@ -7,13 +7,8 @@ |-------------------------------------------------------------------------- */ 'paths' => [ - // .env file directory - 'env' => base_path(), - // backup files directory 'backupDirectory' => storage_path('env-editor'), ], - // .env file name - 'envFileName' => '.env', /* |-------------------------------------------------------------------------- diff --git a/src/Helpers/EnvFilesManager.php b/src/Helpers/EnvFilesManager.php index 229fd98..0f6ea51 100644 --- a/src/Helpers/EnvFilesManager.php +++ b/src/Helpers/EnvFilesManager.php @@ -115,7 +115,7 @@ public function deleteBackup(string $fileName): bool public function getFilePath(string $fileName = ''): string { $path = (empty($fileName)) - ? $this->getEnvDir($this->getEnvFileName()) + ? $this->getEnvFileName() : $this->getBackupsDir($fileName); if ($this->filesystem->exists($path)) { @@ -138,7 +138,7 @@ protected function makeBackUpFileName(): string */ protected function getEnvFileName(): string { - return $this->envEditor->config('envFileName'); + return app()->environmentFilePath(); } public function getBackupsDir(string $path = ''): string @@ -148,7 +148,7 @@ public function getBackupsDir(string $path = ''): string public function getEnvDir(string $path = ''): string { - return $this->envEditor->config('paths.env').($path ? DIRECTORY_SEPARATOR.$path : $path); + return dirname($this->getEnvFileName()).($path ? DIRECTORY_SEPARATOR.$path : $path); } /** diff --git a/tests/Feature/UiTest.php b/tests/Feature/UiTest.php index 423eecb..659c081 100644 --- a/tests/Feature/UiTest.php +++ b/tests/Feature/UiTest.php @@ -13,8 +13,8 @@ class UiTest extends TestCase protected function getEnvironmentSetUp($app): void { parent::getEnvironmentSetUp($app); - $app['config']->set('env-editor.paths.env', self::getTestPath()); - $app['config']->set('env-editor.envFileName', self::getTestFile()); + $app->useEnvironmentPath(self::getTestPath()); + $app->loadEnvironmentFrom(self::getTestFile()); $app['config']->set('env-editor.route.enable', true); } diff --git a/tests/Unit/Helpers/EnvKeysManagerTest.php b/tests/Unit/Helpers/EnvKeysManagerTest.php index c68d0f3..8c44a7d 100644 --- a/tests/Unit/Helpers/EnvKeysManagerTest.php +++ b/tests/Unit/Helpers/EnvKeysManagerTest.php @@ -16,8 +16,8 @@ class EnvKeysManagerTest extends TestCase protected function setUp(): void { parent::setUp(); - $this->app['config']->set('env-editor.paths.env', self::getTestPath()); - $this->app['config']->set('env-editor.envFileName', self::getTestFile()); + $this->app->useEnvironmentPath(self::getTestPath()); + $this->app->loadEnvironmentFrom(self::getTestFile()); } #[Test] @@ -47,8 +47,7 @@ public function deletes_keys(): void { $fileName = 'dummy.tmp'; $fullPath = $this->createNewDummyFile($fileName); - $this->app['config']->set('env-editor.envFileName', $fileName); - + $this->app->loadEnvironmentFrom($fileName); $getContent = fn (): string => file_get_contents($fullPath) ?: throw new \RuntimeException("File {$fullPath}, not found"); self::assertStringContainsString('LOG_CHANNEL', $getContent()); @@ -73,7 +72,7 @@ public function edits_keys(): void { $fileName = 'dummy.tmp'; $fullPath = $this->createNewDummyFile($fileName); - $this->app['config']->set('env-editor.envFileName', $fileName); + $this->app->loadEnvironmentFrom($fileName); $getContent = fn (): string => file_get_contents($fullPath) ?: throw new \RuntimeException("File {$fullPath}, not found"); @@ -105,7 +104,7 @@ public function adds_keys(): void { $fileName = 'dummy.tmp'; $fullPath = $this->createNewDummyFile($fileName); - $this->app['config']->set('env-editor.envFileName', $fileName); + $this->app->loadEnvironmentFrom($fileName); EnvEditorFacade::addKey('FOO', 'bar'); $this->assertSame('bar', EnvEditorFacade::getKey('FOO')); @@ -117,13 +116,10 @@ public function adds_keys(): void } } - /** - * @param array $config - */ - protected function getEnvKeysManager(array $config = []): EnvKeysManager + protected function getEnvKeysManager(): EnvKeysManager { $envEditor = new EnvEditor( - new Repository($config ?: $this->app['config']->get('env-editor')), + new Repository($this->app['config']->get('env-editor')), new Filesystem() ); $this->app->singleton(EnvEditor::class, fn () => $envEditor); diff --git a/tests/Unit/Helpers/FilesManagerTest.php b/tests/Unit/Helpers/FilesManagerTest.php index 3f33647..e2a0873 100644 --- a/tests/Unit/Helpers/FilesManagerTest.php +++ b/tests/Unit/Helpers/FilesManagerTest.php @@ -112,7 +112,7 @@ public function get_all_back_ups_returns_all_files(): void public function back_up_current_env_works_and_returns_bool(): void { $fileName = 'test.tmp'; - $this->app['config']->set('env-editor.envFileName', $fileName); + $this->app->loadEnvironmentFrom($fileName); $content = time().'_dummy'; $manager = $this->getEnvFilesManager(); @@ -138,9 +138,10 @@ public function back_up_current_env_works_and_returns_bool(): void #[Test] public function restore_backup_works_and_returns_bool(): void { + $this->app->loadEnvironmentFrom('.env.example'); $manager = $this->getEnvFilesManager(); // place a dummy env file - file_put_contents($manager->getEnvDir($this->app['config']->get('env-editor.envFileName')), ''); + file_put_contents($this->app->environmentFile(), ''); $fileName = time().'_test.tmp'; $content = time().'_dummy';