Commit fab0aea5 authored by Richard Weinhold's avatar Richard Weinhold 🎩

fixes flysystem auto-free storage

parent 17429459
Pipeline #5405 passed with stages
in 9 seconds
......@@ -56,7 +56,7 @@ class Flysystem extends Storage
/**
* @return array
*/
protected function metadata(): array
public function getMetadata(): array
{
if ($this->metadata === null) {
$this->metadata = $this->flysystem->getMetadata($this->path);
......@@ -72,11 +72,13 @@ class Flysystem extends Storage
{
if (!$this->selfdestruct || !file_exists($this->path)) {
return;
} elseif (!$this->flysystem->has($this->path)) {
return;
}
if (is_file($this->path)) {
if ($this->getMetadata()['type'] === 'file') {
$this->removeFile();
} elseif (is_dir($this->path)) {
} elseif ($this->getMetadata()['type'] === 'dir') {
$this->removeDir();
}
}
......@@ -114,7 +116,7 @@ class Flysystem extends Storage
*/
public function isFile(): bool
{
return $this->flysystem->has($this->path) && $this->metadata()['type'] === 'file';
return $this->flysystem->has($this->path) && $this->getMetadata()['type'] === 'file';
}
/**
......@@ -122,7 +124,7 @@ class Flysystem extends Storage
*/
public function isDir(): bool
{
return $this->flysystem->has($this->path) && $this->metadata()['type'] === 'dir';
return $this->flysystem->has($this->path) && $this->getMetadata()['type'] === 'dir';
}
/**
......@@ -138,7 +140,7 @@ class Flysystem extends Storage
*/
public function isSymlink(): bool
{
return $this->flysystem->has($this->path) && $this->metadata()['type'] === 'link';
return $this->flysystem->has($this->path) && $this->getMetadata()['type'] === 'link';
}
/**
......@@ -233,15 +235,15 @@ class Flysystem extends Storage
public function removeDir(): bool
{
if (!$this->isDir()) {
throw new AccessDeniedException(sprintf('unable to remove directory: "%s"', $this->path), 500);
throw new AccessDeniedException(sprintf('unable to remove path, not a directory: "%s"', $this->path), 500);
}
if ($this->flysystem->deleteDir($this->path)) {
$this->metadata = null;
return true;
if (!$this->flysystem->deleteDir($this->path)) {
return false;
}
return false;
$this->metadata = null;
return true;
}
/**
......
......@@ -4,6 +4,8 @@ namespace ricwein\FileSystem\Tests\Storage;
use PHPUnit\Framework\TestCase;
use ricwein\FileSystem\File;
use ricwein\FileSystem\Helper\Constraint;
use ricwein\FileSystem\Storage;
use ricwein\FileSystem\Directory;
use League\Flysystem\Adapter\Local;
......@@ -28,6 +30,7 @@ class FlysystemTest extends TestCase
*/
public function testFileRead()
{
$cmpFile = new File(new Storage\Disk(__DIR__, '/../_examples', 'test.txt'), Constraint::LOOSE);
$file = new File(new Storage\Flysystem(new Local(__DIR__.'/../_examples'), 'test.txt'));
$this->assertTrue($file->isFile());
......@@ -35,6 +38,13 @@ class FlysystemTest extends TestCase
$file->read(),
file_get_contents(__DIR__ . '/../_examples/test.txt')
);
$this->assertSame([
'type' => 'file',
'path' => 'test.txt',
'timestamp' => $cmpFile->getTime(),
'size' => $cmpFile->getSize(),
], $file->storage()->getMetadata());
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment