]> git.mxchange.org Git - friendica.git/blob - src/Core/StorageManager.php
6f3e98ae7e2e57d77004b858ae3015764878e462
[friendica.git] / src / Core / StorageManager.php
1 <?php
2
3 namespace Friendica\Core;
4
5 use Friendica\Core\Config;
6
7
8
9 /**
10  * @brief Manage storage backends
11  *
12  * Core code uses this class to get and set current storage backend class.
13  * Addons use this class to register and unregister additional backends.
14  */
15 class StorageManager
16 {
17         private static $default_storages = [
18                 'Filesystem' => \Friendica\Model\Storage\Filesystem::class,
19                 'Database' => \Friendica\Model\Storage\Database::class,
20         ];
21
22         private static $storages = [];
23
24         private static function setup()
25         {
26                 if (count(self::$storages)==0) {
27                         self::$storage = Config::get('storage', 'backends', self::$default_storages);
28                 }
29         }
30
31         /**
32          * @brief Return current storage backend class
33          * @return string
34          */
35         public static function getBackend()
36         {
37                 return Config::get('storage', 'class', '');
38         }
39
40         /**
41          * @brief Return storage backend class by registered name
42          *
43          * @param string  $name  Backend name
44          * @return string Empty if no backend registered at $name exists
45          */
46         public static function getByName($name)
47         {
48                 self::setup();
49                 return defaults(self::$storages, $name, '');
50         }
51
52         /**
53          * @brief Set current storage backend class
54          *
55          * @param string  $class  Backend class name
56          */
57         public static function setBackend($class)
58         {
59                 /// @todo Check that $class implements IStorage
60                 Config::set('storage', 'class', $class);
61         }
62
63         /**
64          * @brief Get registered backends
65          *
66          * @return array
67          */
68         public static function listBackends()
69         {
70                 self::setup();
71                 return self::$backends;
72         }
73
74
75
76         /**
77          * @brief Register a storage backend class
78          *
79          * @param string  $name   User readable backend name
80          * @param string  $class  Backend class name
81          */
82         public static function register($name, $class)
83         {
84                 /// @todo Check that $class implements IStorage
85                 self::setup();
86                 self::$storages[$name] = $class;
87                 Config::set('storage', 'backends', self::$storages);
88         }
89
90
91         /**
92          * @brief Unregister a storage backend class
93          *
94          * @param string  $name   User readable backend name
95          */
96         public static function unregister($class)
97         {
98                 self::setup();
99                 unset(self::$storages[$name]);
100                 Config::set('storage', 'backends', self::$storages);
101         }
102 }