From d0536ebea70465e275404541dae1627961d6c3cc Mon Sep 17 00:00:00 2001 From: Philipp Date: Tue, 10 Aug 2021 22:07:52 +0200 Subject: [PATCH] Rename ISelectableStorage to IWritableStorage --- doc/AddonStorageBackend.md | 25 +++++++++--------- src/Console/Storage.php | 2 +- src/Core/StorageManager.php | 26 +++++++++---------- src/DI.php | 4 +-- src/Model/Attach.php | 4 +-- src/Model/Photo.php | 6 ++--- src/Model/Storage/Database.php | 2 +- src/Model/Storage/Filesystem.php | 2 +- ...ctableStorage.php => IWritableStorage.php} | 6 ++--- src/Module/Admin/Storage.php | 16 ++++++------ static/dependencies.config.php | 4 +-- tests/Util/SampleStorageBackend.php | 4 +-- tests/src/Core/StorageManagerTest.php | 12 ++++----- .../src/Model/Storage/DatabaseStorageTest.php | 4 +-- .../Model/Storage/FilesystemStorageTest.php | 4 +-- tests/src/Model/Storage/StorageTest.php | 6 ++--- 16 files changed, 64 insertions(+), 63 deletions(-) rename src/Model/Storage/{ISelectableStorage.php => IWritableStorage.php} (93%) diff --git a/doc/AddonStorageBackend.md b/doc/AddonStorageBackend.md index b9b7182471..e54960252b 100644 --- a/doc/AddonStorageBackend.md +++ b/doc/AddonStorageBackend.md @@ -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`: assertEquals([ 'filename' => [ diff --git a/src/Console/Storage.php b/src/Console/Storage.php index 70e8e26302..34419c48cd 100644 --- a/src/Console/Storage.php +++ b/src/Console/Storage.php @@ -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.'); diff --git a/src/Core/StorageManager.php b/src/Core/StorageManager.php index 4603b0fded..9e75f83058 100644 --- a/src/Core/StorageManager.php +++ b/src/Core/StorageManager.php @@ -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)) { diff --git a/src/DI.php b/src/DI.php index 9e96ddc015..28ad130b4d 100644 --- a/src/DI.php +++ b/src/DI.php @@ -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); } // diff --git a/src/Model/Attach.php b/src/Model/Attach.php index d3ff5462ca..ac8bca07f1 100644 --- a/src/Model/Attach.php +++ b/src/Model/Attach.php @@ -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'] ?? ''); diff --git a/src/Model/Photo.php b/src/Model/Photo.php index a38743d9d6..28eb6f3611 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -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 { diff --git a/src/Model/Storage/Database.php b/src/Model/Storage/Database.php index a77e268117..3457fa4a30 100644 --- a/src/Model/Storage/Database.php +++ b/src/Model/Storage/Database.php @@ -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'; diff --git a/src/Model/Storage/Filesystem.php b/src/Model/Storage/Filesystem.php index fe5230049f..2d6a352015 100644 --- a/src/Model/Storage/Filesystem.php +++ b/src/Model/Storage/Filesystem.php @@ -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'; diff --git a/src/Model/Storage/ISelectableStorage.php b/src/Model/Storage/IWritableStorage.php similarity index 93% rename from src/Model/Storage/ISelectableStorage.php rename to src/Model/Storage/IWritableStorage.php index d3b7547774..650b3246ed 100644 --- a/src/Model/Storage/ISelectableStorage.php +++ b/src/Model/Storage/IWritableStorage.php @@ -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. diff --git a/src/Module/Admin/Storage.php b/src/Module/Admin/Storage.php index 374d692bf7..e12cecd031 100644 --- a/src/Module/Admin/Storage.php +++ b/src/Module/Admin/Storage.php @@ -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, ]); } diff --git a/static/dependencies.config.php b/static/dependencies.config.php index ea57efca5a..5efe78ddf3 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -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], diff --git a/tests/Util/SampleStorageBackend.php b/tests/Util/SampleStorageBackend.php index 1ebd64f6ee..1185a25646 100644 --- a/tests/Util/SampleStorageBackend.php +++ b/tests/Util/SampleStorageBackend.php @@ -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'; diff --git a/tests/src/Core/StorageManagerTest.php b/tests/src/Core/StorageManagerTest.php index f01640dce4..25ae44b1bc 100644 --- a/tests/src/Core/StorageManagerTest.php +++ b/tests/src/Core/StorageManagerTest.php @@ -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); } } diff --git a/tests/src/Model/Storage/DatabaseStorageTest.php b/tests/src/Model/Storage/DatabaseStorageTest.php index 62c8e7f116..d7b810c1f8 100644 --- a/tests/src/Model/Storage/DatabaseStorageTest.php +++ b/tests/src/Model/Storage/DatabaseStorageTest.php @@ -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()); } diff --git a/tests/src/Model/Storage/FilesystemStorageTest.php b/tests/src/Model/Storage/FilesystemStorageTest.php index a0f267d183..45a7264163 100644 --- a/tests/src/Model/Storage/FilesystemStorageTest.php +++ b/tests/src/Model/Storage/FilesystemStorageTest.php @@ -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' => [ diff --git a/tests/src/Model/Storage/StorageTest.php b/tests/src/Model/Storage/StorageTest.php index 4001bfc68b..340aee8bfd 100644 --- a/tests/src/Model/Storage/StorageTest.php +++ b/tests/src/Model/Storage/StorageTest.php @@ -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