3 namespace Friendica\Core\Config\Cache;
5 use Friendica\Core\Addon;
8 * The ConfigCacheLoader loads config-files and stores them in a ConfigCache ( @see ConfigCache )
10 * It is capable of loading the following config files:
11 * - *.config.php (current)
12 * - *.ini.php (deprecated)
13 * - *.htconfig.php (deprecated)
15 class ConfigCacheLoader
18 * The Sub directory of the config-files
21 const SUBDIRECTORY = 'config';
26 public function __construct($baseDir)
28 $this->baseDir = $baseDir;
29 $this->configDir = $baseDir . DIRECTORY_SEPARATOR . self::SUBDIRECTORY;
33 * Load the configuration files
35 * First loads the default value for all the configuration keys, then the legacy configuration files, then the
36 * expected local.config.php
38 public function loadConfigFiles(ConfigCache $config)
40 $config->load($this->loadCoreConfig('defaults'));
41 $config->load($this->loadCoreConfig('settings'));
43 $config->load($this->loadLegacyConfig('htpreconfig'), true);
44 $config->load($this->loadLegacyConfig('htconfig'), true);
46 $config->load($this->loadCoreConfig('local'), true);
50 * Tries to load the specified core-configuration and returns the config array.
52 * @param string $name The name of the configuration
54 * @return array The config array (empty if no config found)
56 * @throws \Exception if the configuration file isn't readable
58 public function loadCoreConfig($name)
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');
70 * Tries to load the specified addon-configuration and returns the config array.
72 * @param string $name The name of the configuration
74 * @return array The config array (empty if no config found)
76 * @throws \Exception if the configuration file isn't readable
78 public function loadAddonConfig($name)
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
86 if (file_exists($filepath)) {
87 return $this->loadConfigFile($filepath);
94 * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
96 * @param string $name The name of the config file
98 * @return array The configuration array (empty if no config found)
100 * @deprecated since version 2018.09
102 private function loadLegacyConfig($name)
104 $filePath = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
108 if (file_exists($filePath)) {
111 if (isset($db_host)) {
112 $config['database']['hostname'] = $db_host;
115 if (isset($db_user)) {
116 $config['database']['username'] = $db_user;
119 if (isset($db_pass)) {
120 $config['database']['password'] = $db_pass;
123 if (isset($db_data)) {
124 $config['database']['database'] = $db_data;
127 if (isset($a->config['system']['db_charset'])) {
128 $a->config['database']['charset'] = $config['system']['charset'];
130 if (isset($pidfile)) {
131 $config['system']['pidfile'] = $pidfile;
134 if (isset($default_timezone)) {
135 $config['system']['default_timezone'] = $default_timezone;
136 unset($default_timezone);
139 $config['system']['language'] = $lang;
142 if (isset($admin_email)) {
143 $config['config']['admin_email'] = $admin_email;
146 if (isset($admin_nickname)) {
147 $config['config']['admin_nickname'] = $admin_nickname;
148 unset($admin_nickname);
150 if (isset($php_path)) {
151 $config['config']['php_path'] = $php_path;
154 if (isset($max_import_size)) {
155 $config['config']['max_import_size'] = $max_import_size;
156 unset($max_import_size);
164 * Tries to load the specified legacy configuration file and returns the config array.
166 * @deprecated since version 2018.12
167 * @param string $filepath
169 * @return array The configuration array
172 private function loadINIConfigFile($filepath)
174 $contents = include($filepath);
176 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
178 if ($config === false) {
179 throw new \Exception('Error parsing INI config file ' . $filepath);
186 * Tries to load the specified configuration file and returns the config array.
188 * The config format is PHP array and the template for configuration files is the following:
196 * @param string $filepath The filepath of the
197 * @return array The config array0
199 * @throws \Exception if the config cannot get loaded.
201 private function loadConfigFile($filepath)
203 $config = include($filepath);
205 if (!is_array($config)) {
206 throw new \Exception('Error loading config file ' . $filepath);