```php
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
-abstract class StorageTest
+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
abstract protected function assertOption(ICanWriteToStorage $storage);
-}
+}
```
## Exception handling
```php
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
-class ExampleStorage implements ICanWriteToStorage
+class ExampleStorage implements ICanWriteToStorage
{
public function get(string $reference) : string
{
throw new \Friendica\Core\Storage\Exception\StorageException(sprintf('The Example Storage throws an exception for reference %s', $reference), 500, $exception);
}
}
-}
+}
```
## Example
/**
* SampleStorageBackend 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
+ * Friendica automatically creates these classes and passes them as argument to the constructor
*/
- public function __construct(string $filename)
+ public function __construct(string $filename)
{
$this->filename = $filename;
}
// a config key
return file_get_contents($this->filename);
}
-
+
public function put(string $data, string $reference = '')
{
if ($reference === '') {
// we don't save $data !
return $reference;
}
-
+
public function delete(string $reference)
{
// we pretend to delete the data
return true;
}
-
+
public function __toString()
{
return self::NAME;
/**
* 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
+ * Friendica automatically creates these classes and passes them as argument to the constructor
*/
- public function __construct(IManageConfigValues $config, L10n $l10n)
+ public function __construct(IManageConfigValues $config, L10n $l10n)
{
$this->config = $config;
$this->l10n = $l10n;
],
];
}
-
+
public function saveOptions(array $data)
{
// the keys in $data are the same keys we defined in getOptions()
$newfilename = trim($data['filename']);
-
+
// this function should always validate the data.
// in this example we check if file exists
if (!file_exists($newfilename)) {
// ['optionname' => 'error message']
return ['filename' => 'The file doesn\'t exists'];
}
-
+
$this->config->set('storage', 'samplestorage', $newfilename);
-
+
// no errors, return empty array
return [];
}
DI::storageManager()->unregister(SampleStorageBackend::class);
}
-function samplestorage_storage_instance(App $a, array &$data)
+function samplestorage_storage_instance(AppHelper $appHelper, array &$data)
{
$config = new SampleStorageBackendConfig(DI::l10n(), DI::config());
$data['storage'] = new SampleStorageBackendConfig($config->getFileName());
}
-function samplestorage_storage_config(App $a, array &$data)
+function samplestorage_storage_config(AppHelper $appHelper, array &$data)
{
$data['storage_config'] = new SampleStorageBackendConfig(DI::l10n(), DI::config());
}
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
use Friendica\Test\src\Core\Storage\StorageTest;
-class SampleStorageTest extends StorageTest
+class SampleStorageTest extends StorageTest
{
// returns an instance of your newly created storage class
protected function getInstance()
],
], $storage->getOptions());
}
-}
+}
```