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