Skip to content

Commit

Permalink
feat: add backup functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot committed Apr 15, 2024
1 parent 8c576d1 commit 1ac4459
Show file tree
Hide file tree
Showing 13 changed files with 451 additions and 75 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"scripts": {
"phpstan": "php --version && php vendor/bin/phpstan --version && php -d memory_limit=1G vendor/bin/phpstan analyse -c ruleset-phpstan.neon -vvv",
"cs": "./vendor/bin/php-cs-fixer fix -vvv --show-progress=dots --config=ruleset-php_cs.php",
"test-all": [
"all": [
"@phpstan",
"@cs"
]
Expand All @@ -58,6 +58,6 @@
]
}
},
"minimum-stability": "dev",
"minimum-stability": "stable",
"prefer-stable": true
}
98 changes: 67 additions & 31 deletions resources/lang/en/filament-env-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,78 @@

'page' => [
'title' => '.Env Editor',

'form' => [
],
'actions' => [
'add' => [
'title' => 'Add new Entry',
'modalHeading' => 'Add new Entry',
'success' => [
'title' => 'Key ":Name", was successfully written',
],
'tabs' => [
'current-env' => [
'title' => 'Current .env',
],
'backups' => [
'title' => 'Backups',
],
],
'actions' => [
'add' => [
'title' => 'Add new Entry',
'modalHeading' => 'Add new Entry',
'success' => [
'title' => 'Key ":Name", was successfully written',
],
'form' => [
'fields' => [
'key' => 'key',
'value' => 'value',
'index' => 'Insert After existing key (optional)',
],
'form' => [
'fields' => [
'key' => 'key',
'value' => 'value',
'index' => 'Insert After existing key (optional)',
],
'helpText' => [
'index' => 'In case you need to put this new entry, after an existing one, you may pick one of the existing key ',
],
'helpText' => [
'index' => 'In case you need to put this new entry, after an existing one, you may pick one of the existing key ',
],
],
'edit' => [
'tooltip' => 'Edit Entry ":name"',
'modal' => [
'text' => 'Edit Entry',
],
],
'edit' => [
'tooltip' => 'Edit Entry ":name"',
'modal' => [
'text' => 'Edit Entry',
],
'delete' => [
'tooltip' => 'Remove the ":name" entry',
'confirm' => [
'title' => 'You are going to permanently remove ":name". Are you sure of this removal?',
],
],
'delete' => [
'tooltip' => 'Remove the ":name" entry',
'confirm' => [
'title' => 'You are going to permanently remove ":name". Are you sure of this removal?',
],
'clear-cache' => [
'title' => 'Clear caches',
'tooltip' => 'Sometimes laravel caches ENV variables, so you need to clear all caches ("artisan optimize:clear"), in order to rer-ead the .env change',
],
'clear-cache' => [
'title' => 'Clear caches',
'tooltip' => 'Sometimes laravel caches ENV variables, so you need to clear all caches ("artisan optimize:clear"), in order to rer-ead the .env change',
],

'backup' => [
'title' => 'Create a new Backup',
'success' => [
'title' => 'Backup, was successfully created',
],
],
'download' => [
'title' => 'Download current .env',
'tooltip' => 'Download ":name" backup file',
],
'upload-backup' => [
'title' => 'Upload a backup file',
],
'show-content' => [
'modalHeading' => 'Raw content of ":name" backup',
'tooltip' => 'Show raw content',
],
'restore-backup' => [
'confirm' => [
'title' => 'You are going to restore ":name", in place of current ".env" file. Please confirm you choice',
],
'modalSubmit' => 'Restore',
'tooltip' => 'Restore ":name", as current ENV',
],
'delete-backup' => [
'tooltip' => 'Remove the ":name" backup file',
'confirm' => [
'title' => 'You are going to permanently remove ":name" backup file. Are you sure of this removal?',
],
],
],
Expand Down
49 changes: 49 additions & 0 deletions src/Pages/Actions/Backups/DeleteBackupAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace GeoSot\FilamentEnvEditor\Pages\Actions\Backups;

use Filament\Forms\Components\Actions\Action;
use Filament\Support\Colors\Color;
use Filament\Support\Enums\ActionSize;
use GeoSot\EnvEditor\Dto\BackupObj;
use GeoSot\EnvEditor\Facades\EnvEditor;
use GeoSot\FilamentEnvEditor\Pages\ViewEnv;

class DeleteBackupAction extends Action
{
private BackupObj $entry;

public static function getDefaultName(): ?string
{
return 'delete';
}

public function setEntry(BackupObj $obj): static
{
$this->entry = $obj;

return $this;
}

protected function setUp(): void
{
parent::setUp();

$this->icon('heroicon-o-trash');
$this->hiddenLabel();
$this->outlined();
$this->color(Color::Rose);

$this->size(ActionSize::Small);
$this->tooltip(fn (): string => __('filament-env-editor::filament-env-editor.actions.delete-backup.tooltip', ['name' => $this->entry->name]));
$this->modalIcon('heroicon-o-trash');
$this->modalHeading(fn (): string => __('filament-env-editor::filament-env-editor.actions.delete-backup.confirm.title', ['name' => $this->entry->name]));

$this->action(function (ViewEnv $page) {
EnvEditor::deleteBackup($this->entry->name);
$page->refresh();
});

$this->requiresConfirmation();
}
}
51 changes: 51 additions & 0 deletions src/Pages/Actions/Backups/DownloadEnvFileAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace GeoSot\FilamentEnvEditor\Pages\Actions\Backups;

use Filament\Forms\Components\Actions\Action;
use Filament\Support\Colors\Color;
use GeoSot\EnvEditor\EnvEditor;
use GeoSot\EnvEditor\Exceptions\EnvException;

class DownloadEnvFileAction extends Action
{
private string $file = '';

public static function getDefaultName(): ?string
{
return 'download';
}

public function setEntry(string $file): static
{
$this->file = $file;

return $this;
}

protected function setUp(): void
{
parent::setUp();

$this->label(fn (): string => __('filament-env-editor::filament-env-editor.actions.download.title'));
$this->tooltip(fn (): string => __('filament-env-editor::filament-env-editor.actions.download.tooltip', ['name' => $this->file]));

$this->outlined();
$this->icon('heroicon-c-inbox-arrow-down');

$this->action(function () {
$result = false;
try {
$result = app(EnvEditor::class)->getFilePath($this->file);
} catch (EnvException $exception) {
$this->failureNotificationTitle($exception->getMessage());
$this->failure();
$this->halt();
}

return response()->download($result);
});

$this->color(Color::Sky);
}
}
37 changes: 37 additions & 0 deletions src/Pages/Actions/Backups/MakeBackupAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace GeoSot\FilamentEnvEditor\Pages\Actions\Backups;

use Filament\Forms\Components\Actions\Action;
use Filament\Support\Colors\Color;
use GeoSot\EnvEditor\Exceptions\EnvException;
use GeoSot\EnvEditor\Facades\EnvEditor;
use GeoSot\FilamentEnvEditor\Pages\ViewEnv;

class MakeBackupAction extends Action
{
protected function setUp(): void
{
parent::setUp();

$this->label(fn (): string => __('filament-env-editor::filament-env-editor.actions.backup.title'));

$this->action(function (array $data, ViewEnv $page) {
$result = false;
try {
$result = EnvEditor::backUpCurrent();
$page->refresh();
$this->successNotificationTitle(fn (
): string => __('filament-env-editor::filament-env-editor.actions.backup.success.title'));
} catch (EnvException $exception) {
$this->failureNotificationTitle($exception->getMessage());
$this->failure();
$this->halt();
}

$result ? $this->success() : $this->failure();
});

$this->color(Color::Teal);
}
}
53 changes: 53 additions & 0 deletions src/Pages/Actions/Backups/RestoreBackupAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace GeoSot\FilamentEnvEditor\Pages\Actions\Backups;

use Filament\Forms\Components\Actions\Action;
use Filament\Support\Colors\Color;
use Filament\Support\Enums\ActionSize;
use GeoSot\EnvEditor\Facades\EnvEditor;
use GeoSot\FilamentEnvEditor\Pages\ViewEnv;

class RestoreBackupAction extends Action
{
private string $file;

public static function getDefaultName(): ?string
{
return 'restore';
}

public function setEntry(string $file): static
{
$this->file = $file;

return $this;
}

protected function setUp(): void
{
parent::setUp();

$this->icon('heroicon-s-bars-arrow-up');
$this->hiddenLabel();
$this->outlined();
$this->color(Color::Teal);

$this->size(ActionSize::Small);
$this->tooltip(fn (): string => __('filament-env-editor::filament-env-editor.actions.restore-backup.tooltip',
['name' => $this->file]));
$this->modalIcon('heroicon-s-bars-arrow-up');
$this->modalHeading(fn (
): string => __('filament-env-editor::filament-env-editor.actions.restore-backup.confirm.title',
['name' => $this->file]));

$this->action(function (ViewEnv $page) {
EnvEditor::restoreBackUp($this->file);
$page->refresh();
});

$this->requiresConfirmation();
$this->modalSubmitActionLabel(fn (
) => __('filament-env-editor::filament-env-editor.actions.restore-backup.modalSubmit'));
}
}
46 changes: 46 additions & 0 deletions src/Pages/Actions/Backups/ShowBackupContentAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace GeoSot\FilamentEnvEditor\Pages\Actions\Backups;

use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\Placeholder;
use Filament\Support\Colors\Color;
use Filament\Support\Enums\ActionSize;
use GeoSot\EnvEditor\Dto\BackupObj;
use Illuminate\Support\HtmlString;

class ShowBackupContentAction extends Action
{
private BackupObj $entry;

public static function getDefaultName(): ?string
{
return 'show_raw_content_';
}

public function setEntry(BackupObj $obj): static
{
$this->entry = $obj;

return $this;
}

protected function setUp(): void
{
parent::setUp();

$this->modalHeading(fn (): string => __('filament-env-editor::filament-env-editor.actions.show-content.modalHeading', ['name' => $this->entry->name]));
$this->tooltip(fn (): string => __('filament-env-editor::filament-env-editor.actions.show-content.tooltip'));
$this->hiddenLabel();
$this->outlined();
$this->modalSubmitAction(false);
$this->icon('heroicon-o-newspaper');
$this->size(ActionSize::Small);

$this->form(fn () => [
Placeholder::make('')->content(new HtmlString("<pre>{$this->entry->rawContent}</pre>")),
]);

$this->color(Color::Zinc);
}
}
Loading

0 comments on commit 1ac4459

Please sign in to comment.