Commit 5a4f85e0 authored by Elias Luhr's avatar Elias Luhr

Merge branch 'dev/FileStorageComponentInterface' into 'master'

add FileStorageComponentInterface

See merge request !2
parents 79c5159f 5f4bd0d6
......@@ -3,7 +3,7 @@
namespace eluhr\flysystemRestApi;
use dmstr\web\traits\AccessBehaviorTrait;
use eluhr\flysystemRestApi\components\FileStorage;
use eluhr\flysystemRestApi\interfaces\FileStorageComponentInterface;
use yii\di\Instance;
class Module extends \yii\base\Module
......@@ -24,7 +24,7 @@ class Module extends \yii\base\Module
{
parent::init();
// Ensure, that file storage component is instance of needed component
Instance::ensure($this->fileStorage, FileStorage::class);
// Ensure, that file storage component is instance of needed component interface
Instance::ensure($this->fileStorage, FileStorageComponentInterface::class);
}
}
......@@ -4,6 +4,7 @@ namespace eluhr\flysystemRestApi\components;
use eluhr\flysystemRestApi\behaviors\PluginsBehavior;
use eluhr\flysystemRestApi\helpers\StorageItemHelper;
use eluhr\flysystemRestApi\interfaces\FileStorageComponentInterface;
use eluhr\flysystemRestApi\interfaces\PluginInterface;
use League\Flysystem\DirectoryListing;
use League\Flysystem\FileAttributes;
......@@ -28,7 +29,7 @@ use eluhr\flysystemRestApi\events\FileStorageEvent;
* @property FilesystemAdapter $adapter
* @property-read Filesystem $filesystem
*/
class FileStorage extends Component
class FileStorage extends Component implements FileStorageComponentInterface
{
/**
* Abstract adapter for the filesystem
......
......@@ -2,7 +2,6 @@
namespace eluhr\flysystemRestApi\controllers;
use eluhr\flysystemRestApi\components\FileStorage;
use eluhr\flysystemRestApi\models\Directory;
use eluhr\flysystemRestApi\models\File;
use eluhr\flysystemRestApi\models\StorageItem;
......
......@@ -2,7 +2,7 @@
namespace eluhr\flysystemRestApi\controllers;
use eluhr\flysystemRestApi\components\FileStorage;
use eluhr\flysystemRestApi\interfaces\FileStorageComponentInterface;
use eluhr\flysystemRestApi\Module;
use Yii;
use yii\base\InvalidConfigException;
......@@ -28,7 +28,7 @@ abstract class BaseController extends Controller
/**
* @throws InvalidConfigException
*/
public function getFileStorage(): FileStorage
public function getFileStorage(): FileStorageComponentInterface
{
return Yii::$app->get($this->module->fileStorage);
}
......
<?php
/**
* @author: Jens Giessmann <jg@handcode.de>
* @copyright: 2023, Jens Giessmann
*/
namespace eluhr\flysystemRestApi\interfaces;
use League\Flysystem\DirectoryListing;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\FilesystemReader;
use League\Flysystem\StorageAttributes;
interface FileStorageComponentInterface
{
/**
* @param string $path relative path to the file
* @param string $contents file contents as string
* @param array $config Config options may vary between adapters
*
* @return bool
*/
public function write(string $path, string $contents, array $config = []): bool;
/**
* @param string $path relative path to the file
* @param resource $stream stream to write to
* @param array $config Config options may vary between adapters
*
* @return bool
*/
public function writeStream(string $path, $stream, array $config = []): bool;
/**
* @param string $sourcePath relative path to the source file
* @param string $destinationPath relative path to the destination file
* @param array $config Config options may vary between adapters
*
* @return bool
*/
public function move(string $sourcePath, string $destinationPath, array $config = []): bool;
/**
* @param string $sourcePath relative path to the source file
* @param string $destinationPath relative path to the destination file
* @param array $config Config options may vary between adapters
*
* @return bool
*/
public function copy(string $sourcePath, string $destinationPath, array $config = []): bool;
/**
* @param string $path relative path to the file
*
* @return string|false
*/
public function read(string $path);
/**
* @param string $path relative path to the file
*
* @return resource|false
*/
public function readStream(string $path);
/**
* @param string $path relative path to the file
*
* @return string|false
*/
public function visibility(string $path);
/**
* @param string $path relative path to the file
* @param string $visibility public|private
*
* @return bool
* @see Visibility
*/
public function setVisibility(string $path, string $visibility): bool;
/**
* @param string $path relative path to the file
*
* @return bool
*/
public function delete(string $path): bool;
/**
* @param string $path relative path to the directory
*
* @return bool
*/
public function deleteDirectory(string $path): bool;
/**
* @param string $path relative path to the directory
*
* @return bool
*/
public function createDirectory(string $path): bool;
/**
* @param string $path relative path to the directory
* @param bool $recursive If true, list recursively the contents of the directory in a flat list.
*
* @return string|false
*/
public function listContents(string $path, bool $recursive = FilesystemReader::LIST_SHALLOW): DirectoryListing|false;
/**
* @param string $path
*
* @return bool
*/
public function fileExists(string $path): bool;
/**
* File size in bytes
*
* @param string $path
*
* @return int|false
*/
public function fileSize(string $path): bool|int;
/**
* @param string $path
*
* @return string|false
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
*/
public function mimeType(string $path): bool|string;
/**
* @param string $path
*
* @return int|false
*/
public function lastModified(string $path): bool|int;
/**
* @return FilesystemAdapter
*/
public function getAdapter(): FilesystemAdapter;
/**
* @param FilesystemAdapter $adapter
*/
public function setAdapter(FilesystemAdapter $adapter): void;
/**
* @param StorageAttributes $storageAttributes
*
* @return array
*/
public static function buildItem(StorageAttributes $storageAttributes): array;
}
\ No newline at end of file
......@@ -2,12 +2,12 @@
namespace eluhr\flysystemRestApi\models;
use eluhr\flysystemRestApi\components\FileStorage;
use eluhr\flysystemRestApi\interfaces\FileStorageComponentInterface;
use yii\base\Model;
abstract class FileStorageModel extends Model
{
private FileStorage $_fileStorage;
private FileStorageComponentInterface $_fileStorage;
/**
* @inheritdoc
......@@ -19,17 +19,17 @@ abstract class FileStorageModel extends Model
}
/**
* @return \eluhr\flysystemRestApi\components\FileStorage
* @return FileStorageComponentInterface
*/
public function getFileStorage(): \eluhr\flysystemRestApi\components\FileStorage
public function getFileStorage(): FileStorageComponentInterface
{
return $this->_fileStorage;
}
/**
* @param \eluhr\flysystemRestApi\components\FileStorage $fileStorage
* @param FileStorageComponentInterface $fileStorage
*/
public function setFileStorage(\eluhr\flysystemRestApi\components\FileStorage $fileStorage): void
public function setFileStorage(FileStorageComponentInterface $fileStorage): void
{
$this->_fileStorage = $fileStorage;
}
......
......@@ -167,7 +167,7 @@ class StorageItem extends ActiveRecord implements StorageAttributes
$transaction->rollBack();
return false;
} else {
$items[] = FileStorage::buildItem($item);
$items[] = static::getFileStorage()::buildItem($item);
}
}
......
......@@ -2,12 +2,12 @@
namespace eluhr\flysystemRestApi\traits;
use eluhr\flysystemRestApi\components\FileStorage;
use eluhr\flysystemRestApi\interfaces\FileStorageComponentInterface;
use Yii;
trait FileStorageTrait
{
public function getFileStorage(): FileStorage
public static function getFileStorage(): FileStorageComponentInterface
{
return Yii::$app->get('fileStorage');
}
......
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