2018-12-12 19:36:56 +00:00
Friendica Storage Backend Addon development
===========================================
* [Home ](help )
Storage backends can be added via addons.
2021-10-23 10:11:38 +00:00
A storage backend is implemented as a class, and the plugin register the class to make it available to the system.
2018-12-12 19:36:56 +00:00
## The Storage Backend Class
The class must live in `Friendica\Addon\youraddonname` namespace, where `youraddonname` the folder name of your addon.
2021-10-04 08:37:31 +00:00
There are two different interfaces you need to implement.
2021-10-23 10:11:38 +00:00
### `ICanWriteToStorage`
2018-12-12 19:36:56 +00:00
2021-10-23 10:11:38 +00:00
The class must implement `Friendica\Core\Storage\Capability\ICanWriteToStorage` interface. All method in the interface must be implemented:
2018-12-12 19:36:56 +00:00
2018-12-12 19:52:09 +00:00
```php
2021-10-23 10:11:38 +00:00
namespace Friendica\Core\Storage\Capability\ICanWriteToStorage;
2021-10-04 08:37:31 +00:00
2021-10-23 10:11:38 +00:00
interface ICanWriteToStorage
2018-12-12 19:52:09 +00:00
{
2020-01-05 21:33:27 +00:00
public function get(string $reference);
public function put(string $data, string $reference = '');
public function delete(string $reference);
public function __toString();
2020-01-09 00:48:48 +00:00
public static function getName();
2018-12-12 19:52:09 +00:00
}
```
2018-12-12 19:36:56 +00:00
2020-01-05 21:33:27 +00:00
- `get(string $reference)` returns data pointed by `$reference`
- `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`
2018-12-12 19:36:56 +00:00
2021-10-23 10:11:38 +00:00
### `ICanConfigureStorage`
2021-10-04 08:37:31 +00:00
2018-12-12 19:36:56 +00:00
Each storage backend can have options the admin can set in admin page.
2021-10-23 10:11:38 +00:00
To make the options possible, you need to implement the `Friendica\Core\Storage\Capability\ICanConfigureStorage` interface.
2021-10-04 08:37:31 +00:00
2021-10-05 16:35:50 +00:00
All methods in the interface must be implemented:
2021-10-04 08:37:31 +00:00
```php
2021-10-23 10:11:38 +00:00
namespace Friendica\Core\Storage\Capability\ICanConfigureStorage;
2021-10-04 08:37:31 +00:00
2021-10-23 10:11:38 +00:00
interface ICanConfigureStorage
2021-10-04 08:37:31 +00:00
{
public function getOptions();
public function saveOptions(array $data);
}
```
2018-12-12 19:36:56 +00:00
- `getOptions()` returns an array with details about each option to build the interface.
2020-01-05 21:33:27 +00:00
- `saveOptions(array $data)` get `$data` from admin page, validate it and save it.
2018-12-12 19:36:56 +00:00
The array returned by `getOptions()` is defined as:
[
'option1name' => [ ..info.. ],
'option2name' => [ ..info.. ],
...
]
An empty array can be returned if backend doesn't have any options.
The info array for each option is defined as:
[
'type',
define the field used in form, and the type of data.
2020-02-05 16:30:35 +00:00
one of 'checkbox', 'combobox', 'custom', 'datetime', 'input', 'intcheckbox', 'password', 'radio', 'richtext', 'select', 'select_raw', 'textarea'
2018-12-12 19:36:56 +00:00
'label',
Translatable label of the field. This label will be shown in admin page
value,
Current value of the option
'help text',
Translatable description for the field. Will be shown in admin page
extra data
Optional. Depends on which 'type' this option is:
- 'select': array `[ value => label ]` of choices
- 'intcheckbox': value of input element
- 'select_raw': prebuild html string of `<option >` tags
Each label should be translatable
];
2021-08-10 20:07:52 +00:00
See doxygen documentation of `IWritableStorage` interface for details about each method.
2018-12-12 19:36:56 +00:00
## Register a storage backend class
2023-03-22 03:16:52 +00:00
Each backend must be registered in the system when the plugin is installed, to be available.
2018-12-12 19:36:56 +00:00
2020-01-08 21:51:37 +00:00
`DI::facStorage()->register(string $class)` is used to register the backend class.
2018-12-12 19:36:56 +00:00
When the plugin is uninstalled, registered backends must be unregistered using
2020-01-08 21:51:37 +00:00
`DI::facStorage()->unregister(string $class)` .
You have to register a new hook in your addon, listening on `storage_instance(App $a, array $data)` .
2021-10-23 10:11:38 +00:00
In case `$data['name']` is your storage class name, you have to instance a new instance of your `Friendica\Core\Storage\Capability\ICanReadFromStorage` class.
2020-01-08 21:51:37 +00:00
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.
2020-01-05 21:33:27 +00:00
## Adding tests
**Currently testing is limited to core Friendica only, this shows theoretically how tests should work in the future**
Each new Storage class should be added to the test-environment at [Storage Tests ](https://github.com/friendica/friendica/tree/develop/tests/src/Model/Storage/ ).
Add a new test class which's naming convention is `StorageClassTest` , which extend the `StorageTest` in the same directory.
Override the two necessary instances:
2021-08-10 20:07:52 +00:00
2020-01-05 21:33:27 +00:00
```php
2021-10-23 10:11:38 +00:00
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
2020-01-05 21:33:27 +00:00
abstract class StorageTest
{
// returns an instance of your newly created storage class
abstract protected function getInstance();
// Assertion for the option array you return for your new StorageClass
2021-10-23 10:11:38 +00:00
abstract protected function assertOption(ICanWriteToStorage $storage);
2020-01-05 21:33:27 +00:00
}
```
2018-12-12 19:36:56 +00:00
2021-08-07 20:34:09 +00:00
## Exception handling
There are two intended types of exceptions for storages
2023-03-22 03:17:45 +00:00
### `ReferenceStorageException`
2021-08-10 19:30:40 +00:00
This storage exception should be used in case the caller tries to use an invalid references.
This could happen in case the caller tries to delete or update an unknown reference.
The implementation of the storage backend must not ignore invalid references.
2023-03-22 03:17:45 +00:00
Avoid throwing the common `StorageException` instead of the `ReferenceStorageException` at this particular situation!
2021-08-10 19:30:40 +00:00
2021-08-07 20:34:09 +00:00
### `StorageException`
This is the common exception in case unexpected errors happen using the storage backend.
2023-03-22 03:17:45 +00:00
If there's a predecessor to this exception (e.g. you caught an exception and are throwing this exception), you should add the predecessor for transparency reasons.
2021-08-07 20:34:09 +00:00
Example:
```php
2021-10-23 10:11:38 +00:00
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
2021-08-07 20:34:09 +00:00
2021-10-23 10:11:38 +00:00
class ExampleStorage implements ICanWriteToStorage
2021-08-07 20:34:09 +00:00
{
public function get(string $reference) : string
{
try {
throw new Exception('a real bad exception');
} catch (Exception $exception) {
2021-10-23 10:11:38 +00:00
throw new \Friendica\Core\Storage\Exception\StorageException(sprintf('The Example Storage throws an exception for reference %s', $reference), 500, $exception);
2021-08-07 20:34:09 +00:00
}
}
}
```
2018-12-12 19:36:56 +00:00
## Example
2021-10-04 08:37:31 +00:00
Here is a hypothetical addon which register a useless storage backend.
2018-12-12 19:36:56 +00:00
Let's call it `samplestorage` .
This backend will discard all data we try to save and will return always the same image when we ask for some data.
The image returned can be set by the administrator in admin page.
First, the backend class.
The file will be `addon/samplestorage/SampleStorageBackend.php` :
```php
< ?php
namespace Friendica\Addon\samplestorage;
2021-10-23 10:11:38 +00:00
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
2018-12-12 19:36:56 +00:00
2021-10-26 19:44:29 +00:00
use Friendica\Core\Config\Capability\IManageConfigValues;
2018-12-12 19:36:56 +00:00
use Friendica\Core\L10n;
2021-10-23 10:11:38 +00:00
class SampleStorageBackend implements ICanWriteToStorage
2018-12-12 19:36:56 +00:00
{
2020-01-05 21:33:27 +00:00
const NAME = 'Sample Storage';
2021-10-04 08:37:31 +00:00
/** @var string */
private $filename;
2020-01-05 21:33:27 +00:00
/**
* SampleStorageBackend constructor.
2021-10-04 08:37:31 +00:00
*
2020-01-05 21:33:27 +00:00
* You can add here every dynamic class as dependency you like and add them to a private field
* Friendica automatically creates these classes and passes them as argument to the constructor
*/
2021-10-04 08:37:31 +00:00
public function __construct(string $filename)
2020-01-05 21:33:27 +00:00
{
2021-10-04 08:37:31 +00:00
$this->filename = $filename;
2020-01-05 21:33:27 +00:00
}
public function get(string $reference)
2018-12-12 19:36:56 +00:00
{
2020-01-05 21:33:27 +00:00
// we return always the same image data. Which file we load is defined by
2018-12-12 19:36:56 +00:00
// a config key
2021-10-04 08:37:31 +00:00
return file_get_contents($this->filename);
2018-12-12 19:36:56 +00:00
}
2020-01-05 21:33:27 +00:00
public function put(string $data, string $reference = '')
2018-12-12 19:36:56 +00:00
{
2020-01-05 21:33:27 +00:00
if ($reference === '') {
$reference = 'sample';
2018-12-12 19:36:56 +00:00
}
// we don't save $data !
2020-01-05 21:33:27 +00:00
return $reference;
2018-12-12 19:36:56 +00:00
}
2020-01-05 21:33:27 +00:00
public function delete(string $reference)
2018-12-12 19:36:56 +00:00
{
// we pretend to delete the data
return true;
}
2021-10-04 08:37:31 +00:00
public function __toString()
{
return self::NAME;
}
public static function getName()
{
return self::NAME;
}
}
```
```php
< ?php
namespace Friendica\Addon\samplestorage;
2021-10-23 10:11:38 +00:00
use Friendica\Core\Storage\Capability\ICanConfigureStorage;
2021-10-04 08:37:31 +00:00
2021-10-26 19:44:29 +00:00
use Friendica\Core\Config\Capability\IManageConfigValues;
2021-10-04 08:37:31 +00:00
use Friendica\Core\L10n;
2021-10-23 10:11:38 +00:00
class SampleStorageBackendConfig implements ICanConfigureStorage
2021-10-04 08:37:31 +00:00
{
2021-10-26 19:44:29 +00:00
/** @var \Friendica\Core\Config\Capability\IManageConfigValues */
2021-10-04 08:37:31 +00:00
private $config;
/** @var L10n */
private $l10n;
/**
* SampleStorageBackendConfig constructor.
*
* You can add here every dynamic class as dependency you like and add them to a private field
* Friendica automatically creates these classes and passes them as argument to the constructor
*/
2021-10-26 19:44:29 +00:00
public function __construct(IManageConfigValues $config, L10n $l10n)
2021-10-04 08:37:31 +00:00
{
$this->config = $config;
$this->l10n = $l10n;
}
public function getFileName(): string
{
return $this->config->get('storage', 'samplestorage', 'sample.jpg');
}
2020-01-05 21:33:27 +00:00
public function getOptions()
2018-12-12 19:36:56 +00:00
{
2020-01-05 21:33:27 +00:00
$filename = $this->config->get('storage', 'samplestorage', 'sample.jpg');
2018-12-12 19:36:56 +00:00
return [
2020-01-05 21:33:27 +00:00
'filename' => [
'input', // will use a simple text input
$this->l10n->t('The file to return'), // the label
2018-12-12 19:36:56 +00:00
$filename, // the current value
2020-01-05 21:33:27 +00:00
$this->l10n->t('Enter the path to a file'), // the help text
// no extra data for 'input' type..
],
2018-12-12 19:36:56 +00:00
];
}
2020-01-05 21:33:27 +00:00
public function saveOptions(array $data)
2018-12-12 19:36:56 +00:00
{
// the keys in $data are the same keys we defined in getOptions()
2020-01-05 21:33:27 +00:00
$newfilename = trim($data['filename']);
2018-12-12 19:36:56 +00:00
// this function should always validate the data.
// in this example we check if file exists
if (!file_exists($newfilename)) {
// in case of error we return an array with
2020-01-05 21:33:27 +00:00
// ['optionname' => 'error message']
return ['filename' => 'The file doesn\'t exists'];
2018-12-12 19:36:56 +00:00
}
2020-01-05 21:33:27 +00:00
$this->config->set('storage', 'samplestorage', $newfilename);
2018-12-12 19:36:56 +00:00
// no errors, return empty array
return [];
}
2020-01-05 21:33:27 +00:00
2018-12-12 19:36:56 +00:00
}
```
Now the plugin main file. Here we register and unregister the backend class.
The file is `addon/samplestorage/samplestorage.php`
```php
< ?php
/**
* Name: Sample Storage Addon
2023-03-26 20:05:26 +00:00
* Description: A sample addon which implements a very limited storage backend
2018-12-12 19:36:56 +00:00
* Version: 1.0.0
* Author: Alice < https: // alice . social /~ alice >
*/
use Friendica\Addon\samplestorage\SampleStorageBackend;
2021-10-04 08:37:31 +00:00
use Friendica\Addon\samplestorage\SampleStorageBackendConfig;
2020-01-05 00:58:49 +00:00
use Friendica\DI;
2018-12-12 19:36:56 +00:00
function samplestorage_install()
{
2021-10-04 08:37:31 +00:00
Hook::register('storage_instance' , __FILE__ , 'samplestorage_storage_instance');
Hook::register('storage_config' , __FILE__ , 'samplestorage_storage_config');
2020-01-09 00:48:48 +00:00
DI::storageManager()->register(SampleStorageBackend::class);
2018-12-12 19:36:56 +00:00
}
2021-10-05 17:06:13 +00:00
function samplestorage_storage_uninstall()
2018-12-12 19:36:56 +00:00
{
2020-01-09 00:48:48 +00:00
DI::storageManager()->unregister(SampleStorageBackend::class);
2018-12-12 19:36:56 +00:00
}
2020-01-08 21:51:37 +00:00
2021-10-05 17:06:13 +00:00
function samplestorage_storage_instance(App $a, array & $data)
2020-01-08 21:51:37 +00:00
{
2021-10-04 08:37:31 +00:00
$config = new SampleStorageBackendConfig(DI::l10n(), DI::config());
$data['storage'] = new SampleStorageBackendConfig($config->getFileName());
2020-01-08 21:51:37 +00:00
}
2021-10-04 08:37:31 +00:00
2021-10-05 17:06:13 +00:00
function samplestorage_storage_config(App $a, array & $data)
2021-10-04 08:37:31 +00:00
{
2021-10-05 17:06:13 +00:00
$data['storage_config'] = new SampleStorageBackendConfig(DI::l10n(), DI::config());
2021-10-04 08:37:31 +00:00
}
2018-12-12 19:52:09 +00:00
```
2018-12-12 19:36:56 +00:00
2020-01-05 21:33:27 +00:00
**Theoretically - until tests for Addons are enabled too - create a test class with the name `addon/tests/SampleStorageTest.php` :
2018-12-12 19:36:56 +00:00
2020-01-05 21:33:27 +00:00
```php
2021-10-23 10:11:38 +00:00
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
use Friendica\Test\src\Core\Storage\StorageTest;
2018-12-12 19:36:56 +00:00
2020-01-05 21:33:27 +00:00
class SampleStorageTest extends StorageTest
{
// returns an instance of your newly created storage class
protected function getInstance()
{
// create a new SampleStorageBackend instance with all it's dependencies
// Have a look at DatabaseStorageTest or FilesystemStorageTest for further insights
return new SampleStorageBackend();
}
// Assertion for the option array you return for your new StorageClass
2021-10-23 10:11:38 +00:00
protected function assertOption(ICanWriteToStorage $storage)
2020-01-05 21:33:27 +00:00
{
$this->assertEquals([
'filename' => [
'input',
'The file to return',
'sample.jpg',
'Enter the path to a file'
],
], $storage->getOptions());
}
}
```