]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/ConfigCacheLoader.php
codeblock updates
[friendica.git] / src / Core / Config / ConfigCacheLoader.php
1 <?php
2
3 namespace Friendica\Core\Config;
4
5 use Friendica\Core\Addon;
6
7 /**
8  * The ConfigCacheLoader loads config-files and stores them in a ConfigCache ( @see ConfigCache )
9  *
10  * It is capable of loading the following config files:
11  * - *.config.php   (current)
12  * - *.ini.php      (deprecated)
13  * - *.htconfig.php (deprecated)
14  */
15 class ConfigCacheLoader
16 {
17         /**
18          * The Sub directory of the config-files
19          * @var string
20          */
21         const SUBDIRECTORY = 'config';
22
23         private $baseDir;
24         private $configDir;
25
26         public function __construct($baseDir)
27         {
28                 $this->baseDir = $baseDir;
29                 $this->configDir = $baseDir . DIRECTORY_SEPARATOR . self::SUBDIRECTORY;
30         }
31
32         /**
33          * Load the configuration files
34          *
35          * First loads the default value for all the configuration keys, then the legacy configuration files, then the
36          * expected local.config.php
37          */
38         public function loadConfigFiles(ConfigCache $config)
39         {
40                 // Setting at least the basepath we know
41                 $config->set('system', 'basepath', $this->baseDir);
42
43                 $config->loadConfigArray($this->loadCoreConfig('defaults'));
44                 $config->loadConfigArray($this->loadCoreConfig('settings'));
45
46                 $config->loadConfigArray($this->loadLegacyConfigFile('htpreconfig'), true);
47                 $config->loadConfigArray($this->loadLegacyConfigFile('htconfig'), true);
48
49                 $config->loadConfigArray($this->loadCoreConfig('local'), true);
50         }
51
52         /**
53          * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php)
54          *
55          * @param string $name The name of the config file
56          * @return array The configuration array
57          *
58          * @deprecated since version 2018.09
59          */
60         private function loadLegacyConfigFile($name)
61         {
62                 $filePath = $this->baseDir  . DIRECTORY_SEPARATOR . '.' . $name . '.php';
63
64                 if (file_exists($filePath)) {
65                         $a = new \stdClass();
66                         $a->config = [];
67                         include $filePath;
68
69                         if (isset($db_host)) {
70                                 $a->config['database']['hostname'] = $db_host;
71                                 unset($db_host);
72                         }
73                         if (isset($db_user)) {
74                                 $a->config['database']['username'] = $db_user;
75                                 unset($db_user);
76                         }
77                         if (isset($db_pass)) {
78                                 $a->config['database']['password'] = $db_pass;
79                                 unset($db_pass);
80                         }
81                         if (isset($db_data)) {
82                                 $a->config['database']['database'] = $db_data;
83                                 unset($db_data);
84                         }
85                         if (isset($a->config['system']['db_charset'])) {
86                                 $a->config['database']['charset'] = $a->config['system']['charset'];
87                         }
88                         if (isset($pidfile)) {
89                                 $a->config['system']['pidfile'] = $pidfile;
90                                 unset($pidfile);
91                         }
92                         if (isset($default_timezone)) {
93                                 $a->config['system']['default_timezone'] = $default_timezone;
94                                 unset($default_timezone);
95                         }
96                         if (isset($lang)) {
97                                 $a->config['system']['language'] = $lang;
98                                 unset($lang);
99                         }
100
101                         return $a->config;
102                 } else {
103                         return [];
104                 }
105         }
106
107         /**
108          * Tries to load the specified legacy configuration file and returns the config array.
109          *
110          * @deprecated since version 2018.12
111          * @param string $filename
112          *
113          * @return array The configuration array
114          * @throws \Exception
115          */
116         public function loadINIConfigFile($filename)
117         {
118                 $filepath = $this->configDir . DIRECTORY_SEPARATOR . $filename . ".ini.php";
119
120                 if (!file_exists($filepath)) {
121                         throw new \Exception('Error parsing non-existent INI config file ' . $filepath);
122                 }
123
124                 $contents = include($filepath);
125
126                 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
127
128                 if ($config === false) {
129                         throw new \Exception('Error parsing INI config file ' . $filepath);
130                 }
131
132                 return $config;
133         }
134
135         /**
136          * Tries to load the specified configuration file and returns the config array.
137          *
138          * The config format is PHP array and the template for configuration files is the following:
139          *
140          * <?php return [
141          *      'section' => [
142          *          'key' => 'value',
143          *      ],
144          * ];
145          *
146          * @param  string $filepath The filepath of the
147          * @return array The config array0
148          *
149          * @throws \Exception if the config cannot get loaded.
150          */
151         private function loadConfigFile($filepath)
152         {
153                 if (!file_exists($filepath)) {
154                         throw new \Exception('Error loading non-existent config file ' . $filepath);
155                 }
156
157                 $config = include($filepath);
158
159                 if (!is_array($config)) {
160                         throw new \Exception('Error loading config file ' . $filepath);
161                 }
162
163                 return $config;
164         }
165
166         /**
167          * Tries to load the specified core-configuration and returns the config array.
168          *
169          * @param string $name The name of the configuration
170          *
171          * @return array The config array (empty if no config found)
172          *
173          * @throws \Exception if the configuration file isn't readable
174          */
175         public function loadCoreConfig($name)
176         {
177                 if (file_exists($this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php')) {
178                         return $this->loadConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php');
179                 } elseif (file_exists($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php')) {
180                         return $this->loadINIConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php');
181                 } else {
182                         return [];
183                 }
184         }
185
186         /**
187          * Tries to load the specified addon-configuration and returns the config array.
188          *
189          * First loads all activated addons default configuration through the load_config hook, then load the local.config.php
190          * again to overwrite potential local addon configuration.
191          *
192          * @param string $name The name of the configuration
193          *
194          * @return array The config array (empty if no config found)
195          *
196          * @throws \Exception if the configuration file isn't readable
197          */
198         public function loadAddonConfig($name)
199         {
200                 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
201                         Addon::DIRECTORY       . DIRECTORY_SEPARATOR . // addon/
202                         $name                  . DIRECTORY_SEPARATOR . // openstreetmap/
203                         self::SUBDIRECTORY     . DIRECTORY_SEPARATOR . // config/
204                         $name . ".config.php";                         // openstreetmap.config.php
205
206                 if (file_exists($filepath)) {
207                         return $this->loadConfigFile($filepath);
208                 } else {
209                         return [];
210                 }
211         }
212 }