Improve caching, performance and add dry-run

This commit is contained in:
René Preuß
2021-07-22 16:42:01 +02:00
parent 53cff7d805
commit 820287dd2c
13 changed files with 342 additions and 40 deletions

View File

@@ -4,15 +4,17 @@ namespace App\Bunny\Filesystem;
use Illuminate\Support\Str;
class LocalFile implements File
class LocalFile implements File, FileStreamable, FileSerialized
{
private string $filename;
private ?string $checksum;
private ?string $contents;
public function __construct(string $filename, ?string $checksum)
public function __construct(string $filename, ?string $checksum, string $contents = null)
{
$this->filename = $filename;
$this->checksum = $checksum;
$this->contents = $contents;
}
public function getFilename($search = '', $replace = ''): string
@@ -20,13 +22,35 @@ class LocalFile implements File
return Str::replaceFirst($search, $replace, $this->filename);
}
public function getChecksum(): string
public function getChecksum(): ?string
{
return $this->checksum;
}
public function isDirectory(): bool
{
return $this->checksum == null;
return $this->getChecksum() === null;
}
public function getResource()
{
if ($this->contents) {
return $this->contents;
}
return fopen($this->getFilename(), 'r');
}
public function toArray(string $search = '', string $replace = ''): array
{
return [
'sha256' => $this->getChecksum(),
'filename' => $this->getFilename($search, $replace),
];
}
public static function fromArray(array $array): self
{
return new self($array['filename'], $array['sha256']);
}
}