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';
106 if (file_exists($filePath)) {
107 $a = new \stdClass();
111 if (isset($db_host)) {
112 $a->config['database']['hostname'] = $db_host;
115 if (isset($db_user)) {
116 $a->config['database']['username'] = $db_user;
119 if (isset($db_pass)) {
120 $a->config['database']['password'] = $db_pass;
123 if (isset($db_data)) {
124 $a->config['database']['database'] = $db_data;
127 if (isset($a->config['system']['db_charset'])) {
128 $a->config['database']['charset'] = $a->config['system']['charset'];
130 if (isset($pidfile)) {
131 $a->config['system']['pidfile'] = $pidfile;
134 if (isset($default_timezone)) {
135 $a->config['system']['default_timezone'] = $default_timezone;
136 unset($default_timezone);
139 $a->config['system']['language'] = $lang;
150 * Tries to load the specified legacy configuration file and returns the config array.
152 * @deprecated since version 2018.12
153 * @param string $filepath
155 * @return array The configuration array
158 private function loadINIConfigFile($filepath)
160 $contents = include($filepath);
162 $config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
164 if ($config === false) {
165 throw new \Exception('Error parsing INI config file ' . $filepath);
172 * Tries to load the specified configuration file and returns the config array.
174 * The config format is PHP array and the template for configuration files is the following:
182 * @param string $filepath The filepath of the
183 * @return array The config array0
185 * @throws \Exception if the config cannot get loaded.
187 private function loadConfigFile($filepath)
189 $config = include($filepath);
191 if (!is_array($config)) {
192 throw new \Exception('Error loading config file ' . $filepath);