mirror of
https://github.com/friendica/friendica
synced 2025-04-25 01:10:12 +00:00
Add storage backend manager class
This commit is contained in:
parent
e5c2d4e2f8
commit
6a0ed7c298
2 changed files with 104 additions and 1 deletions
102
src/Core/StorageManager.php
Normal file
102
src/Core/StorageManager.php
Normal file
|
@ -0,0 +1,102 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core;
|
||||
|
||||
use Friendica\Core\Config;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Manage storage backends
|
||||
*
|
||||
* Core code uses this class to get and set current storage backend class.
|
||||
* Addons use this class to register and unregister additional backends.
|
||||
*/
|
||||
class StorageManager
|
||||
{
|
||||
private static $default_storages = [
|
||||
'Filesystem' => \Friendica\Model\Storage\Filesystem::class,
|
||||
'Database' => \Friendica\Model\Storage\Database::class,
|
||||
];
|
||||
|
||||
private static $storages = [];
|
||||
|
||||
private static function setup()
|
||||
{
|
||||
if (count(self::$storages)==0) {
|
||||
self::$storage = Config::get('storage', 'backends', self::$default_storages);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return current storage backend class
|
||||
* @return string
|
||||
*/
|
||||
public static function getBackend()
|
||||
{
|
||||
return Config::get('storage', 'class', '');
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return storage backend class by registered name
|
||||
*
|
||||
* @param string $name Backend name
|
||||
* @return string Empty if no backend registered at $name exists
|
||||
*/
|
||||
public static function getByName($name)
|
||||
{
|
||||
self::setup();
|
||||
return defaults(self::$storages, $name, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set current storage backend class
|
||||
*
|
||||
* @param string $class Backend class name
|
||||
*/
|
||||
public static function setBackend($class)
|
||||
{
|
||||
/// @todo Check that $class implements IStorage
|
||||
Config::set('storage', 'class', $class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get registered backends
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function listBackends()
|
||||
{
|
||||
self::setup();
|
||||
return self::$backends;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Register a storage backend class
|
||||
*
|
||||
* @param string $name User readable backend name
|
||||
* @param string $class Backend class name
|
||||
*/
|
||||
public static function register($name, $class)
|
||||
{
|
||||
/// @todo Check that $class implements IStorage
|
||||
self::setup();
|
||||
self::$storages[$name] = $class;
|
||||
Config::set('storage', 'backends', self::$storages);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Unregister a storage backend class
|
||||
*
|
||||
* @param string $name User readable backend name
|
||||
*/
|
||||
public static function unregister($class)
|
||||
{
|
||||
self::setup();
|
||||
unset(self::$storages[$name]);
|
||||
Config::set('storage', 'backends', self::$storages);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue