feat(dump): list dumps stored in the plugin filesystem
This commit is contained in:
@@ -0,0 +1,48 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace AeonDumpManager\Service;
|
||||||
|
|
||||||
|
use AeonDumpManager\Struct\DumpFile;
|
||||||
|
use League\Flysystem\FilesystemOperator;
|
||||||
|
|
||||||
|
class DumpLister
|
||||||
|
{
|
||||||
|
private const DUMP_DIRECTORY = 'dumps';
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private readonly FilesystemOperator $aeonDumpManagerFilesystemPrivate,
|
||||||
|
private readonly DumpFilenameParser $filenameParser,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return DumpFile[] newest first
|
||||||
|
*/
|
||||||
|
public function list(): array
|
||||||
|
{
|
||||||
|
$dumps = [];
|
||||||
|
|
||||||
|
foreach ($this->aeonDumpManagerFilesystemPrivate->listContents(self::DUMP_DIRECTORY) as $item) {
|
||||||
|
if (!$item->isFile()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$filename = basename($item->path());
|
||||||
|
$datetime = $this->filenameParser->parse($filename);
|
||||||
|
if ($datetime === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$dumps[] = new DumpFile($filename, $datetime, $item->fileSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
usort($dumps, static fn (DumpFile $a, DumpFile $b) => $b->datetime <=> $a->datetime ?: $b->filename <=> $a->filename);
|
||||||
|
|
||||||
|
return $dumps;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function path(string $filename): string
|
||||||
|
{
|
||||||
|
return self::DUMP_DIRECTORY . '/' . $filename;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace AeonDumpManager\Service\Exception;
|
||||||
|
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
class DumpNotFoundException extends RuntimeException
|
||||||
|
{
|
||||||
|
public static function create(string $filename): self
|
||||||
|
{
|
||||||
|
return new self(sprintf('Dump "%s" does not exist.', $filename));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace AeonDumpManager\Struct;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A dump on disk. Not a DAL entity — dumps are plain files in the plugin's
|
||||||
|
* private Flysystem mount, identified by their filename.
|
||||||
|
*/
|
||||||
|
final class DumpFile
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public readonly string $filename,
|
||||||
|
public readonly \DateTimeImmutable $datetime,
|
||||||
|
public readonly int $sizeBytes,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{filename: string, datetime: string, sizeBytes: int}
|
||||||
|
*/
|
||||||
|
public function jsonSerialize(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'filename' => $this->filename,
|
||||||
|
'datetime' => $this->datetime->format(DATE_ATOM),
|
||||||
|
'sizeBytes' => $this->sizeBytes,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user