Rename ISelectableStorage to IWritableStorage

This commit is contained in:
Philipp Holzer 2021-08-10 22:07:52 +02:00
parent eb035771f1
commit d0536ebea7
No known key found for this signature in database
GPG key ID: 9A28B7D4FF5667BD
16 changed files with 64 additions and 63 deletions

View file

@ -10,12 +10,12 @@ A storage backend is implemented as a class, and the plugin register the class t
The class must live in `Friendica\Addon\youraddonname` namespace, where `youraddonname` the folder name of your addon.
The class must implement `Friendica\Model\Storage\ISelectableStorage` interface. All method in the interface must be implemented:
The class must implement `Friendica\Model\Storage\IWritableStorage` interface. All method in the interface must be implemented:
namespace Friendica\Model\ISelectableStorage;
namespace Friendica\Model\IWritableStorage;
```php
interface ISelectableStorage
interface IWritableStorage
{
public function get(string $reference);
public function put(string $data, string $reference = '');
@ -79,7 +79,7 @@ Each label should be translatable
];
See doxygen documentation of `ISelectableStorage` interface for details about each method.
See doxygen documentation of `IWritableStorage` interface for details about each method.
## Register a storage backend class
@ -105,8 +105,9 @@ Each new Storage class should be added to the test-environment at [Storage Tests
Add a new test class which's naming convention is `StorageClassTest`, which extend the `StorageTest` in the same directory.
Override the two necessary instances:
```php
use Friendica\Model\Storage\ISelectableStorage;
use Friendica\Model\Storage\IWritableStorage;
abstract class StorageTest
{
@ -114,7 +115,7 @@ abstract class StorageTest
abstract protected function getInstance();
// Assertion for the option array you return for your new StorageClass
abstract protected function assertOption(ISelectableStorage $storage);
abstract protected function assertOption(IWritableStorage $storage);
}
```
@ -138,9 +139,9 @@ If there's a predecessor to this exception (e.g. you caught an exception and are
Example:
```php
use Friendica\Model\Storage\ISelectableStorage;
use Friendica\Model\Storage\IWritableStorage;
class ExampleStorage implements ISelectableStorage
class ExampleStorage implements IWritableStorage
{
public function get(string $reference) : string
{
@ -168,12 +169,12 @@ The file will be `addon/samplestorage/SampleStorageBackend.php`:
<?php
namespace Friendica\Addon\samplestorage;
use Friendica\Model\Storage\ISelectableStorage;
use Friendica\Model\Storage\IWritableStorage;
use Friendica\Core\Config\IConfig;
use Friendica\Core\L10n;
class SampleStorageBackend implements ISelectableStorage
class SampleStorageBackend implements IWritableStorage
{
const NAME = 'Sample Storage';
@ -305,7 +306,7 @@ function samplestorage_storage_instance(\Friendica\App $a, array $data)
**Theoretically - until tests for Addons are enabled too - create a test class with the name `addon/tests/SampleStorageTest.php`:
```php
use Friendica\Model\Storage\ISelectableStorage;
use Friendica\Model\Storage\IWritableStorage;
use Friendica\Test\src\Model\Storage\StorageTest;
class SampleStorageTest extends StorageTest
@ -319,7 +320,7 @@ class SampleStorageTest extends StorageTest
}
// Assertion for the option array you return for your new StorageClass
protected function assertOption(ISelectableStorage $storage)
protected function assertOption(IWritableStorage $storage)
{
$this->assertEquals([
'filename' => [

View file

@ -132,7 +132,7 @@ HELP;
}
$name = $this->args[1];
$class = $this->storageManager->getSelectableStorageByName($name);
$class = $this->storageManager->getWritableStorageByName($name);
if (is_null($class)) {
$this->out($name . ' is not a registered backend.');

View file

@ -82,13 +82,13 @@ class StorageManager
$currentName = $this->config->get('storage', 'name', '');
// you can only use user backends as a "default" backend, so the second parameter is true
$this->currentBackend = $this->getSelectableStorageByName($currentName);
$this->currentBackend = $this->getWritableStorageByName($currentName);
}
/**
* Return current storage backend class
*
* @return Storage\ISelectableStorage|null
* @return Storage\IWritableStorage|null
*/
public function getBackend()
{
@ -96,16 +96,16 @@ class StorageManager
}
/**
* Returns a selectable storage backend class by registered name
* Returns a writable storage backend class by registered name
*
* @param string $name Backend name
*
* @return Storage\ISelectableStorage
* @return Storage\IWritableStorage
*
* @throws Storage\ReferenceStorageException in case there's no backend class for the name
* @throws Storage\StorageException in case of an unexpected failure during the hook call
*/
public function getSelectableStorageByName(string $name = null)
public function getWritableStorageByName(string $name = null)
{
// @todo 2020.09 Remove this call after 2 releases
$name = $this->checkLegacyBackend($name);
@ -130,7 +130,7 @@ class StorageManager
];
try {
Hook::callAll('storage_instance', $data);
if (($data['storage'] ?? null) instanceof Storage\ISelectableStorage) {
if (($data['storage'] ?? null) instanceof Storage\IWritableStorage) {
$this->backendInstances[$data['name'] ?? $name] = $data['storage'];
} else {
throw new Storage\ReferenceStorageException(sprintf('Backend %s was not found', $name));
@ -244,11 +244,11 @@ class StorageManager
/**
* Set current storage backend class
*
* @param Storage\ISelectableStorage $storage The storage class
* @param Storage\IWritableStorage $storage The storage class
*
* @return boolean True, if the set was successful
*/
public function setBackend(Storage\ISelectableStorage $storage)
public function setBackend(Storage\IWritableStorage $storage)
{
if ($this->config->set('storage', 'name', $storage::getName())) {
$this->currentBackend = $storage;
@ -327,15 +327,15 @@ class StorageManager
* Copy existing data to destination storage and delete from source.
* This method cannot move to legacy in-table `data` field.
*
* @param Storage\ISelectableStorage $destination Destination storage class name
* @param array $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
* @param int $limit Limit of the process batch size, defaults to 5000
* @param Storage\IWritableStorage $destination Destination storage class name
* @param array $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
* @param int $limit Limit of the process batch size, defaults to 5000
*
* @return int Number of moved resources
* @throws Storage\StorageException
* @throws Exception
*/
public function move(Storage\ISelectableStorage $destination, array $tables = self::TABLES, int $limit = 5000)
public function move(Storage\IWritableStorage $destination, array $tables = self::TABLES, int $limit = 5000)
{
if (!$this->isValidBackend($destination, true)) {
throw new Storage\StorageException(sprintf("Can't move to storage backend '%s'", $destination::getName()));
@ -354,7 +354,7 @@ class StorageManager
while ($resource = $this->dba->fetch($resources)) {
$id = $resource['id'];
$data = $resource['data'];
$source = $this->getSelectableStorageByName($resource['backend-class']);
$source = $this->getWritableStorageByName($resource['backend-class']);
$sourceRef = $resource['backend-ref'];
if (!empty($source)) {

View file

@ -387,11 +387,11 @@ abstract class DI
}
/**
* @return Model\Storage\ISelectableStorage
* @return Model\Storage\IWritableStorage
*/
public static function storage()
{
return self::$dice->create(Model\Storage\ISelectableStorage::class);
return self::$dice->create(Model\Storage\IWritableStorage::class);
}
//

View file

@ -284,7 +284,7 @@ class Attach
$items = self::selectToArray(['backend-class','backend-ref'], $conditions);
foreach($items as $item) {
$backend_class = DI::storageManager()->getSelectableStorageByName($item['backend-class'] ?? '');
$backend_class = DI::storageManager()->getWritableStorageByName($item['backend-class'] ?? '');
if (!empty($backend_class)) {
$fields['backend-ref'] = $backend_class->put($img->asString(), $item['backend-ref'] ?? '');
} else {
@ -316,7 +316,7 @@ class Attach
$items = self::selectToArray(['backend-class','backend-ref'], $conditions);
foreach($items as $item) {
$backend_class = DI::storageManager()->getSelectableStorageByName($item['backend-class'] ?? '');
$backend_class = DI::storageManager()->getWritableStorageByName($item['backend-class'] ?? '');
if (!empty($backend_class)) {
try {
$backend_class->delete($item['backend-ref'] ?? '');

View file

@ -347,7 +347,7 @@ class Photo
if (DBA::isResult($existing_photo)) {
$backend_ref = (string)$existing_photo["backend-ref"];
$storage = DI::storageManager()->getSelectableStorageByName($existing_photo["backend-class"] ?? '');
$storage = DI::storageManager()->getWritableStorageByName($existing_photo["backend-class"] ?? '');
} else {
$storage = DI::storage();
}
@ -411,7 +411,7 @@ class Photo
$photos = DBA::select('photo', ['id', 'backend-class', 'backend-ref'], $conditions);
while ($photo = DBA::fetch($photos)) {
$backend_class = DI::storageManager()->getSelectableStorageByName($photo['backend-class'] ?? '');
$backend_class = DI::storageManager()->getWritableStorageByName($photo['backend-class'] ?? '');
if (!empty($backend_class)) {
try {
$backend_class->delete($item['backend-ref'] ?? '');
@ -448,7 +448,7 @@ class Photo
$photos = self::selectToArray(['backend-class', 'backend-ref'], $conditions);
foreach($photos as $photo) {
$backend_class = DI::storageManager()->getSelectableStorageByName($photo['backend-class'] ?? '');
$backend_class = DI::storageManager()->getWritableStorageByName($photo['backend-class'] ?? '');
if (!empty($backend_class)) {
$fields["backend-ref"] = $backend_class->put($img->asString(), $photo['backend-ref']);
} else {

View file

@ -29,7 +29,7 @@ use Friendica\Database\Database as DBA;
*
* This class manage data stored in database table.
*/
class Database implements ISelectableStorage
class Database implements IWritableStorage
{
const NAME = 'Database';

View file

@ -36,7 +36,7 @@ use Friendica\Util\Strings;
* Each new resource gets a value as reference and is saved in a
* folder tree stucture created from that value.
*/
class Filesystem implements ISelectableStorage
class Filesystem implements IWritableStorage
{
const NAME = 'Filesystem';

View file

@ -22,12 +22,12 @@
namespace Friendica\Model\Storage;
/**
* Interface for selectable storage backends
* Interface for writable storage backends
*
* Used for storages with CRUD functionality, mainly used for user data (e.g. photos, attachements).
* There's only one active, selectable storage possible and can be selected by the current administrator
* There's only one active, writable storage possible. This type of storages are selectable by the current administrator
*/
interface ISelectableStorage extends IStorage
interface IWritableStorage extends IStorage
{
/**
* Put data in backend as $ref. If $ref is not defined a new reference is created.

View file

@ -23,7 +23,7 @@ namespace Friendica\Module\Admin;
use Friendica\Core\Renderer;
use Friendica\DI;
use Friendica\Model\Storage\ISelectableStorage;
use Friendica\Model\Storage\IWritableStorage;
use Friendica\Module\BaseAdmin;
use Friendica\Util\Strings;
@ -37,8 +37,8 @@ class Storage extends BaseAdmin
$storagebackend = Strings::escapeTags(trim($parameters['name'] ?? ''));
/** @var ISelectableStorage $newstorage */
$newstorage = DI::storageManager()->getSelectableStorageByName($storagebackend);
/** @var IWritableStorage $newstorage */
$newstorage = DI::storageManager()->getWritableStorageByName($storagebackend);
// save storage backend form
$storage_opts = $newstorage->getOptions();
@ -68,8 +68,8 @@ class Storage extends BaseAdmin
}
if (!empty($_POST['submit_save_set'])) {
/** @var ISelectableStorage $newstorage */
$newstorage = DI::storageManager()->getSelectableStorageByName($storagebackend);
/** @var IWritableStorage $newstorage */
$newstorage = DI::storageManager()->getWritableStorageByName($storagebackend);
if (!DI::storageManager()->setBackend($newstorage)) {
notice(DI::l10n()->t('Invalid storage backend setting value.'));
@ -92,7 +92,7 @@ class Storage extends BaseAdmin
$storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $name);
$storage_form = [];
foreach (DI::storageManager()->getSelectableStorageByName($name)->getOptions() as $option => $info) {
foreach (DI::storageManager()->getWritableStorageByName($name)->getOptions() as $option => $info) {
$type = $info[0];
// Backward compatibilty with yesno field description
if ($type == 'yesno') {
@ -111,7 +111,7 @@ class Storage extends BaseAdmin
'name' => $name,
'prefix' => $storage_form_prefix,
'form' => $storage_form,
'active' => $current_storage_backend instanceof ISelectableStorage && $name === $current_storage_backend::getName(),
'active' => $current_storage_backend instanceof IWritableStorage && $name === $current_storage_backend::getName(),
];
}
@ -127,7 +127,7 @@ class Storage extends BaseAdmin
'$noconfig' => DI::l10n()->t('This backend doesn\'t have custom settings'),
'$baseurl' => DI::baseUrl()->get(true),
'$form_security_token' => self::getFormSecurityToken("admin_storage"),
'$storagebackend' => $current_storage_backend instanceof ISelectableStorage ? $current_storage_backend::getName() : DI::l10n()->t('Database (legacy)'),
'$storagebackend' => $current_storage_backend instanceof IWritableStorage ? $current_storage_backend::getName() : DI::l10n()->t('Database (legacy)'),
'$availablestorageforms' => $available_storage_forms,
]);
}

View file

@ -44,7 +44,7 @@ use Friendica\Core\Session\ISession;
use Friendica\Core\StorageManager;
use Friendica\Database\Database;
use Friendica\Factory;
use Friendica\Model\Storage\ISelectableStorage;
use Friendica\Model\Storage\IWritableStorage;
use Friendica\Model\User\Cookie;
use Friendica\Network;
use Friendica\Util;
@ -213,7 +213,7 @@ return [
$_SERVER, $_COOKIE
],
],
ISelectableStorage::class => [
IWritableStorage::class => [
'instanceOf' => StorageManager::class,
'call' => [
['getBackend', [], Dice::CHAIN_CALL],

View file

@ -22,14 +22,14 @@
namespace Friendica\Test\Util;
use Friendica\Core\Hook;
use Friendica\Model\Storage\ISelectableStorage;
use Friendica\Model\Storage\IWritableStorage;
use Friendica\Core\L10n;
/**
* A backend storage example class
*/
class SampleStorageBackend implements ISelectableStorage
class SampleStorageBackend implements IWritableStorage
{
const NAME = 'Sample Storage';

View file

@ -178,7 +178,7 @@ class StorageManagerTest extends DatabaseTest
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
if ($userBackend) {
$storage = $storageManager->getSelectableStorageByName($name);
$storage = $storageManager->getWritableStorageByName($name);
} else {
$storage = $storageManager->getByName($name);
}
@ -230,7 +230,7 @@ class StorageManagerTest extends DatabaseTest
self::assertNull($storageManager->getBackend());
if ($userBackend) {
$selBackend = $storageManager->getSelectableStorageByName($name);
$selBackend = $storageManager->getWritableStorageByName($name);
$storageManager->setBackend($selBackend);
self::assertInstanceOf($assert, $storageManager->getBackend());
@ -287,7 +287,7 @@ class StorageManagerTest extends DatabaseTest
SampleStorageBackend::registerHook();
Hook::loadHooks();
self::assertTrue($storageManager->setBackend( $storageManager->getSelectableStorageByName(SampleStorageBackend::NAME)));
self::assertTrue($storageManager->setBackend( $storageManager->getWritableStorageByName(SampleStorageBackend::NAME)));
self::assertEquals(SampleStorageBackend::NAME, $this->config->get('storage', 'name'));
self::assertInstanceOf(SampleStorageBackend::class, $storageManager->getBackend());
@ -314,7 +314,7 @@ class StorageManagerTest extends DatabaseTest
$this->loadFixture(__DIR__ . '/../../datasets/storage/database.fixture.php', $this->dba);
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
$storage = $storageManager->getSelectableStorageByName($name);
$storage = $storageManager->getWritableStorageByName($name);
$storageManager->move($storage);
$photos = $this->dba->select('photo', ['backend-ref', 'backend-class', 'id', 'data']);
@ -333,12 +333,12 @@ class StorageManagerTest extends DatabaseTest
/**
* Test moving data to a WRONG storage
*/
public function testWrongSelectableStorage()
public function testWrongWritableStorage()
{
$this->expectException(\TypeError::class);
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
$storage = $storageManager->getSelectableStorageByName(Storage\SystemResource::getName());
$storage = $storageManager->getWritableStorageByName(Storage\SystemResource::getName());
$storageManager->move($storage);
}
}

View file

@ -23,7 +23,7 @@ namespace Friendica\Test\src\Model\Storage;
use Friendica\Factory\ConfigFactory;
use Friendica\Model\Storage\Database;
use Friendica\Model\Storage\ISelectableStorage;
use Friendica\Model\Storage\IWritableStorage;
use Friendica\Test\DatabaseTestTrait;
use Friendica\Test\Util\Database\StaticDatabase;
use Friendica\Test\Util\VFSTrait;
@ -63,7 +63,7 @@ class DatabaseStorageTest extends StorageTest
return new Database($dba);
}
protected function assertOption(ISelectableStorage $storage)
protected function assertOption(IWritableStorage $storage)
{
self::assertEmpty($storage->getOptions());
}

View file

@ -24,7 +24,7 @@ namespace Friendica\Test\src\Model\Storage;
use Friendica\Core\Config\IConfig;
use Friendica\Core\L10n;
use Friendica\Model\Storage\Filesystem;
use Friendica\Model\Storage\ISelectableStorage;
use Friendica\Model\Storage\IWritableStorage;
use Friendica\Model\Storage\StorageException;
use Friendica\Test\Util\VFSTrait;
use Friendica\Util\Profiler;
@ -64,7 +64,7 @@ class FilesystemStorageTest extends StorageTest
return new Filesystem($this->config, $l10n);
}
protected function assertOption(ISelectableStorage $storage)
protected function assertOption(IWritableStorage $storage)
{
self::assertEquals([
'storagepath' => [

View file

@ -21,17 +21,17 @@
namespace Friendica\Test\src\Model\Storage;
use Friendica\Model\Storage\ISelectableStorage;
use Friendica\Model\Storage\IWritableStorage;
use Friendica\Model\Storage\IStorage;
use Friendica\Model\Storage\ReferenceStorageException;
use Friendica\Test\MockedTest;
abstract class StorageTest extends MockedTest
{
/** @return ISelectableStorage */
/** @return IWritableStorage */
abstract protected function getInstance();
abstract protected function assertOption(ISelectableStorage $storage);
abstract protected function assertOption(IWritableStorage $storage);
/**
* Test if the instance is "really" implementing the interface