3 namespace Friendica\Core\Config;
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 // Setting at least the basepath we know
41 $config->set('system', 'basepath', $this->baseDir);
43 $config->loadConfigArray($this->loadCoreConfig('defaults'));
44 $config->loadConfigArray($this->loadCoreConfig('settings'));
46 $config->loadConfigArray($this->loadLegacyConfigFile('htpreconfig'), true);
47 $config->loadConfigArray($this->loadLegacyConfigFile('htconfig'), true);
49 $config->loadConfigArray($this->loadCoreConfig('local'), true);
53 * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php)
55 * @param string $name The name of the config file
56 * @return array The configuration array
58 * @deprecated since version 2018.09
60 private function loadLegacyConfigFile($name)
62 $filePath = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
64 if (file_exists($filePath)) {
69 if (isset($db_host)) {
70 $a->config['database']['hostname'] = $db_host;
73 if (isset($db_user)) {
74 $a->config['database']['username'] = $db_user;
77 if (isset($db_pass)) {
78 $a->config['database']['password'] = $db_pass;
81 if (isset($db_data)) {
82 $a->config['database']['database'] = $db_data;
85 if (isset($a->config['system']['db_charset'])) {
86 $a->config['database']['charset'] = $a->config['system']['charset'];
88 if (isset($pidfile)) {
89 $a->config['system']['pidfile'] = $pidfile;
92 if (isset($default_timezone)) {
93 $a->config['system']['default_timezone'] = $default_timezone;
94 unset($default_timezone);
97 $a->config['system']['language'] = $lang;
108 * Tries to load the specified legacy configuration file and returns the config array.
110 * @deprecated since version 2018.12
111 * @param string $filename
113 * @return array The configuration array
116 public function loadINIConfigFile($filename)
118 $filepath = $this->configDir . DIRECTORY_SEPARATOR . $filename . ".ini.php";
120 if (!file_exists($filepath)) {
121 throw new \Exception('Error parsing non-existent INI config file ' . $filepath);
124 $contents = include($filepath);
126 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
128 if ($config === false) {
129 throw new \Exception('Error parsing INI config file ' . $filepath);
136 * Tries to load the specified configuration file and returns the config array.
138 * The config format is PHP array and the template for configuration files is the following:
146 * @param string $filepath The filepath of the
147 * @return array The config array0
149 * @throws \Exception if the config cannot get loaded.
151 private function loadConfigFile($filepath)
153 if (!file_exists($filepath)) {
154 throw new \Exception('Error loading non-existent config file ' . $filepath);
157 $config = include($filepath);
159 if (!is_array($config)) {
160 throw new \Exception('Error loading config file ' . $filepath);
167 * Tries to load the specified core-configuration and returns the config array.
169 * @param string $name The name of the configuration
171 * @return array The config array (empty if no config found)
173 * @throws \Exception if the configuration file isn't readable
175 public function loadCoreConfig($name)
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');
187 * Tries to load the specified addon-configuration and returns the config array.
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.
192 * @param string $name The name of the configuration
194 * @return array The config array (empty if no config found)
196 * @throws \Exception if the configuration file isn't readable
198 public function loadAddonConfig($name)
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
206 if (file_exists($filepath)) {
207 return $this->loadConfigFile($filepath);