From 2ab0d06996410f68cf501e6e2014bf4829b121ae Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 23 Oct 2021 12:11:38 +0200 Subject: [PATCH 1/4] Restructure Storage to new paradigm --- doc/AddonStorageBackend.md | 44 +++--- doc/Addons.md | 4 +- src/Console/Storage.php | 8 +- .../Capability/ICanConfigureStorage.php} | 4 +- .../Capability/ICanReadFromStorage.php} | 9 +- .../Capability/ICanWriteToStorage.php} | 7 +- .../InvalidClassStorageException.php | 2 +- .../Exception}/ReferenceStorageException.php | 2 +- .../Storage/Exception}/StorageException.php | 2 +- .../Repository}/StorageManager.php | 132 +++++++++--------- .../Storage/Type}/Database.php | 9 +- .../Storage/Type}/ExternalResource.php | 8 +- .../Storage/Type}/Filesystem.php | 9 +- .../Storage/Type}/FilesystemConfig.php | 5 +- .../Storage/Type}/SystemResource.php | 10 +- src/DI.php | 8 +- src/Model/Attach.php | 4 +- src/Model/Photo.php | 10 +- src/Module/Admin/Storage.php | 14 +- src/Module/Photo.php | 4 +- static/dependencies.config.php | 6 +- tests/Util/SampleStorageBackend.php | 6 +- .../Storage/DatabaseStorageTest.php | 4 +- .../Storage/FilesystemStorageConfigTest.php | 8 +- .../Storage/FilesystemStorageTest.php | 8 +- .../Repository}/StorageManagerTest.php | 75 +++++----- .../Storage/StorageConfigTest.php | 8 +- .../{Model => Core}/Storage/StorageTest.php | 12 +- update.php | 6 +- 29 files changed, 229 insertions(+), 199 deletions(-) rename src/{Model/Storage/IStorageConfiguration.php => Core/Storage/Capability/ICanConfigureStorage.php} (97%) rename src/{Model/Storage/IStorage.php => Core/Storage/Capability/ICanReadFromStorage.php} (84%) rename src/{Model/Storage/IWritableStorage.php => Core/Storage/Capability/ICanWriteToStorage.php} (88%) rename src/{Model/Storage => Core/Storage/Exception}/InvalidClassStorageException.php (95%) rename src/{Model/Storage => Core/Storage/Exception}/ReferenceStorageException.php (95%) rename src/{Model/Storage => Core/Storage/Exception}/StorageException.php (95%) rename src/Core/{ => Storage/Repository}/StorageManager.php (65%) rename src/{Model/Storage => Core/Storage/Type}/Database.php (92%) rename src/{Model/Storage => Core/Storage/Type}/ExternalResource.php (89%) rename src/{Model/Storage => Core/Storage/Type}/Filesystem.php (94%) rename src/{Model/Storage => Core/Storage/Type}/FilesystemConfig.php (93%) rename src/{Model/Storage => Core/Storage/Type}/SystemResource.php (86%) rename tests/src/{Model => Core}/Storage/DatabaseStorageTest.php (95%) rename tests/src/{Model => Core}/Storage/FilesystemStorageConfigTest.php (89%) rename tests/src/{Model => Core}/Storage/FilesystemStorageTest.php (94%) rename tests/src/Core/{ => Storage/Repository}/StorageManagerTest.php (80%) rename tests/src/{Model => Core}/Storage/StorageConfigTest.php (79%) rename tests/src/{Model => Core}/Storage/StorageTest.php (88%) diff --git a/doc/AddonStorageBackend.md b/doc/AddonStorageBackend.md index 0b0672376..eaa58cd4d 100644 --- a/doc/AddonStorageBackend.md +++ b/doc/AddonStorageBackend.md @@ -4,7 +4,7 @@ Friendica Storage Backend Addon development * [Home](help) Storage backends can be added via addons. -A storage backend is implemented as a class, and the plugin register the class to make it avaiable to the system. +A storage backend is implemented as a class, and the plugin register the class to make it available to the system. ## The Storage Backend Class @@ -12,14 +12,14 @@ The class must live in `Friendica\Addon\youraddonname` namespace, where `youradd There are two different interfaces you need to implement. -### `IWritableStorage` +### `ICanWriteToStorage` -The class must implement `Friendica\Model\Storage\IWritableStorage` interface. All method in the interface must be implemented: +The class must implement `Friendica\Core\Storage\Capability\ICanWriteToStorage` interface. All method in the interface must be implemented: ```php -namespace Friendica\Model\Storage\IWritableStorage; +namespace Friendica\Core\Storage\Capability\ICanWriteToStorage; -interface IWritableStorage +interface ICanWriteToStorage { public function get(string $reference); public function put(string $data, string $reference = ''); @@ -33,17 +33,17 @@ interface IWritableStorage - `put(string $data, string $reference)` saves data in `$data` to position `$reference`, or a new position if `$reference` is empty. - `delete(string $reference)` delete data pointed by `$reference` -### `IStorageConfiguration` +### `ICanConfigureStorage` Each storage backend can have options the admin can set in admin page. -To make the options possible, you need to implement the `Friendica\Model\Storage\IStorageConfiguration` interface. +To make the options possible, you need to implement the `Friendica\Core\Storage\Capability\ICanConfigureStorage` interface. All methods in the interface must be implemented: ```php -namespace Friendica\Model\Storage\IStorageConfiguration; +namespace Friendica\Core\Storage\Capability\ICanConfigureStorage; -interface IStorageConfiguration +interface ICanConfigureStorage { public function getOptions(); public function saveOptions(array $data); @@ -108,7 +108,7 @@ When the plugin is uninstalled, registered backends must be unregistered using `DI::facStorage()->unregister(string $class)`. You have to register a new hook in your addon, listening on `storage_instance(App $a, array $data)`. -In case `$data['name']` is your storage class name, you have to instance a new instance of your `Friendica\Model\Storage\IStorage` class. +In case `$data['name']` is your storage class name, you have to instance a new instance of your `Friendica\Core\Storage\Capability\ICanReadFromStorage` class. Set the instance of your class as `$data['storage']` to pass it back to the backend. This is necessary because it isn't always clear, if you need further construction arguments. @@ -124,7 +124,7 @@ Add a new test class which's naming convention is `StorageClassTest`, which exte Override the two necessary instances: ```php -use Friendica\Model\Storage\IWritableStorage; +use Friendica\Core\Storage\Capability\ICanWriteToStorage; abstract class StorageTest { @@ -132,7 +132,7 @@ abstract class StorageTest abstract protected function getInstance(); // Assertion for the option array you return for your new StorageClass - abstract protected function assertOption(IWritableStorage $storage); + abstract protected function assertOption(ICanWriteToStorage $storage); } ``` @@ -156,16 +156,16 @@ If there's a predecessor to this exception (e.g. you caught an exception and are Example: ```php -use Friendica\Model\Storage\IWritableStorage; +use Friendica\Core\Storage\Capability\ICanWriteToStorage; -class ExampleStorage implements IWritableStorage +class ExampleStorage implements ICanWriteToStorage { public function get(string $reference) : string { try { throw new Exception('a real bad exception'); } catch (Exception $exception) { - throw new \Friendica\Model\Storage\StorageException(sprintf('The Example Storage throws an exception for reference %s', $reference), 500, $exception); + throw new \Friendica\Core\Storage\Exception\StorageException(sprintf('The Example Storage throws an exception for reference %s', $reference), 500, $exception); } } } @@ -186,12 +186,12 @@ The file will be `addon/samplestorage/SampleStorageBackend.php`: assertEquals([ 'filename' => [ diff --git a/doc/Addons.md b/doc/Addons.md index debdc89dd..6b3cd169b 100644 --- a/doc/Addons.md +++ b/doc/Addons.md @@ -544,7 +544,7 @@ Called when a custom storage is used (e.g. webdav_storage) Hook data: - **name** (input): the name of the used storage backend -- **data['storage']** (output): the storage instance to use (**must** implement `\Friendica\Model\Storage\IWritableStorage`) +- **data['storage']** (output): the storage instance to use (**must** implement `\Friendica\Core\Storage\IWritableStorage`) ### storage_config @@ -552,7 +552,7 @@ Called when the admin of the node wants to configure a custom storage (e.g. webd Hook data: - **name** (input): the name of the used storage backend -- **data['storage_config']** (output): the storage configuration instance to use (**must** implement `\Friendica\Model\Storage\IStorageConfiguration`) +- **data['storage_config']** (output): the storage configuration instance to use (**must** implement `\Friendica\Core\Storage\Capability\IConfigureStorage`) ## Complete list of hook callbacks diff --git a/src/Console/Storage.php b/src/Console/Storage.php index 3377f33dd..fbe55f34c 100644 --- a/src/Console/Storage.php +++ b/src/Console/Storage.php @@ -22,9 +22,9 @@ namespace Friendica\Console; use Asika\SimpleConsole\CommandArgsException; -use Friendica\Core\StorageManager; -use Friendica\Model\Storage\ReferenceStorageException; -use Friendica\Model\Storage\StorageException; +use Friendica\Core\Storage\Repository\StorageManager; +use Friendica\Core\Storage\Exception\ReferenceStorageException; +use Friendica\Core\Storage\Exception\StorageException; /** * tool to manage storage backend and stored data from CLI @@ -33,7 +33,7 @@ class Storage extends \Asika\SimpleConsole\Console { protected $helpOptions = ['h', 'help', '?']; - /** @var StorageManager */ + /** @var \Friendica\Core\Storage\Repository\StorageManager */ private $storageManager; /** diff --git a/src/Model/Storage/IStorageConfiguration.php b/src/Core/Storage/Capability/ICanConfigureStorage.php similarity index 97% rename from src/Model/Storage/IStorageConfiguration.php rename to src/Core/Storage/Capability/ICanConfigureStorage.php index c589f5ed5..09d0718cf 100644 --- a/src/Model/Storage/IStorageConfiguration.php +++ b/src/Core/Storage/Capability/ICanConfigureStorage.php @@ -19,12 +19,12 @@ * */ -namespace Friendica\Model\Storage; +namespace Friendica\Core\Storage\Capability; /** * The interface to use for configurable storage backends */ -interface IStorageConfiguration +interface ICanConfigureStorage { /** * Get info about storage options diff --git a/src/Model/Storage/IStorage.php b/src/Core/Storage/Capability/ICanReadFromStorage.php similarity index 84% rename from src/Model/Storage/IStorage.php rename to src/Core/Storage/Capability/ICanReadFromStorage.php index 884148741..b6c63751d 100644 --- a/src/Model/Storage/IStorage.php +++ b/src/Core/Storage/Capability/ICanReadFromStorage.php @@ -19,12 +19,15 @@ * */ -namespace Friendica\Model\Storage; +namespace Friendica\Core\Storage\Capability; + +use Friendica\Core\Storage\Exception\ReferenceStorageException; +use Friendica\Core\Storage\Exception\StorageException; /** * Interface for basic storage backends */ -interface IStorage +interface ICanReadFromStorage { /** * Get data from backend @@ -43,7 +46,7 @@ interface IStorage * * @return string */ - public function __toString(); + public function __toString(): string; /** * The name of the backend diff --git a/src/Model/Storage/IWritableStorage.php b/src/Core/Storage/Capability/ICanWriteToStorage.php similarity index 88% rename from src/Model/Storage/IWritableStorage.php rename to src/Core/Storage/Capability/ICanWriteToStorage.php index 118f4b256..fd9d0e14f 100644 --- a/src/Model/Storage/IWritableStorage.php +++ b/src/Core/Storage/Capability/ICanWriteToStorage.php @@ -19,7 +19,10 @@ * */ -namespace Friendica\Model\Storage; +namespace Friendica\Core\Storage\Capability; + +use Friendica\Core\Storage\Exception\ReferenceStorageException; +use Friendica\Core\Storage\Exception\StorageException; /** * Interface for writable storage backends @@ -27,7 +30,7 @@ namespace Friendica\Model\Storage; * Used for storages with CRUD functionality, mainly used for user data (e.g. photos, attachements). * There's only one active writable storage possible. This type of storage is selectable by the current administrator. */ -interface IWritableStorage extends IStorage +interface ICanWriteToStorage extends ICanReadFromStorage { /** * Put data in backend as $ref. If $ref is not defined a new reference is created. diff --git a/src/Model/Storage/InvalidClassStorageException.php b/src/Core/Storage/Exception/InvalidClassStorageException.php similarity index 95% rename from src/Model/Storage/InvalidClassStorageException.php rename to src/Core/Storage/Exception/InvalidClassStorageException.php index 9c39b3a60..9c1c7bb2b 100644 --- a/src/Model/Storage/InvalidClassStorageException.php +++ b/src/Core/Storage/Exception/InvalidClassStorageException.php @@ -19,7 +19,7 @@ * */ -namespace Friendica\Model\Storage; +namespace Friendica\Core\Storage\Exception; /** * Storage Exception in case of invalid storage class diff --git a/src/Model/Storage/ReferenceStorageException.php b/src/Core/Storage/Exception/ReferenceStorageException.php similarity index 95% rename from src/Model/Storage/ReferenceStorageException.php rename to src/Core/Storage/Exception/ReferenceStorageException.php index fcfd3ab59..7ef596074 100644 --- a/src/Model/Storage/ReferenceStorageException.php +++ b/src/Core/Storage/Exception/ReferenceStorageException.php @@ -19,7 +19,7 @@ * */ -namespace Friendica\Model\Storage; +namespace Friendica\Core\Storage\Exception; /** * Storage Exception in case of invalid references diff --git a/src/Model/Storage/StorageException.php b/src/Core/Storage/Exception/StorageException.php similarity index 95% rename from src/Model/Storage/StorageException.php rename to src/Core/Storage/Exception/StorageException.php index 34a09d57b..7c7b3d12a 100644 --- a/src/Model/Storage/StorageException.php +++ b/src/Core/Storage/Exception/StorageException.php @@ -19,7 +19,7 @@ * */ -namespace Friendica\Model\Storage; +namespace Friendica\Core\Storage\Exception; use Exception; diff --git a/src/Core/StorageManager.php b/src/Core/Storage/Repository/StorageManager.php similarity index 65% rename from src/Core/StorageManager.php rename to src/Core/Storage/Repository/StorageManager.php index e27b59edb..5df6e5ac7 100644 --- a/src/Core/StorageManager.php +++ b/src/Core/Storage/Repository/StorageManager.php @@ -19,12 +19,20 @@ * */ -namespace Friendica\Core; +namespace Friendica\Core\Storage\Repository; use Exception; use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Core\Hook; +use Friendica\Core\L10n; +use Friendica\Core\Storage\Exception\InvalidClassStorageException; +use Friendica\Core\Storage\Exception\ReferenceStorageException; +use Friendica\Core\Storage\Exception\StorageException; +use Friendica\Core\Storage\Capability\ICanReadFromStorage; +use Friendica\Core\Storage\Capability\ICanConfigureStorage; +use Friendica\Core\Storage\Capability\ICanWriteToStorage; use Friendica\Database\Database; -use Friendica\Model\Storage; +use Friendica\Core\Storage\Type; use Friendica\Network\HTTPException\InternalServerErrorException; use Psr\Log\LoggerInterface; @@ -42,15 +50,15 @@ class StorageManager // Default storage backends /** @var string[] */ const DEFAULT_BACKENDS = [ - Storage\Filesystem::NAME, - Storage\Database::NAME, + Type\Filesystem::NAME, + Type\Database::NAME, ]; /** @var string[] List of valid backend classes */ private $validBackends; /** - * @var Storage\IStorage[] A local cache for storage instances + * @var ICanReadFromStorage[] A local cache for storage instances */ private $backendInstances = []; @@ -63,7 +71,7 @@ class StorageManager /** @var L10n */ private $l10n; - /** @var Storage\IWritableStorage */ + /** @var ICanWriteToStorage */ private $currentBackend; /** @@ -72,8 +80,8 @@ class StorageManager * @param LoggerInterface $logger * @param L10n $l10n * - * @throws Storage\InvalidClassStorageException in case the active backend class is invalid - * @throws Storage\StorageException in case of unexpected errors during the active backend class loading + * @throws InvalidClassStorageException in case the active backend class is invalid + * @throws StorageException in case of unexpected errors during the active backend class loading */ public function __construct(Database $dba, IManageConfigValues $config, LoggerInterface $logger, L10n $l10n) { @@ -92,9 +100,9 @@ class StorageManager /** * Return current storage backend class * - * @return Storage\IWritableStorage + * @return ICanWriteToStorage */ - public function getBackend() + public function getBackend(): ICanWriteToStorage { return $this->currentBackend; } @@ -104,16 +112,16 @@ class StorageManager * * @param string $name Backend name * - * @return Storage\IWritableStorage + * @return ICanWriteToStorage * - * @throws Storage\InvalidClassStorageException in case there's no backend class for the name - * @throws Storage\StorageException in case of an unexpected failure during the hook call + * @throws InvalidClassStorageException in case there's no backend class for the name + * @throws StorageException in case of an unexpected failure during the hook call */ - public function getWritableStorageByName(string $name): Storage\IWritableStorage + public function getWritableStorageByName(string $name): ICanWriteToStorage { $storage = $this->getByName($name, $this->validBackends); - if (!$storage instanceof Storage\IWritableStorage) { - throw new Storage\InvalidClassStorageException(sprintf('Backend %s is not writable', $name)); + if (!$storage instanceof ICanWriteToStorage) { + throw new InvalidClassStorageException(sprintf('Backend %s is not writable', $name)); } return $storage; @@ -124,19 +132,19 @@ class StorageManager * * @param string $name Backend name * - * @return Storage\IStorageConfiguration|false + * @return ICanConfigureStorage|false * - * @throws Storage\InvalidClassStorageException in case there's no backend class for the name - * @throws Storage\StorageException in case of an unexpected failure during the hook call + * @throws InvalidClassStorageException in case there's no backend class for the name + * @throws StorageException in case of an unexpected failure during the hook call */ public function getConfigurationByName(string $name) { switch ($name) { // Try the filesystem backend - case Storage\Filesystem::getName(): - return new Storage\FilesystemConfig($this->config, $this->l10n); + case Type\Filesystem::getName(): + return new Type\FilesystemConfig($this->config, $this->l10n); // try the database backend - case Storage\Database::getName(): + case Type\Database::getName(): return false; default: $data = [ @@ -145,13 +153,13 @@ class StorageManager ]; try { Hook::callAll('storage_config', $data); - if (!($data['storage_config'] ?? null) instanceof Storage\IStorageConfiguration) { - throw new Storage\InvalidClassStorageException(sprintf('Configuration for backend %s was not found', $name)); + if (!($data['storage_config'] ?? null) instanceof ICanConfigureStorage) { + throw new InvalidClassStorageException(sprintf('Configuration for backend %s was not found', $name)); } return $data['storage_config']; } catch (InternalServerErrorException $exception) { - throw new Storage\StorageException(sprintf('Failed calling hook::storage_config for backend %s', $name), $exception); + throw new StorageException(sprintf('Failed calling hook::storage_config for backend %s', $name), $exception); } } } @@ -162,36 +170,36 @@ class StorageManager * @param string $name Backend name * @param string[]|null $validBackends possible, manual override of the valid backends * - * @return Storage\IStorage + * @return ICanReadFromStorage * - * @throws Storage\InvalidClassStorageException in case there's no backend class for the name - * @throws Storage\StorageException in case of an unexpected failure during the hook call + * @throws InvalidClassStorageException in case there's no backend class for the name + * @throws StorageException in case of an unexpected failure during the hook call */ - public function getByName(string $name, array $validBackends = null): Storage\IStorage + public function getByName(string $name, array $validBackends = null): ICanReadFromStorage { // If there's no cached instance create a new instance if (!isset($this->backendInstances[$name])) { // If the current name isn't a valid backend (or the SystemResource instance) create it if (!$this->isValidBackend($name, $validBackends)) { - throw new Storage\InvalidClassStorageException(sprintf('Backend %s is not valid', $name)); + throw new InvalidClassStorageException(sprintf('Backend %s is not valid', $name)); } switch ($name) { // Try the filesystem backend - case Storage\Filesystem::getName(): - $storageConfig = new Storage\FilesystemConfig($this->config, $this->l10n); - $this->backendInstances[$name] = new Storage\Filesystem($storageConfig->getStoragePath()); + case Type\Filesystem::getName(): + $storageConfig = new Type\FilesystemConfig($this->config, $this->l10n); + $this->backendInstances[$name] = new Type\Filesystem($storageConfig->getStoragePath()); break; // try the database backend - case Storage\Database::getName(): - $this->backendInstances[$name] = new Storage\Database($this->dba); + case Type\Database::getName(): + $this->backendInstances[$name] = new Type\Database($this->dba); break; // at least, try if there's an addon for the backend - case Storage\SystemResource::getName(): - $this->backendInstances[$name] = new Storage\SystemResource(); + case \Friendica\Core\Storage\Type\SystemResource::getName(): + $this->backendInstances[$name] = new \Friendica\Core\Storage\Type\SystemResource(); break; - case Storage\ExternalResource::getName(): - $this->backendInstances[$name] = new Storage\ExternalResource(); + case \Friendica\Core\Storage\Type\ExternalResource::getName(): + $this->backendInstances[$name] = new \Friendica\Core\Storage\Type\ExternalResource(); break; default: $data = [ @@ -200,13 +208,13 @@ class StorageManager ]; try { Hook::callAll('storage_instance', $data); - if (!($data['storage'] ?? null) instanceof Storage\IStorage) { - throw new Storage\InvalidClassStorageException(sprintf('Backend %s was not found', $name)); + if (!($data['storage'] ?? null) instanceof ICanReadFromStorage) { + throw new InvalidClassStorageException(sprintf('Backend %s was not found', $name)); } $this->backendInstances[$data['name'] ?? $name] = $data['storage']; } catch (InternalServerErrorException $exception) { - throw new Storage\StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception); + throw new StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception); } break; } @@ -227,8 +235,8 @@ class StorageManager { $validBackends = $validBackends ?? array_merge($this->validBackends, [ - Storage\SystemResource::getName(), - Storage\ExternalResource::getName(), + Type\SystemResource::getName(), + Type\ExternalResource::getName(), ]); return in_array($name, $validBackends); } @@ -236,11 +244,11 @@ class StorageManager /** * Set current storage backend class * - * @param Storage\IWritableStorage $storage The storage class + * @param ICanWriteToStorage $storage The storage class * * @return boolean True, if the set was successful */ - public function setBackend(Storage\IWritableStorage $storage): bool + public function setBackend(ICanWriteToStorage $storage): bool { if ($this->config->set('storage', 'name', $storage::getName())) { $this->currentBackend = $storage; @@ -271,9 +279,8 @@ class StorageManager */ public function register(string $class): bool { - if (is_subclass_of($class, Storage\IStorage::class)) { - /** @var Storage\IStorage $class */ - + if (is_subclass_of($class, ICanReadFromStorage::class)) { + /** @var ICanReadFromStorage $class */ if ($this->isValidBackend($class::getName(), $this->validBackends)) { return true; } @@ -299,15 +306,14 @@ class StorageManager * * @return boolean True, if unregistering was successful * - * @throws Storage\StorageException + * @throws StorageException */ public function unregister(string $class): bool { - if (is_subclass_of($class, Storage\IStorage::class)) { - /** @var Storage\IStorage $class */ - + if (is_subclass_of($class, ICanReadFromStorage::class)) { + /** @var ICanReadFromStorage $class */ if ($this->currentBackend::getName() == $class::getName()) { - throw new Storage\StorageException(sprintf('Cannot unregister %s, because it\'s currently active.', $class::getName())); + throw new StorageException(sprintf('Cannot unregister %s, because it\'s currently active.', $class::getName())); } $key = array_search($class::getName(), $this->validBackends); @@ -336,18 +342,18 @@ class StorageManager * Copy existing data to destination storage and delete from source. * This method cannot move to legacy in-table `data` field. * - * @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 + * @param ICanWriteToStorage $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 StorageException * @throws Exception */ - public function move(Storage\IWritableStorage $destination, array $tables = self::TABLES, int $limit = 5000): int + public function move(ICanWriteToStorage $destination, array $tables = self::TABLES, int $limit = 5000): int { if (!$this->isValidBackend($destination, $this->validBackends)) { - throw new Storage\StorageException(sprintf("Can't move to storage backend '%s'", $destination::getName())); + throw new StorageException(sprintf("Can't move to storage backend '%s'", $destination::getName())); } $moved = 0; @@ -369,10 +375,10 @@ class StorageManager $source = $this->getWritableStorageByName($resource['backend-class'] ?? ''); $this->logger->info('Get data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]); $data = $source->get($sourceRef); - } catch (Storage\InvalidClassStorageException $exception) { + } catch (InvalidClassStorageException $exception) { $this->logger->info('Get data from DB resource field.', ['oldReference' => $sourceRef]); $data = $resource['data']; - } catch (Storage\ReferenceStorageException $exception) { + } catch (ReferenceStorageException $exception) { $this->logger->info('Invalid source reference.', ['oldBackend' => $source, 'oldReference' => $sourceRef]); continue; } @@ -385,7 +391,7 @@ class StorageManager $this->logger->info('update row'); if ($this->dba->update($table, ['backend-class' => $destination::getName(), 'backend-ref' => $destinationRef, 'data' => ''], ['id' => $id])) { if (!empty($source)) { - $this->logger->info('Delete data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]); + $this->logger->info('Deleted data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]); $source->delete($sourceRef); } $moved++; diff --git a/src/Model/Storage/Database.php b/src/Core/Storage/Type/Database.php similarity index 92% rename from src/Model/Storage/Database.php rename to src/Core/Storage/Type/Database.php index 7b90d8789..a22f8ae45 100644 --- a/src/Model/Storage/Database.php +++ b/src/Core/Storage/Type/Database.php @@ -19,9 +19,12 @@ * */ -namespace Friendica\Model\Storage; +namespace Friendica\Core\Storage\Type; use Exception; +use Friendica\Core\Storage\Exception\ReferenceStorageException; +use Friendica\Core\Storage\Exception\StorageException; +use Friendica\Core\Storage\Capability\ICanWriteToStorage; use Friendica\Database\Database as DBA; /** @@ -29,7 +32,7 @@ use Friendica\Database\Database as DBA; * * This class manage data stored in database table. */ -class Database implements IWritableStorage +class Database implements ICanWriteToStorage { const NAME = 'Database'; @@ -121,7 +124,7 @@ class Database implements IWritableStorage return self::NAME; } - public function __toString() + public function __toString(): string { return self::getName(); } diff --git a/src/Model/Storage/ExternalResource.php b/src/Core/Storage/Type/ExternalResource.php similarity index 89% rename from src/Model/Storage/ExternalResource.php rename to src/Core/Storage/Type/ExternalResource.php index 413050c30..33e65921b 100644 --- a/src/Model/Storage/ExternalResource.php +++ b/src/Core/Storage/Type/ExternalResource.php @@ -19,9 +19,11 @@ * */ -namespace Friendica\Model\Storage; +namespace Friendica\Core\Storage\Type; use Exception; +use Friendica\Core\Storage\Exception\ReferenceStorageException; +use Friendica\Core\Storage\Capability\ICanReadFromStorage; use Friendica\Util\HTTPSignature; /** @@ -30,7 +32,7 @@ use Friendica\Util\HTTPSignature; * This class is used to load external resources, like images. * Is not intended to be selectable by admins as default storage class. */ -class ExternalResource implements IStorage +class ExternalResource implements ICanReadFromStorage { const NAME = 'ExternalResource'; @@ -64,7 +66,7 @@ class ExternalResource implements IStorage /** * @inheritDoc */ - public function __toString() + public function __toString(): string { return self::NAME; } diff --git a/src/Model/Storage/Filesystem.php b/src/Core/Storage/Type/Filesystem.php similarity index 94% rename from src/Model/Storage/Filesystem.php rename to src/Core/Storage/Type/Filesystem.php index 8b5078f54..21a531946 100644 --- a/src/Model/Storage/Filesystem.php +++ b/src/Core/Storage/Type/Filesystem.php @@ -19,9 +19,12 @@ * */ -namespace Friendica\Model\Storage; +namespace Friendica\Core\Storage\Type; use Exception; +use Friendica\Core\Storage\Exception\ReferenceStorageException; +use Friendica\Core\Storage\Exception\StorageException; +use Friendica\Core\Storage\Capability\ICanWriteToStorage; use Friendica\Util\Strings; /** @@ -34,7 +37,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 IWritableStorage +class Filesystem implements ICanWriteToStorage { const NAME = 'Filesystem'; @@ -175,7 +178,7 @@ class Filesystem implements IWritableStorage return self::NAME; } - public function __toString() + public function __toString(): string { return self::getName(); } diff --git a/src/Model/Storage/FilesystemConfig.php b/src/Core/Storage/Type/FilesystemConfig.php similarity index 93% rename from src/Model/Storage/FilesystemConfig.php rename to src/Core/Storage/Type/FilesystemConfig.php index 1aa1c3817..b30b2af08 100644 --- a/src/Model/Storage/FilesystemConfig.php +++ b/src/Core/Storage/Type/FilesystemConfig.php @@ -19,15 +19,16 @@ * */ -namespace Friendica\Model\Storage; +namespace Friendica\Core\Storage\Type; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\L10n; +use Friendica\Core\Storage\Capability\ICanConfigureStorage; /** * Filesystem based storage backend configuration */ -class FilesystemConfig implements IStorageConfiguration +class FilesystemConfig implements ICanConfigureStorage { // Default base folder const DEFAULT_BASE_FOLDER = 'storage'; diff --git a/src/Model/Storage/SystemResource.php b/src/Core/Storage/Type/SystemResource.php similarity index 86% rename from src/Model/Storage/SystemResource.php rename to src/Core/Storage/Type/SystemResource.php index 39bc0a024..7b73b7c2e 100644 --- a/src/Model/Storage/SystemResource.php +++ b/src/Core/Storage/Type/SystemResource.php @@ -19,7 +19,11 @@ * */ -namespace Friendica\Model\Storage; +namespace Friendica\Core\Storage\Type; + +use Friendica\Core\Storage\Exception\ReferenceStorageException; +use Friendica\Core\Storage\Exception\StorageException; +use Friendica\Core\Storage\Capability\ICanReadFromStorage; /** * System resource storage class @@ -27,7 +31,7 @@ namespace Friendica\Model\Storage; * This class is used to load system resources, like images. * Is not intended to be selectable by admins as default storage class. */ -class SystemResource implements IStorage +class SystemResource implements ICanReadFromStorage { const NAME = 'SystemResource'; @@ -58,7 +62,7 @@ class SystemResource implements IStorage /** * @inheritDoc */ - public function __toString() + public function __toString(): string { return self::NAME; } diff --git a/src/DI.php b/src/DI.php index d45801a9f..5ba7e88db 100644 --- a/src/DI.php +++ b/src/DI.php @@ -211,11 +211,11 @@ abstract class DI } /** - * @return Core\StorageManager + * @return \Friendica\Core\Storage\Repository\StorageManager */ public static function storageManager() { - return self::$dice->create(Core\StorageManager::class); + return self::$dice->create(Core\Storage\Repository\StorageManager::class); } // @@ -395,11 +395,11 @@ abstract class DI } /** - * @return Model\Storage\IWritableStorage + * @return Core\Storage\Capability\ICanWriteToStorage */ public static function storage() { - return self::$dice->create(Model\Storage\IWritableStorage::class); + return self::$dice->create(Core\Storage\Capability\ICanWriteToStorage::class); } /** diff --git a/src/Model/Attach.php b/src/Model/Attach.php index e11fd01bc..dcc790676 100644 --- a/src/Model/Attach.php +++ b/src/Model/Attach.php @@ -25,8 +25,8 @@ use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Database\DBStructure; use Friendica\DI; -use Friendica\Model\Storage\InvalidClassStorageException; -use Friendica\Model\Storage\ReferenceStorageException; +use Friendica\Core\Storage\Exception\InvalidClassStorageException; +use Friendica\Core\Storage\Exception\ReferenceStorageException; use Friendica\Object\Image; use Friendica\Util\DateTimeFormat; use Friendica\Util\Mimetype; diff --git a/src/Model/Photo.php b/src/Model/Photo.php index ebd278753..13beb05b3 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -27,11 +27,11 @@ use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Database\DBStructure; use Friendica\DI; -use Friendica\Model\Storage\ExternalResource; -use Friendica\Model\Storage\InvalidClassStorageException; -use Friendica\Model\Storage\ReferenceStorageException; -use Friendica\Model\Storage\StorageException; -use Friendica\Model\Storage\SystemResource; +use Friendica\Core\Storage\Type\ExternalResource; +use Friendica\Core\Storage\Exception\InvalidClassStorageException; +use Friendica\Core\Storage\Exception\ReferenceStorageException; +use Friendica\Core\Storage\Exception\StorageException; +use Friendica\Core\Storage\Type\SystemResource; use Friendica\Object\Image; use Friendica\Util\DateTimeFormat; use Friendica\Util\Images; diff --git a/src/Module/Admin/Storage.php b/src/Module/Admin/Storage.php index 6b22d905c..8eb706fa0 100644 --- a/src/Module/Admin/Storage.php +++ b/src/Module/Admin/Storage.php @@ -23,9 +23,9 @@ namespace Friendica\Module\Admin; use Friendica\Core\Renderer; use Friendica\DI; -use Friendica\Model\Storage\InvalidClassStorageException; -use Friendica\Model\Storage\IStorageConfiguration; -use Friendica\Model\Storage\IWritableStorage; +use Friendica\Core\Storage\Exception\InvalidClassStorageException; +use Friendica\Core\Storage\Capability\ICanConfigureStorage; +use Friendica\Core\Storage\Capability\ICanWriteToStorage; use Friendica\Module\BaseAdmin; use Friendica\Util\Strings; @@ -40,7 +40,7 @@ class Storage extends BaseAdmin $storagebackend = Strings::escapeTags(trim($parameters['name'] ?? '')); try { - /** @var IStorageConfiguration|false $newStorageConfig */ + /** @var \Friendica\Core\Storage\Capability\ICanConfigureStorage|false $newStorageConfig */ $newStorageConfig = DI::storageManager()->getConfigurationByName($storagebackend); } catch (InvalidClassStorageException $storageException) { notice(DI::l10n()->t('Storage backend, %s is invalid.', $storagebackend)); @@ -78,7 +78,7 @@ class Storage extends BaseAdmin if (!empty($_POST['submit_save_set'])) { try { - /** @var IWritableStorage $newstorage */ + /** @var \Friendica\Core\Storage\Capability\ICanWriteToStorage $newstorage */ $newstorage = DI::storageManager()->getWritableStorageByName($storagebackend); if (!DI::storageManager()->setBackend($newstorage)) { @@ -129,7 +129,7 @@ class Storage extends BaseAdmin 'name' => $name, 'prefix' => $storage_form_prefix, 'form' => $storage_form, - 'active' => $current_storage_backend instanceof IWritableStorage && $name === $current_storage_backend::getName(), + 'active' => $current_storage_backend instanceof ICanWriteToStorage && $name === $current_storage_backend::getName(), ]; } @@ -147,7 +147,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 IWritableStorage ? $current_storage_backend::getName() : DI::l10n()->t('Database (legacy)'), + '$storagebackend' => $current_storage_backend instanceof ICanWriteToStorage ? $current_storage_backend::getName() : DI::l10n()->t('Database (legacy)'), '$availablestorageforms' => $available_storage_forms, ]); } diff --git a/src/Module/Photo.php b/src/Module/Photo.php index 0b03f4a79..db21803e6 100644 --- a/src/Module/Photo.php +++ b/src/Module/Photo.php @@ -29,8 +29,8 @@ use Friendica\Model\Contact; use Friendica\Model\Photo as MPhoto; use Friendica\Model\Post; use Friendica\Model\Profile; -use Friendica\Model\Storage\ExternalResource; -use Friendica\Model\Storage\SystemResource; +use Friendica\Core\Storage\Type\ExternalResource; +use Friendica\Core\Storage\Type\SystemResource; use Friendica\Model\User; use Friendica\Network\HTTPException; use Friendica\Object\Image; diff --git a/static/dependencies.config.php b/static/dependencies.config.php index f4aacb340..bbf8c5599 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -42,10 +42,10 @@ use Friendica\Core\L10n; use Friendica\Core\Lock; use Friendica\Core\Process; use Friendica\Core\Session\Capability\IHandleSessions; -use Friendica\Core\StorageManager; +use Friendica\Core\Storage\Repository\StorageManager; use Friendica\Database\Database; use Friendica\Factory; -use Friendica\Model\Storage\IWritableStorage; +use Friendica\Core\Storage\Capability\ICanWriteToStorage; use Friendica\Model\User\Cookie; use Friendica\Model\Log\ParsedLogIterator; use Friendica\Network; @@ -218,7 +218,7 @@ return [ $_SERVER, $_COOKIE ], ], - IWritableStorage::class => [ + ICanWriteToStorage::class => [ 'instanceOf' => StorageManager::class, 'call' => [ ['getBackend', [], Dice::CHAIN_CALL], diff --git a/tests/Util/SampleStorageBackend.php b/tests/Util/SampleStorageBackend.php index 1185a2564..03fca0171 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\IWritableStorage; +use Friendica\Core\Storage\Capability\ICanWriteToStorage; use Friendica\Core\L10n; /** * A backend storage example class */ -class SampleStorageBackend implements IWritableStorage +class SampleStorageBackend implements ICanWriteToStorage { const NAME = 'Sample Storage'; @@ -102,7 +102,7 @@ class SampleStorageBackend implements IWritableStorage return $this->options; } - public function __toString() + public function __toString(): string { return self::NAME; } diff --git a/tests/src/Model/Storage/DatabaseStorageTest.php b/tests/src/Core/Storage/DatabaseStorageTest.php similarity index 95% rename from tests/src/Model/Storage/DatabaseStorageTest.php rename to tests/src/Core/Storage/DatabaseStorageTest.php index 796b8937c..b51f9fc2b 100644 --- a/tests/src/Model/Storage/DatabaseStorageTest.php +++ b/tests/src/Core/Storage/DatabaseStorageTest.php @@ -19,10 +19,10 @@ * */ -namespace Friendica\Test\src\Model\Storage; +namespace Friendica\Test\src\Core\Storage; use Friendica\Core\Config\Factory\Config; -use Friendica\Model\Storage\Database; +use Friendica\Core\Storage\Type\Database; use Friendica\Test\DatabaseTestTrait; use Friendica\Test\Util\Database\StaticDatabase; use Friendica\Test\Util\VFSTrait; diff --git a/tests/src/Model/Storage/FilesystemStorageConfigTest.php b/tests/src/Core/Storage/FilesystemStorageConfigTest.php similarity index 89% rename from tests/src/Model/Storage/FilesystemStorageConfigTest.php rename to tests/src/Core/Storage/FilesystemStorageConfigTest.php index a5989b1cd..a48faed76 100644 --- a/tests/src/Model/Storage/FilesystemStorageConfigTest.php +++ b/tests/src/Core/Storage/FilesystemStorageConfigTest.php @@ -19,12 +19,12 @@ * */ -namespace Friendica\Test\src\Model\Storage; +namespace Friendica\Test\src\Core\Storage; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\L10n; -use Friendica\Model\Storage\FilesystemConfig; -use Friendica\Model\Storage\IStorageConfiguration; +use Friendica\Core\Storage\Capability\ICanConfigureStorage; +use Friendica\Core\Storage\Type\FilesystemConfig; use Friendica\Test\Util\VFSTrait; use Mockery\MockInterface; use org\bovigo\vfs\vfsStream; @@ -54,7 +54,7 @@ class FilesystemStorageConfigTest extends StorageConfigTest return new FilesystemConfig($config, $l10n); } - protected function assertOption(IStorageConfiguration $storage) + protected function assertOption(ICanConfigureStorage $storage) { self::assertEquals([ 'storagepath' => [ diff --git a/tests/src/Model/Storage/FilesystemStorageTest.php b/tests/src/Core/Storage/FilesystemStorageTest.php similarity index 94% rename from tests/src/Model/Storage/FilesystemStorageTest.php rename to tests/src/Core/Storage/FilesystemStorageTest.php index 837c16681..c97b93755 100644 --- a/tests/src/Model/Storage/FilesystemStorageTest.php +++ b/tests/src/Core/Storage/FilesystemStorageTest.php @@ -19,11 +19,11 @@ * */ -namespace Friendica\Test\src\Model\Storage; +namespace Friendica\Test\src\Core\Storage; -use Friendica\Model\Storage\Filesystem; -use Friendica\Model\Storage\FilesystemConfig; -use Friendica\Model\Storage\StorageException; +use Friendica\Core\Storage\Exception\StorageException; +use Friendica\Core\Storage\Type\Filesystem; +use Friendica\Core\Storage\Type\FilesystemConfig; use Friendica\Test\Util\VFSTrait; use org\bovigo\vfs\vfsStream; diff --git a/tests/src/Core/StorageManagerTest.php b/tests/src/Core/Storage/Repository/StorageManagerTest.php similarity index 80% rename from tests/src/Core/StorageManagerTest.php rename to tests/src/Core/Storage/Repository/StorageManagerTest.php index f9d05e637..c7b656943 100644 --- a/tests/src/Core/StorageManagerTest.php +++ b/tests/src/Core/Storage/Repository/StorageManagerTest.php @@ -19,7 +19,7 @@ * */ -namespace Friendica\Test\src\Core; +namespace Friendica\Test\src\Core\Storage\Repository; use Dice\Dice; use Friendica\Core\Config\Capability\IManageConfigValues; @@ -28,12 +28,17 @@ use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Session\Capability\IHandleSessions; use Friendica\Core\Session\Type\Memory; -use Friendica\Core\StorageManager; +use Friendica\Core\Storage\Exception\InvalidClassStorageException; +use Friendica\Core\Storage\Capability\ICanReadFromStorage; +use Friendica\Core\Storage\Capability\ICanWriteToStorage; +use Friendica\Core\Storage\Repository\StorageManager; +use Friendica\Core\Storage\Type\Filesystem; +use Friendica\Core\Storage\Type\SystemResource; use Friendica\Database\Database; use Friendica\DI; use Friendica\Core\Config\Factory\Config; use Friendica\Core\Config\Repository; -use Friendica\Model\Storage; +use Friendica\Core\Storage\Type; use Friendica\Network\HTTPClient; use Friendica\Test\DatabaseTest; use Friendica\Test\Util\Database\StaticDatabase; @@ -64,7 +69,7 @@ class StorageManagerTest extends DatabaseTest $this->setUpVfsDir(); - vfsStream::newDirectory(Storage\FilesystemConfig::DEFAULT_BASE_FOLDER, 0777)->at($this->root); + vfsStream::newDirectory(Type\FilesystemConfig::DEFAULT_BASE_FOLDER, 0777)->at($this->root); $this->logger = new NullLogger(); @@ -83,7 +88,7 @@ class StorageManagerTest extends DatabaseTest $configModel = new Repository\Config($this->dba); $this->config = new PreloadConfig($configCache, $configModel); $this->config->set('storage', 'name', 'Database'); - $this->config->set('storage', 'filesystem_path', $this->root->getChild(Storage\FilesystemConfig::DEFAULT_BASE_FOLDER)->url()); + $this->config->set('storage', 'filesystem_path', $this->root->getChild(Type\FilesystemConfig::DEFAULT_BASE_FOLDER)->url()); $this->l10n = \Mockery::mock(L10n::class); @@ -92,7 +97,7 @@ class StorageManagerTest extends DatabaseTest protected function tearDown(): void { - $this->root->removeChild(Storage\FilesystemConfig::DEFAULT_BASE_FOLDER); + $this->root->removeChild(Type\FilesystemConfig::DEFAULT_BASE_FOLDER); parent::tearDown(); } @@ -113,30 +118,30 @@ class StorageManagerTest extends DatabaseTest 'empty' => [ 'name' => '', 'valid' => false, - 'interface' => Storage\IStorage::class, + 'interface' => ICanReadFromStorage::class, 'assert' => null, 'assertName' => '', ], 'database' => [ - 'name' => Storage\Database::NAME, + 'name' => Type\Database::NAME, 'valid' => true, - 'interface' => Storage\IWritableStorage::class, - 'assert' => Storage\Database::class, - 'assertName' => Storage\Database::NAME, + 'interface' => ICanWriteToStorage::class, + 'assert' => Type\Database::class, + 'assertName' => Type\Database::NAME, ], 'filesystem' => [ - 'name' => Storage\Filesystem::NAME, + 'name' => Filesystem::NAME, 'valid' => true, - 'interface' => Storage\IWritableStorage::class, - 'assert' => Storage\Filesystem::class, - 'assertName' => Storage\Filesystem::NAME, + 'interface' => ICanWriteToStorage::class, + 'assert' => Filesystem::class, + 'assertName' => Filesystem::NAME, ], 'systemresource' => [ - 'name' => Storage\SystemResource::NAME, + 'name' => SystemResource::NAME, 'valid' => true, - 'interface' => Storage\IStorage::class, - 'assert' => Storage\SystemResource::class, - 'assertName' => Storage\SystemResource::NAME, + 'interface' => ICanReadFromStorage::class, + 'assert' => SystemResource::class, + 'assertName' => SystemResource::NAME, ], 'invalid' => [ 'name' => 'invalid', @@ -157,16 +162,16 @@ class StorageManagerTest extends DatabaseTest public function testGetByName($name, $valid, $interface, $assert, $assertName) { if (!$valid) { - $this->expectException(Storage\InvalidClassStorageException::class); + $this->expectException(InvalidClassStorageException::class); } - if ($interface === Storage\IWritableStorage::class) { + if ($interface === ICanWriteToStorage::class) { $this->config->set('storage', 'name', $name); } $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n); - if ($interface === Storage\IWritableStorage::class) { + if ($interface === ICanWriteToStorage::class) { $storage = $storageManager->getWritableStorageByName($name); } else { $storage = $storageManager->getByName($name); @@ -189,8 +194,8 @@ class StorageManagerTest extends DatabaseTest // true in every of the backends self::assertEquals(!empty($assertName), $storageManager->isValidBackend($name)); - // if it's a IWritableStorage, the valid backend should return true, otherwise false - self::assertEquals($interface === Storage\IWritableStorage::class, $storageManager->isValidBackend($name, StorageManager::DEFAULT_BACKENDS)); + // if it's a ICanWriteToStorage, the valid backend should return true, otherwise false + self::assertEquals($interface === ICanWriteToStorage::class, $storageManager->isValidBackend($name, StorageManager::DEFAULT_BACKENDS)); } /** @@ -210,8 +215,8 @@ class StorageManagerTest extends DatabaseTest */ public function testGetBackend($name, $valid, $interface, $assert, $assertName) { - if ($interface !== Storage\IWritableStorage::class) { - static::markTestSkipped('only works for IWritableStorage'); + if ($interface !== ICanWriteToStorage::class) { + static::markTestSkipped('only works for ICanWriteToStorage'); } $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n); @@ -230,8 +235,8 @@ class StorageManagerTest extends DatabaseTest public function testPresetBackend($name, $valid, $interface, $assert, $assertName) { $this->config->set('storage', 'name', $name); - if ($interface !== Storage\IWritableStorage::class) { - $this->expectException(Storage\InvalidClassStorageException::class); + if ($interface !== ICanWriteToStorage::class) { + $this->expectException(InvalidClassStorageException::class); } $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n); @@ -250,7 +255,7 @@ class StorageManagerTest extends DatabaseTest { /// @todo Remove dice once "Hook" is dynamic and mockable $dice = (new Dice()) - ->addRules(include __DIR__ . '/../../../static/dependencies.config.php') + ->addRules(include __DIR__ . '/../../../../../static/dependencies.config.php') ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]) ->addRule(IHandleSessions::class, ['instanceOf' => Session\Type\Memory::class, 'shared' => true, 'call' => null]); DI::init($dice); @@ -278,7 +283,7 @@ class StorageManagerTest extends DatabaseTest { /// @todo Remove dice once "Hook" is dynamic and mockable $dice = (new Dice()) - ->addRules(include __DIR__ . '/../../../static/dependencies.config.php') + ->addRules(include __DIR__ . '/../../../../../static/dependencies.config.php') ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]) ->addRule(IHandleSessions::class, ['instanceOf' => Memory::class, 'shared' => true, 'call' => null]); DI::init($dice); @@ -303,7 +308,7 @@ class StorageManagerTest extends DatabaseTest self::assertInstanceOf(SampleStorageBackend::class, $storageManager->getBackend()); - self::expectException(Storage\StorageException::class); + self::expectException(\Friendica\Core\Storage\Exception\StorageException::class); self::expectExceptionMessage('Cannot unregister Sample Storage, because it\'s currently active.'); $storageManager->unregister(SampleStorageBackend::class); @@ -316,11 +321,11 @@ class StorageManagerTest extends DatabaseTest */ public function testMoveStorage($name, $valid, $interface, $assert, $assertName) { - if ($interface !== Storage\IWritableStorage::class) { + if ($interface !== ICanWriteToStorage::class) { self::markTestSkipped("No user backend"); } - $this->loadFixture(__DIR__ . '/../../datasets/storage/database.fixture.php', $this->dba); + $this->loadFixture(__DIR__ . '/../../../../datasets/storage/database.fixture.php', $this->dba); $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n); $storage = $storageManager->getWritableStorageByName($name); @@ -343,11 +348,11 @@ class StorageManagerTest extends DatabaseTest */ public function testWrongWritableStorage() { - $this->expectException(Storage\InvalidClassStorageException::class); + $this->expectException(InvalidClassStorageException::class); $this->expectExceptionMessage('Backend SystemResource is not valid'); $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n); - $storage = $storageManager->getWritableStorageByName(Storage\SystemResource::getName()); + $storage = $storageManager->getWritableStorageByName(SystemResource::getName()); $storageManager->move($storage); } } diff --git a/tests/src/Model/Storage/StorageConfigTest.php b/tests/src/Core/Storage/StorageConfigTest.php similarity index 79% rename from tests/src/Model/Storage/StorageConfigTest.php rename to tests/src/Core/Storage/StorageConfigTest.php index 80cbbaeb0..a01be343d 100644 --- a/tests/src/Model/Storage/StorageConfigTest.php +++ b/tests/src/Core/Storage/StorageConfigTest.php @@ -19,17 +19,17 @@ * */ -namespace Friendica\Test\src\Model\Storage; +namespace Friendica\Test\src\Core\Storage; -use Friendica\Model\Storage\IStorageConfiguration; +use Friendica\Core\Storage\Capability\ICanConfigureStorage; use Friendica\Test\MockedTest; abstract class StorageConfigTest extends MockedTest { - /** @return IStorageConfiguration */ + /** @return \Friendica\Core\Storage\Capability\ICanConfigureStorage */ abstract protected function getInstance(); - abstract protected function assertOption(IStorageConfiguration $storage); + abstract protected function assertOption(\Friendica\Core\Storage\Capability\ICanConfigureStorage $storage); /** * Test if the "getOption" is asserted diff --git a/tests/src/Model/Storage/StorageTest.php b/tests/src/Core/Storage/StorageTest.php similarity index 88% rename from tests/src/Model/Storage/StorageTest.php rename to tests/src/Core/Storage/StorageTest.php index 743374757..71377a955 100644 --- a/tests/src/Model/Storage/StorageTest.php +++ b/tests/src/Core/Storage/StorageTest.php @@ -19,16 +19,16 @@ * */ -namespace Friendica\Test\src\Model\Storage; +namespace Friendica\Test\src\Core\Storage; -use Friendica\Model\Storage\IWritableStorage; -use Friendica\Model\Storage\IStorage; -use Friendica\Model\Storage\ReferenceStorageException; +use Friendica\Core\Storage\Capability\ICanReadFromStorage; +use Friendica\Core\Storage\Capability\ICanWriteToStorage; +use Friendica\Core\Storage\Exception\ReferenceStorageException; use Friendica\Test\MockedTest; abstract class StorageTest extends MockedTest { - /** @return IWritableStorage */ + /** @return ICanWriteToStorage */ abstract protected function getInstance(); /** @@ -37,7 +37,7 @@ abstract class StorageTest extends MockedTest public function testInstance() { $instance = $this->getInstance(); - self::assertInstanceOf(IStorage::class, $instance); + self::assertInstanceOf(ICanReadFromStorage::class, $instance); } /** diff --git a/update.php b/update.php index a2999ed5a..368b70b22 100644 --- a/update.php +++ b/update.php @@ -41,6 +41,7 @@ */ use Friendica\Core\Logger; +use Friendica\Core\Storage\Capability\ICanReadFromStorage; use Friendica\Core\Update; use Friendica\Core\Worker; use Friendica\Database\Database; @@ -54,7 +55,6 @@ use Friendica\Model\Notification; use Friendica\Model\Photo; use Friendica\Model\Post; use Friendica\Model\Profile; -use Friendica\Model\Storage; use Friendica\Security\PermissionSet\Repository\PermissionSet; use Friendica\Worker\Delivery; @@ -183,7 +183,7 @@ function update_1330() // set the name of the storage instead of the classpath as config if (!empty($currStorage)) { - /** @var Storage\IStorage $currStorage */ + /** @var ICanReadFromStorage $currStorage */ if (!DI::config()->set('storage', 'name', $currStorage::getName())) { return Update::FAILED; } @@ -989,7 +989,7 @@ function update_1434() // in case of an empty config, set "Database" as default storage backend if (empty($name)) { - DI::config()->set('storage', 'name', Storage\Database::getName()); + DI::config()->set('storage', 'name', \Friendica\Core\Storage\Type\Database::getName()); } // In case of a Using deprecated storage class value, set the right name for it From d34497e69b79b9f157061ccef046a0403d65065b Mon Sep 17 00:00:00 2001 From: Philipp Date: Wed, 27 Oct 2021 20:02:42 +0200 Subject: [PATCH 2/4] Update messages.po --- view/lang/C/messages.po | 46 ++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/view/lang/C/messages.po b/view/lang/C/messages.po index d71fea26f..0cb7b2c78 100644 --- a/view/lang/C/messages.po +++ b/view/lang/C/messages.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: 2021.12-dev\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2021-10-24 23:21-0400\n" +"POT-Creation-Date: 2021-10-27 20:01+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -2087,11 +2087,11 @@ msgid "" "your site allow private mail from unknown senders." msgstr "" -#: src/App.php:456 +#: src/App.php:455 msgid "No system theme config value set." msgstr "" -#: src/App/Module.php:240 +#: src/App/Module.php:241 msgid "You must be logged in to use addons. " msgstr "" @@ -3877,6 +3877,20 @@ msgstr "" msgid "template engine is not registered!" msgstr "" +#: src/Core/Storage/Type/FilesystemConfig.php:78 +msgid "Storage base path" +msgstr "" + +#: src/Core/Storage/Type/FilesystemConfig.php:80 +msgid "" +"Folder where uploaded files are saved. For maximum security, This should be " +"a path outside web server folder tree" +msgstr "" + +#: src/Core/Storage/Type/FilesystemConfig.php:93 +msgid "Enter a valid existing folder" +msgstr "" + #: src/Core/Update.php:67 #, php-format msgid "" @@ -4414,20 +4428,6 @@ msgstr "" msgid "Contact information and Social Networks" msgstr "" -#: src/Model/Storage/FilesystemConfig.php:77 -msgid "Storage base path" -msgstr "" - -#: src/Model/Storage/FilesystemConfig.php:79 -msgid "" -"Folder where uploaded files are saved. For maximum security, This should be " -"a path outside web server folder tree" -msgstr "" - -#: src/Model/Storage/FilesystemConfig.php:92 -msgid "Enter a valid existing folder" -msgstr "" - #: src/Model/User.php:208 src/Model/User.php:1050 msgid "SERIOUS ERROR: Generation of security keys failed." msgstr "" @@ -4475,13 +4475,13 @@ msgstr "" msgid "Invalid OpenID url" msgstr "" -#: src/Model/User.php:962 src/Security/Authentication.php:223 +#: src/Model/User.php:962 src/Security/Authentication.php:224 msgid "" "We encountered a problem while logging in with the OpenID you provided. " "Please check the correct spelling of the ID." msgstr "" -#: src/Model/User.php:962 src/Security/Authentication.php:223 +#: src/Model/User.php:962 src/Security/Authentication.php:224 msgid "The error message was:" msgstr "" @@ -10579,20 +10579,20 @@ msgstr "" msgid "The folder view/smarty3/ must be writable by webserver." msgstr "" -#: src/Security/Authentication.php:209 +#: src/Security/Authentication.php:210 msgid "Login failed." msgstr "" -#: src/Security/Authentication.php:250 +#: src/Security/Authentication.php:251 msgid "Login failed. Please check your credentials." msgstr "" -#: src/Security/Authentication.php:348 +#: src/Security/Authentication.php:349 #, php-format msgid "Welcome %s" msgstr "" -#: src/Security/Authentication.php:349 +#: src/Security/Authentication.php:350 msgid "Please upload a profile photo." msgstr "" From 7c38647573b3bd767c1a924753f1e2f11ab585e4 Mon Sep 17 00:00:00 2001 From: Philipp Date: Wed, 27 Oct 2021 20:06:04 +0200 Subject: [PATCH 3/4] Cleanup namespace --- src/Console/Storage.php | 2 +- src/Core/Storage/Repository/StorageManager.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Console/Storage.php b/src/Console/Storage.php index fbe55f34c..a4342e4b4 100644 --- a/src/Console/Storage.php +++ b/src/Console/Storage.php @@ -33,7 +33,7 @@ class Storage extends \Asika\SimpleConsole\Console { protected $helpOptions = ['h', 'help', '?']; - /** @var \Friendica\Core\Storage\Repository\StorageManager */ + /** @var StorageManager */ private $storageManager; /** diff --git a/src/Core/Storage/Repository/StorageManager.php b/src/Core/Storage/Repository/StorageManager.php index 5df6e5ac7..b8cb0c9e4 100644 --- a/src/Core/Storage/Repository/StorageManager.php +++ b/src/Core/Storage/Repository/StorageManager.php @@ -195,11 +195,11 @@ class StorageManager $this->backendInstances[$name] = new Type\Database($this->dba); break; // at least, try if there's an addon for the backend - case \Friendica\Core\Storage\Type\SystemResource::getName(): - $this->backendInstances[$name] = new \Friendica\Core\Storage\Type\SystemResource(); + case Type\SystemResource::getName(): + $this->backendInstances[$name] = new Type\SystemResource(); break; - case \Friendica\Core\Storage\Type\ExternalResource::getName(): - $this->backendInstances[$name] = new \Friendica\Core\Storage\Type\ExternalResource(); + case Type\ExternalResource::getName(): + $this->backendInstances[$name] = new Type\ExternalResource(); break; default: $data = [ From d778af0b0ee3462d6cff943e0a8d8a9a5364c67f Mon Sep 17 00:00:00 2001 From: Philipp Date: Wed, 27 Oct 2021 20:16:34 +0200 Subject: [PATCH 4/4] Cleanup namespaces --- src/Module/Admin/Storage.php | 3 +-- tests/src/Core/Storage/Repository/StorageManagerTest.php | 5 +++-- tests/src/Core/Storage/StorageConfigTest.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Module/Admin/Storage.php b/src/Module/Admin/Storage.php index 8eb706fa0..dfee3d236 100644 --- a/src/Module/Admin/Storage.php +++ b/src/Module/Admin/Storage.php @@ -40,7 +40,7 @@ class Storage extends BaseAdmin $storagebackend = Strings::escapeTags(trim($parameters['name'] ?? '')); try { - /** @var \Friendica\Core\Storage\Capability\ICanConfigureStorage|false $newStorageConfig */ + /** @var ICanConfigureStorage|false $newStorageConfig */ $newStorageConfig = DI::storageManager()->getConfigurationByName($storagebackend); } catch (InvalidClassStorageException $storageException) { notice(DI::l10n()->t('Storage backend, %s is invalid.', $storagebackend)); @@ -78,7 +78,6 @@ class Storage extends BaseAdmin if (!empty($_POST['submit_save_set'])) { try { - /** @var \Friendica\Core\Storage\Capability\ICanWriteToStorage $newstorage */ $newstorage = DI::storageManager()->getWritableStorageByName($storagebackend); if (!DI::storageManager()->setBackend($newstorage)) { diff --git a/tests/src/Core/Storage/Repository/StorageManagerTest.php b/tests/src/Core/Storage/Repository/StorageManagerTest.php index c7b656943..0ba28af82 100644 --- a/tests/src/Core/Storage/Repository/StorageManagerTest.php +++ b/tests/src/Core/Storage/Repository/StorageManagerTest.php @@ -31,6 +31,7 @@ use Friendica\Core\Session\Type\Memory; use Friendica\Core\Storage\Exception\InvalidClassStorageException; use Friendica\Core\Storage\Capability\ICanReadFromStorage; use Friendica\Core\Storage\Capability\ICanWriteToStorage; +use Friendica\Core\Storage\Exception\StorageException; use Friendica\Core\Storage\Repository\StorageManager; use Friendica\Core\Storage\Type\Filesystem; use Friendica\Core\Storage\Type\SystemResource; @@ -257,7 +258,7 @@ class StorageManagerTest extends DatabaseTest $dice = (new Dice()) ->addRules(include __DIR__ . '/../../../../../static/dependencies.config.php') ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]) - ->addRule(IHandleSessions::class, ['instanceOf' => Session\Type\Memory::class, 'shared' => true, 'call' => null]); + ->addRule(IHandleSessions::class, ['instanceOf' => Memory::class, 'shared' => true, 'call' => null]); DI::init($dice); $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n); @@ -308,7 +309,7 @@ class StorageManagerTest extends DatabaseTest self::assertInstanceOf(SampleStorageBackend::class, $storageManager->getBackend()); - self::expectException(\Friendica\Core\Storage\Exception\StorageException::class); + self::expectException(StorageException::class); self::expectExceptionMessage('Cannot unregister Sample Storage, because it\'s currently active.'); $storageManager->unregister(SampleStorageBackend::class); diff --git a/tests/src/Core/Storage/StorageConfigTest.php b/tests/src/Core/Storage/StorageConfigTest.php index a01be343d..51beef640 100644 --- a/tests/src/Core/Storage/StorageConfigTest.php +++ b/tests/src/Core/Storage/StorageConfigTest.php @@ -26,10 +26,10 @@ use Friendica\Test\MockedTest; abstract class StorageConfigTest extends MockedTest { - /** @return \Friendica\Core\Storage\Capability\ICanConfigureStorage */ + /** @return ICanConfigureStorage */ abstract protected function getInstance(); - abstract protected function assertOption(\Friendica\Core\Storage\Capability\ICanConfigureStorage $storage); + abstract protected function assertOption(ICanConfigureStorage $storage); /** * Test if the "getOption" is asserted