]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Adapter/PreloadConfigAdapter.php
updated the credits
[friendica.git] / src / Core / Config / Adapter / PreloadConfigAdapter.php
1 <?php
2
3 namespace Friendica\Core\Config\Adapter;
4
5 use Friendica\Database\DBA;
6
7 /**
8  * Preload Configuration Adapter
9  *
10  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
11  *
12  * @author Hypolite Petovan <hypolite@mrpetovan.com>
13  */
14 class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
15 {
16         private $config_loaded = false;
17
18         /**
19          * {@inheritdoc}
20          */
21         public function load($cat = 'config')
22         {
23                 $return = [];
24
25                 if (!$this->isConnected()) {
26                         return $return;
27                 }
28
29                 if ($this->config_loaded) {
30                         return $return;
31                 }
32
33                 $configs = DBA::select('config', ['cat', 'v', 'k']);
34                 while ($config = DBA::fetch($configs)) {
35                         $value = $config['v'];
36                         if (isset($value) && $value !== '') {
37                                 $return[$config['cat']][$config['k']] = $value;
38                         } else {
39                                 $return[$config['cat']][$config['k']] = '!<unset>!';
40                         }
41                 }
42                 DBA::close($configs);
43
44                 $this->config_loaded = true;
45
46                 return $return;
47         }
48
49         /**
50          * {@inheritdoc}
51          */
52         public function get($cat, $key)
53         {
54                 if (!$this->isConnected()) {
55                         return '!<unset>!';
56                 }
57
58                 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
59                 if (DBA::isResult($config)) {
60                         // manage array value
61                         $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
62
63                         if (isset($value) && $value !== '') {
64                                 return $value;
65                         }
66                 }
67
68                 return '!<unset>!';
69         }
70
71         /**
72          * {@inheritdoc}
73          */
74         public function set($cat, $key, $value)
75         {
76                 if (!$this->isConnected()) {
77                         return false;
78                 }
79
80                 // We store our setting values as strings.
81                 // So we have to do the conversion here so that the compare below works.
82                 // The exception are array values.
83                 $compare_value = !is_array($value) ? (string)$value : $value;
84
85                 if ($this->get($cat, $key) === $compare_value) {
86                         return true;
87                 }
88
89                 // manage array value
90                 $dbvalue = is_array($value) ? serialize($value) : $value;
91
92                 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
93
94                 return $result;
95         }
96
97         /**
98          * {@inheritdoc}
99          */
100         public function delete($cat, $key)
101         {
102                 if (!$this->isConnected()) {
103                         return false;
104                 }
105
106                 $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
107
108                 return $result;
109         }
110
111         /**
112          * {@inheritdoc}
113          */
114         public function isLoaded($cat, $key)
115         {
116                 if (!$this->isConnected()) {
117                         return false;
118                 }
119
120                 return $this->config_loaded;
121         }
122 }