]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Cache/ConfigCacheLoader.php
Refactoring Logging to use Configuration
[friendica.git] / src / Core / Config / Cache / ConfigCacheLoader.php
1 <?php
2
3 namespace Friendica\Core\Config\Cache;
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->load($this->loadCoreConfig('defaults'));
44                 $config->load($this->loadCoreConfig('settings'));
45
46                 $config->load($this->loadLegacyConfig('htpreconfig'), true);
47                 $config->load($this->loadLegacyConfig('htconfig'), true);
48
49                 $config->load($this->loadCoreConfig('local'), true);
50         }
51
52         /**
53          * Tries to load the specified core-configuration and returns the config array.
54          *
55          * @param string $name The name of the configuration
56          *
57          * @return array The config array (empty if no config found)
58          *
59          * @throws \Exception if the configuration file isn't readable
60          */
61         public function loadCoreConfig($name)
62         {
63                 if (file_exists($this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php')) {
64                         return $this->loadConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php');
65                 } elseif (file_exists($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php')) {
66                         return $this->loadINIConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php');
67                 } else {
68                         return [];
69                 }
70         }
71
72         /**
73          * Tries to load the specified addon-configuration and returns the config array.
74          *
75          * @param string $name The name of the configuration
76          *
77          * @return array The config array (empty if no config found)
78          *
79          * @throws \Exception if the configuration file isn't readable
80          */
81         public function loadAddonConfig($name)
82         {
83                 $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
84                         Addon::DIRECTORY       . DIRECTORY_SEPARATOR . // addon/
85                         $name                  . DIRECTORY_SEPARATOR . // openstreetmap/
86                         self::SUBDIRECTORY     . DIRECTORY_SEPARATOR . // config/
87                         $name . ".config.php";                         // openstreetmap.config.php
88
89                 if (file_exists($filepath)) {
90                         return $this->loadConfigFile($filepath);
91                 } else {
92                         return [];
93                 }
94         }
95
96         /**
97          * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
98          *
99          * @param string $name The name of the config file
100          *
101          * @return array The configuration array (empty if no config found)
102          *
103          * @deprecated since version 2018.09
104          */
105         private function loadLegacyConfig($name)
106         {
107                 $filePath = $this->baseDir  . DIRECTORY_SEPARATOR . '.' . $name . '.php';
108
109                 if (file_exists($filePath)) {
110                         $a = new \stdClass();
111                         $a->config = [];
112                         include $filePath;
113
114                         if (isset($db_host)) {
115                                 $a->config['database']['hostname'] = $db_host;
116                                 unset($db_host);
117                         }
118                         if (isset($db_user)) {
119                                 $a->config['database']['username'] = $db_user;
120                                 unset($db_user);
121                         }
122                         if (isset($db_pass)) {
123                                 $a->config['database']['password'] = $db_pass;
124                                 unset($db_pass);
125                         }
126                         if (isset($db_data)) {
127                                 $a->config['database']['database'] = $db_data;
128                                 unset($db_data);
129                         }
130                         if (isset($a->config['system']['db_charset'])) {
131                                 $a->config['database']['charset'] = $a->config['system']['charset'];
132                         }
133                         if (isset($pidfile)) {
134                                 $a->config['system']['pidfile'] = $pidfile;
135                                 unset($pidfile);
136                         }
137                         if (isset($default_timezone)) {
138                                 $a->config['system']['default_timezone'] = $default_timezone;
139                                 unset($default_timezone);
140                         }
141                         if (isset($lang)) {
142                                 $a->config['system']['language'] = $lang;
143                                 unset($lang);
144                         }
145
146                         return $a->config;
147                 } else {
148                         return [];
149                 }
150         }
151
152         /**
153          * Tries to load the specified legacy configuration file and returns the config array.
154          *
155          * @deprecated since version 2018.12
156          * @param string $filepath
157          *
158          * @return array The configuration array
159          * @throws \Exception
160          */
161         private function loadINIConfigFile($filepath)
162         {
163                 $contents = include($filepath);
164
165                 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
166
167                 if ($config === false) {
168                         throw new \Exception('Error parsing INI config file ' . $filepath);
169                 }
170
171                 return $config;
172         }
173
174         /**
175          * Tries to load the specified configuration file and returns the config array.
176          *
177          * The config format is PHP array and the template for configuration files is the following:
178          *
179          * <?php return [
180          *      'section' => [
181          *          'key' => 'value',
182          *      ],
183          * ];
184          *
185          * @param  string $filepath The filepath of the
186          * @return array The config array0
187          *
188          * @throws \Exception if the config cannot get loaded.
189          */
190         private function loadConfigFile($filepath)
191         {
192                 $config = include($filepath);
193
194                 if (!is_array($config)) {
195                         throw new \Exception('Error loading config file ' . $filepath);
196                 }
197
198                 return $config;
199         }
200 }