friendica/src/Core/Config/Cache/ConfigCacheLoader.php

215 lines
5.6 KiB
PHP
Raw Normal View History

2019-02-03 16:22:04 -05:00
<?php
namespace Friendica\Core\Config\Cache;
2019-02-03 16:22:04 -05:00
2019-02-04 03:33:55 -05:00
use Friendica\Core\Addon;
2019-02-03 17:39:30 -05:00
/**
* The ConfigCacheLoader loads config-files and stores them in a ConfigCache ( @see ConfigCache )
*
* It is capable of loading the following config files:
* - *.config.php (current)
* - *.ini.php (deprecated)
* - *.htconfig.php (deprecated)
*/
2019-02-03 16:22:04 -05:00
class ConfigCacheLoader
{
2019-02-03 17:39:30 -05:00
/**
* The Sub directory of the config-files
* @var string
*/
2019-02-05 17:36:01 -05:00
const SUBDIRECTORY = 'config';
2019-02-03 17:39:30 -05:00
2019-02-03 16:22:04 -05:00
private $baseDir;
private $configDir;
public function __construct($baseDir)
{
$this->baseDir = $baseDir;
2019-02-05 17:36:01 -05:00
$this->configDir = $baseDir . DIRECTORY_SEPARATOR . self::SUBDIRECTORY;
2019-02-03 16:22:04 -05:00
}
/**
* Load the configuration files
*
* First loads the default value for all the configuration keys, then the legacy configuration files, then the
* expected local.config.php
*/
public function loadConfigFiles(ConfigCache $config)
{
$config->load($this->loadCoreConfig('defaults'));
$config->load($this->loadCoreConfig('settings'));
2019-02-03 16:22:04 -05:00
$config->load($this->loadLegacyConfig('htpreconfig'), true);
$config->load($this->loadLegacyConfig('htconfig'), true);
2019-02-03 16:22:04 -05:00
$config->load($this->loadCoreConfig('local'), true);
2019-02-05 17:36:01 -05:00
}
2019-02-03 16:22:04 -05:00
2019-02-05 17:36:01 -05:00
/**
2019-02-05 18:02:25 -05:00
* Tries to load the specified core-configuration and returns the config array.
*
* @param string $name The name of the configuration
*
* @return array The config array (empty if no config found)
*
* @throws \Exception if the configuration file isn't readable
*/
public function loadCoreConfig($name)
{
if (file_exists($this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php')) {
return $this->loadConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php');
} elseif (file_exists($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php')) {
return $this->loadINIConfigFile($this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php');
} else {
return [];
}
}
/**
* Tries to load the specified addon-configuration and returns the config array.
*
* @param string $name The name of the configuration
*
* @return array The config array (empty if no config found)
*
* @throws \Exception if the configuration file isn't readable
*/
public function loadAddonConfig($name)
{
$filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
$name . DIRECTORY_SEPARATOR . // openstreetmap/
self::SUBDIRECTORY . DIRECTORY_SEPARATOR . // config/
$name . ".config.php"; // openstreetmap.config.php
if (file_exists($filepath)) {
return $this->loadConfigFile($filepath);
} else {
return [];
}
}
/**
* Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
2019-02-05 17:36:01 -05:00
*
* @param string $name The name of the config file
2019-02-05 18:02:25 -05:00
*
* @return array The configuration array (empty if no config found)
2019-02-05 17:36:01 -05:00
*
* @deprecated since version 2018.09
*/
2019-02-05 18:02:25 -05:00
private function loadLegacyConfig($name)
2019-02-05 17:36:01 -05:00
{
$filePath = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
2019-02-03 16:22:04 -05:00
$config = [];
2019-02-05 17:36:01 -05:00
if (file_exists($filePath)) {
2019-03-16 15:31:10 -04:00
$a = new \stdClass();
$a->config = [];
2019-02-05 17:36:01 -05:00
include $filePath;
2019-02-03 16:22:04 -05:00
2019-03-16 15:31:10 -04:00
$htconfigAr = array_keys($a->config);
// map the legacy configuration structure to the current structure
foreach ($htconfigAr as $htconfig) {
if (isset($a->config[$htconfig]) && is_array($a->config[$htconfig])) {
$keys = array_keys($a->config[$htconfig]);
foreach ($keys as $key) {
$config[$htconfig][$key] = $a->config[$htconfig][$key];
}
} else {
$config['config'][$htconfig] = $a->config[$htconfig];
}
}
unset($a);
2019-02-05 17:36:01 -05:00
if (isset($db_host)) {
$config['database']['hostname'] = $db_host;
2019-02-05 17:36:01 -05:00
unset($db_host);
}
if (isset($db_user)) {
$config['database']['username'] = $db_user;
2019-02-05 17:36:01 -05:00
unset($db_user);
}
if (isset($db_pass)) {
$config['database']['password'] = $db_pass;
2019-02-05 17:36:01 -05:00
unset($db_pass);
}
if (isset($db_data)) {
$config['database']['database'] = $db_data;
2019-02-05 17:36:01 -05:00
unset($db_data);
}
2019-03-16 15:31:10 -04:00
if (isset($config['system']['db_charset'])) {
$config['database']['charset'] = $config['system']['db_charset'];
2019-02-03 16:22:04 -05:00
}
if (isset($pidfile)) {
$config['system']['pidfile'] = $pidfile;
2019-02-03 16:22:04 -05:00
unset($pidfile);
}
2019-02-05 17:36:01 -05:00
if (isset($default_timezone)) {
$config['system']['default_timezone'] = $default_timezone;
2019-02-05 17:36:01 -05:00
unset($default_timezone);
}
2019-02-03 16:22:04 -05:00
if (isset($lang)) {
$config['system']['language'] = $lang;
2019-02-03 16:22:04 -05:00
unset($lang);
}
2019-02-05 17:36:01 -05:00
}
return $config;
2019-02-03 16:22:04 -05:00
}
/**
2019-02-05 17:36:01 -05:00
* Tries to load the specified legacy configuration file and returns the config array.
2019-02-03 16:22:04 -05:00
*
* @deprecated since version 2018.12
2019-02-05 17:47:40 -05:00
* @param string $filepath
2019-02-03 16:22:04 -05:00
*
2019-02-05 17:36:01 -05:00
* @return array The configuration array
2019-02-03 16:22:04 -05:00
* @throws \Exception
*/
2019-02-05 18:02:25 -05:00
private function loadINIConfigFile($filepath)
2019-02-03 16:22:04 -05:00
{
$contents = include($filepath);
$config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
if ($config === false) {
throw new \Exception('Error parsing INI config file ' . $filepath);
}
return $config;
}
/**
2019-02-05 16:27:57 -05:00
* Tries to load the specified configuration file and returns the config array.
2019-02-03 16:22:04 -05:00
*
* The config format is PHP array and the template for configuration files is the following:
*
* <?php return [
* 'section' => [
* 'key' => 'value',
* ],
* ];
*
2019-02-05 16:27:57 -05:00
* @param string $filepath The filepath of the
* @return array The config array0
*
* @throws \Exception if the config cannot get loaded.
2019-02-03 16:22:04 -05:00
*/
2019-02-05 16:27:57 -05:00
private function loadConfigFile($filepath)
2019-02-03 16:22:04 -05:00
{
$config = include($filepath);
if (!is_array($config)) {
throw new \Exception('Error loading config file ' . $filepath);
}
return $config;
}
}