Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pref: Add backup collection #50

Merged
merged 2 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions resources/views/components/_backup.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<template v-for="(item, index) in items">
<tr :key="item.real_name" :bind="item">
<th scope="row" class="font-weight-bold ">@{{ item.name }}</th>
<td>@{{ item.created_at_formatted }}</td>
<td>@{{ item.created_at }}</td>
<td>
<div class="btn-group" role="group">
<button class="btn btn-info" data-toggle="collapse" aria-expanded="false"
Expand All @@ -40,7 +40,7 @@ class="fas fa-download"></span></a>
<div class="collapse" :id="'collapse_'+item.real_name" data-parent="#env-editor-table-accordion">
<div class="table-responsive table-sm px-3 pb-3">
<table class="w-100 bg-light">
<tr v-for="(dt, index) in item.parsed_data">
<tr v-for="(dt, index) in item.entries">
<td class="pl-3"><code>@{{ dt.key||'&nbsp;' }}</code></td>
<td><code>@{{ dt.value }}</code></td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/EnvController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function deleteKey(Request $request): JsonResponse
*/
public function getBackupFiles(Request $request): View|JsonResponse
{
$allBackUps = $this->envEditor->getAllBackUps();
$allBackUps = $this->envEditor->getAllBackUps()->toArray();
if ($request->wantsJson()) {
return $this->returnGenericResponse(true, ['items' => $allBackUps]);
}
Expand Down
50 changes: 50 additions & 0 deletions src/Dto/BackupObj.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace GeoSot\EnvEditor\Dto;

use Carbon\Carbon;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;

/**
* @implements Arrayable<string, scalar>
*/
class BackupObj implements \JsonSerializable, Arrayable
{
/**
* @param Collection<int, EntryObj> $entries
*/
public function __construct(
public readonly string $name,
public readonly Carbon $createdAt,
public readonly Carbon $modifiedAt,
public readonly string $path,
public readonly string $rawContent,
public readonly Collection $entries,
) {
}

/**
* @return array{real_name:string, name:string, created_at:string, modified_at:string, raw_content:string, path:string, entries:array<int,EntryObj>}
*/
public function toArray(): array
{
return [
'real_name' => $this->name,
'name' => $this->name,
'created_at' => Carbon::createFromTimestamp($this->createdAt)->format(config('env-editor.timeFormat')),
'modified_at' => Carbon::createFromTimestamp($this->modifiedAt)->format(config('env-editor.timeFormat')),
'raw_content' => $this->rawContent,
'path' => $this->path,
'entries' => $this->entries->toArray(),
];
}

/**
* @return array{real_name:string, name:string, created_at:string, modified_at:string, raw_content:string, path:string, entries:array<int,EntryObj>}
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
}
17 changes: 13 additions & 4 deletions src/Helpers/EntryObj.php → src/Dto/EntryObj.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
<?php

namespace GeoSot\EnvEditor\Helpers;
namespace GeoSot\EnvEditor\Dto;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;

class EntryObj implements \JsonSerializable
/**
* @implements Arrayable<string, scalar>
*/
class EntryObj implements \JsonSerializable, Arrayable
{
/**
* @param int|string|null $value
*/
public function __construct(public string $key, protected mixed $value, public int $group, public int $index, protected bool $isSeparator = false)
{
public function __construct(
public readonly string $key,
protected mixed $value,
public readonly int $group,
public readonly int $index,
protected bool $isSeparator = false
) {
}

public static function parseEnvLine(string $line, int $group, int $index): self
Expand Down
7 changes: 3 additions & 4 deletions src/EnvEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace GeoSot\EnvEditor;

use GeoSot\EnvEditor\Dto\BackupObj;
use GeoSot\EnvEditor\Dto\EntryObj;
use GeoSot\EnvEditor\Exceptions\EnvException;
use GeoSot\EnvEditor\Helpers\EntryObj;
use GeoSot\EnvEditor\Helpers\EnvFileContentManager;
use GeoSot\EnvEditor\Helpers\EnvFilesManager;
use GeoSot\EnvEditor\Helpers\EnvKeysManager;
Expand Down Expand Up @@ -94,9 +95,7 @@ public function deleteKey(string $key): bool
/**
* Get all Backup Files.
*
* @return Collection<int, array{real_name:string, name:string, created_at:int, modified_at:int, created_at_formatted:string, modified_at_formatted:string, content:string, path:string,parsed_data:Collection<int, EntryObj>}>
*
* @throws EnvException
* @return Collection<int, BackupObj>
*/
public function getAllBackUps(): Collection
{
Expand Down
1 change: 1 addition & 0 deletions src/Helpers/EnvFileContentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace GeoSot\EnvEditor\Helpers;

use GeoSot\EnvEditor\Dto\EntryObj;
use GeoSot\EnvEditor\EnvEditor;
use GeoSot\EnvEditor\Exceptions\EnvException;
use Illuminate\Filesystem\Filesystem;
Expand Down
32 changes: 13 additions & 19 deletions src/Helpers/EnvFilesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace GeoSot\EnvEditor\Helpers;

use Carbon\Carbon;
use GeoSot\EnvEditor\Dto\BackupObj;
use GeoSot\EnvEditor\EnvEditor;
use GeoSot\EnvEditor\Exceptions\EnvException;
use GeoSot\EnvEditor\ServiceProvider;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\HttpFoundation\File\File;

class EnvFilesManager
Expand All @@ -21,30 +23,22 @@ public function __construct(protected EnvEditor $envEditor, protected Filesystem
/**
* Get all Backup Files.
*
* @return Collection<int, array{real_name:string, name:string, created_at:int, modified_at:int, created_at_formatted:string, modified_at_formatted:string, content:string, path:string,parsed_data:Collection<int, EntryObj>}>
* @return Collection<int, BackupObj>
*/
public function getAllBackUps(): Collection
{
$files = $this->filesystem->files($this->getBackupsDir());
/** @var Collection<int, array{real_name:string, name:string, created_at:int, modified_at:int, created_at_formatted:string, modified_at_formatted:string, content:string, path:string,parsed_data:Collection<int, EntryObj>}> $collection */
$collection = collect([]);
foreach ($files as $file) {
$data = [
'real_name' => $file->getFilename(),
'name' => $file->getFilename(),
'created_at' => $file->getCTime(),
'modified_at' => $file->getMTime(),
'created_at_formatted' => Carbon::createFromTimestamp($file->getCTime())->format($this->envEditor->config('timeFormat')),
'modified_at_formatted' => Carbon::createFromTimestamp($file->getMTime())->format($this->envEditor->config('timeFormat')),
'content' => $file->getContents(),
'path' => $file->getPath(),
'parsed_data' => $this->envEditor->getFileContentManager()->getParsedFileContent($file->getFilename()),
];

$collection->push($data);
}

return $collection->sortByDesc('created_at');
return (new Collection($files))
->map(fn (SplFileInfo $file): BackupObj => new BackupObj(
$file->getFilename(),
Carbon::parse($file->getCTime()),
Carbon::parse($file->getMTime()),
$file->getPath(),
$file->getContents(),
$this->envEditor->getFileContentManager()->getParsedFileContent($file->getFilename()),
))
->sortByDesc('createdAt');
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Helpers/EnvKeysManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace GeoSot\EnvEditor\Helpers;

use GeoSot\EnvEditor\Dto\EntryObj;
use GeoSot\EnvEditor\EnvEditor;
use GeoSot\EnvEditor\Exceptions\EnvException;
use GeoSot\EnvEditor\ServiceProvider;
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/UiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function can_restore_backups(): void
EnvEditor::backUpCurrent();
EnvEditor::deleteKey('FOO');
$this->assertNull(EnvEditor::getKey('FOO'));
$file = EnvEditor::getAllBackUps()->first()['name'];
$file = EnvEditor::getAllBackUps()->first()->name;
$this->postJson($this->makeRoute('restoreBackup').'/'.$file);
$this->assertSame('bar', EnvEditor::getKey('FOO'));
}
Expand All @@ -136,7 +136,7 @@ public function can_destroy_backups(): void
File::deleteDirectory($backupsDir);
EnvEditor::backUpCurrent();

$file = EnvEditor::getAllBackUps()->first()['name'];
$file = EnvEditor::getAllBackUps()->first()->name;
$this->deleteJson($this->makeRoute('destroyBackup').'/'.$file);
$this->assertCount(0, EnvEditor::getAllBackUps());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Helpers/EntryObjTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GeoSot\EnvEditor\Tests\Unit\Helpers;

use GeoSot\EnvEditor\Helpers\EntryObj;
use GeoSot\EnvEditor\Dto\EntryObj;
use GeoSot\EnvEditor\Tests\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Helpers/EnvFileContentManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace GeoSot\EnvEditor\Tests\Unit\Helpers;

use GeoSot\EnvEditor\Dto\EntryObj;
use GeoSot\EnvEditor\EnvEditor;
use GeoSot\EnvEditor\Exceptions\EnvException;
use GeoSot\EnvEditor\Helpers\EntryObj;
use GeoSot\EnvEditor\Helpers\EnvFileContentManager;
use GeoSot\EnvEditor\Tests\TestCase;
use Illuminate\Config\Repository;
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Helpers/FilesManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use GeoSot\EnvEditor\Tests\TestCase;
use Illuminate\Config\Repository;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;

Expand Down Expand Up @@ -129,8 +128,9 @@ public function back_up_current_env_works_and_returns_bool(): void
$this->assertTrue($result);

$backUps = $manager->getAllBackUps();

$this->assertEquals(1, $backUps->count());
$this->assertEquals(Arr::get($backUps->first(), 'content'), $content);
$this->assertEquals($backUps->first()->rawContent, $content);

unlink($file);
}
Expand Down
Loading