Skip to content

Commit

Permalink
feat!: change file manager to auto-discover .env file (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot authored Apr 13, 2024
1 parent 5558764 commit a040641
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 23 deletions.
5 changes: 0 additions & 5 deletions config/env-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@
|--------------------------------------------------------------------------
*/
'paths' => [
// .env file directory
'env' => base_path(),
// backup files directory
'backupDirectory' => storage_path('env-editor'),
],
// .env file name
'envFileName' => '.env',

/*
|--------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions src/Helpers/EnvFilesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
Expand All @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/UiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
18 changes: 7 additions & 11 deletions tests/Unit/Helpers/EnvKeysManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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());
Expand All @@ -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");

Expand Down Expand Up @@ -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'));
Expand All @@ -117,13 +116,10 @@ public function adds_keys(): void
}
}

/**
* @param array<string, mixed> $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);
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/Helpers/FilesManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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';
Expand Down

0 comments on commit a040641

Please sign in to comment.