Skip to content

Commit

Permalink
feat: create FilesystemInterface and FilesystemException
Browse files Browse the repository at this point in the history
  • Loading branch information
augustas committed Oct 23, 2024
1 parent 6b88264 commit 45e5f47
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 42 deletions.
2 changes: 0 additions & 2 deletions common/oatbox/filesystem/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

namespace oat\oatbox\filesystem;

use League\Flysystem\FilesystemException;

class Directory extends FileSystemHandler implements \IteratorAggregate
{
public const ITERATOR_RECURSIVE = '1';
Expand Down
1 change: 0 additions & 1 deletion common/oatbox/filesystem/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

use GuzzleHttp\Psr7\Stream;
use GuzzleHttp\Psr7\StreamWrapper;
use League\Flysystem\FilesystemException;
use Psr\Http\Message\StreamInterface;
use tao_helpers_File;

Expand Down
2 changes: 1 addition & 1 deletion common/oatbox/filesystem/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* Class Filesystem
*/
class FileSystem implements FilesystemOperator
class FileSystem implements FilesystemInterface
{
use FileSystemWrapperTrait;

Expand Down
4 changes: 2 additions & 2 deletions common/oatbox/filesystem/FileSystemService.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function hasDirectory($id)
* Retrieve an existing FileSystem by ID.
*
* @param string $id
* @return FilesystemOperator
* @return FileSystem
* @throws \common_exception_Error
* @throws \common_exception_NotFound
*/
Expand All @@ -117,7 +117,7 @@ public function getFileSystem($id)
*
* @param string $id
* @param string $subPath
* @return FilesystemOperator
* @return FileSystem
*/
public function createFileSystem($id, $subPath = null)
{
Expand Down
10 changes: 10 additions & 0 deletions common/oatbox/filesystem/FilesystemException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace oat\oatbox\filesystem;

use League\Flysystem\FilesystemException as FlyFilesystemException;
use RuntimeException;

class FilesystemException extends RuntimeException implements FlyFilesystemException
{
}
10 changes: 10 additions & 0 deletions common/oatbox/filesystem/FilesystemInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace oat\oatbox\filesystem;

use League\Flysystem\FilesystemOperator;

interface FilesystemInterface extends FilesystemOperator
{

}
119 changes: 83 additions & 36 deletions common/oatbox/filesystem/utils/FileSystemWrapperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
namespace oat\oatbox\filesystem\utils;

use League\Flysystem\DirectoryListing;
use League\Flysystem\FilesystemException as FlyFilesystemException;
use League\Flysystem\FilesystemOperator;
use oat\oatbox\filesystem\FilesystemException;

/**
* A trait to facilitate creation of filesystem wrappers
Expand All @@ -33,164 +35,209 @@ trait FileSystemWrapperTrait
{
/**
* @see FilesystemOperator::has
* @inheritDoc
* @throws FilesystemException
*/
public function has(string $location): bool
{
return $this->getFileSystem()->has($this->getFullPath($location));
return $this->wrapFileSystemOperation(function () use ($location) {
return $this->getFileSystem()->has($this->getFullPath($location));
});
}

/**
* @see FilesystemOperator::directoryExists
* @inheritDoc
* @throws FilesystemException
*/
public function directoryExists(string $location): bool
{
return $this->getFileSystem()->directoryExists($this->getFullPath($location));
return $this->wrapFileSystemOperation(function () use ($location) {
return $this->getFileSystem()->directoryExists($this->getFullPath($location));
});
}

/**
* @see FilesystemOperator::read
* @inheritDoc
* @throws FilesystemException
*/
public function read(string $location): string
{
return $this->getFileSystem()->read($this->getFullPath($location));
return $this->wrapFileSystemOperation(function () use ($location) {
return $this->getFileSystem()->read($this->getFullPath($location));
});
}

/**
* @see FilesystemOperator::readStream
* @inheritDoc
* @throws FilesystemException
*/
public function readStream($path)
{
return $this->getFileSystem()->readStream($this->getFullPath($path));
return $this->wrapFileSystemOperation(function () use ($path) {
return $this->getFileSystem()->readStream($this->getFullPath($path));
});
}

/**
* @see FilesystemOperator::listContents
* @inheritDoc
* @throws FilesystemException
*/
public function listContents(string $location = '', bool $deep = self::LIST_SHALLOW): DirectoryListing
{
return $this->getFileSystem()->listContents($this->getFullPath($location), $deep);
return $this->wrapFileSystemOperation(function () use ($location, $deep) {
return $this->getFileSystem()->listContents($this->getFullPath($location), $deep);
});
}

/**
* @see FilesystemOperator::write
* @inheritDoc
* @throws FilesystemException
*/
public function write(string $location, string $contents, array $config = []): void
{
$this->getFileSystem()->write($this->getFullPath($location), $contents, $config);
$this->wrapFileSystemOperation(function () use ($location, $contents, $config) {
$this->getFileSystem()->write($this->getFullPath($location), $contents, $config);
});
}

/**
* @see FilesystemOperator::writeStream
* @inheritDoc
* @throws FilesystemException
*/
public function writeStream(string $location, $contents, array $config = []): void
{
$this->getFileSystem()->writeStream($this->getFullPath($location), $contents, $config);
$this->wrapFileSystemOperation(function () use ($location, $contents, $config) {
$this->getFileSystem()->writeStream($this->getFullPath($location), $contents, $config);
});
}

/**
* @see FilesystemOperator::copy
* @inheritDoc
* @throws FilesystemException
*/
public function copy(string $source, string $destination, array $config = []): void
{
$this->getFileSystem()->copy($this->getFullPath($source), $destination);
$this->wrapFileSystemOperation(function () use ($source, $destination, $config) {
$this->getFileSystem()->copy($this->getFullPath($source), $destination, $config);
});
}

/**
* @see FilesystemOperator::delete
* @inheritDoc
* @throws FilesystemException
*/
public function delete(string $location): void
{
$this->getFileSystem()->delete($this->getFullPath($location));
$this->wrapFileSystemOperation(function () use ($location) {
$this->getFileSystem()->delete($this->getFullPath($location));
});
}

/**
* @see FilesystemOperator::setVisibility
* @inheritDoc
* @throws FilesystemException
*/
public function setVisibility(string $path, string $visibility): void
{
$this->getFileSystem()->setVisibility($this->getFullPath($path), $visibility);
$this->wrapFileSystemOperation(function () use ($path, $visibility) {
$this->getFileSystem()->setVisibility($this->getFullPath($path), $visibility);
});
}

/**
* @see FilesystemOperator::fileExists
* @inheritDoc
* @throws FilesystemException
*/
public function fileExists(string $location): bool
{
return $this->getFileSystem()->fileExists($this->getFullPath($location));
return $this->wrapFileSystemOperation(function () use ($location) {
return $this->getFileSystem()->fileExists($this->getFullPath($location));
});
}

/**
* @see FilesystemOperator::lastModified
* @inheritDoc
* @throws FilesystemException
*/
public function lastModified(string $path): int
{
return $this->getFileSystem()->lastModified($this->getFullPath($path));
return $this->wrapFileSystemOperation(function () use ($path) {
return $this->getFileSystem()->lastModified($this->getFullPath($path));
});
}

/**
* @see FilesystemOperator::fileSize
* @inheritDoc
* @throws FilesystemException
*/
public function fileSize(string $path): int
{
return $this->getFileSystem()->fileSize($this->getFullPath($path));
return $this->wrapFileSystemOperation(function () use ($path) {
return $this->getFileSystem()->fileSize($this->getFullPath($path));
});
}

/**
* @see FilesystemOperator::mimeType
* @inheritDoc
* @throws FilesystemException
*/
public function mimeType(string $path): string
{
return $this->getFileSystem()->mimeType($this->getFullPath($path));
return $this->wrapFileSystemOperation(function () use ($path) {
return $this->getFileSystem()->mimeType($this->getFullPath($path));
});
}

/**
* @see FilesystemOperator::visibility
* @inheritDoc
* @throws FilesystemException
*/
public function visibility(string $path): string
{
return $this->getFileSystem()->visibility($this->getFullPath($path));
return $this->wrapFileSystemOperation(function () use ($path) {
return $this->getFileSystem()->visibility($this->getFullPath($path));
});
}

/**
* @see FilesystemOperator::deleteDirectory
* @inheritDoc
* @throws FilesystemException
*/
public function deleteDirectory(string $location): void
{
$this->getFileSystem()->deleteDirectory($this->getFullPath($location));
$this->wrapFileSystemOperation(function () use ($location) {
$this->getFileSystem()->deleteDirectory($this->getFullPath($location));
});
}

/**
* @see FilesystemOperator::createDirectory
* @inheritDoc
* @throws FilesystemException
*/
public function createDirectory(string $location, array $config = []): void
{
$this->getFileSystem()->createDirectory($this->getFullPath($location), $config);
$this->wrapFileSystemOperation(function () use ($location, $config) {
$this->getFileSystem()->createDirectory($this->getFullPath($location), $config);
});
}

/**
* @see FilesystemOperator::move
* @inheritDoc
* @throws FilesystemException
*/
public function move(string $source, string $destination, array $config = []): void
{
$this->getFileSystem()->move($this->getFullPath($source), $destination, $config);
$this->wrapFileSystemOperation(function () use ($source, $destination, $config) {
$this->getFileSystem()->move($this->getFullPath($source), $destination, $config);
});
}

private function wrapFileSystemOperation(callable $operation)
{
try {
return $operation();
} catch (FlyFilesystemException $e) {
throw new FilesystemException($e->getMessage(), $e->getCode(), $e);
}
}

abstract protected function getFileSystem(): FilesystemOperator;
Expand Down

0 comments on commit 45e5f47

Please sign in to comment.