<?php
/**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
*/
class ConfigFileLoader
{
- /**
- * The Sub directory of the config-files
- *
- * @var string
- */
- const CONFIG_DIR = 'config';
-
- /**
- * The Sub directory of the static config-files
- *
- * @var string
- */
- const STATIC_DIR = 'static';
-
/**
* The default name of the user defined ini file
*
*/
private $staticDir;
- public function __construct(string $basePath)
+ /**
+ * @param string $baseDir The base
+ * @param string $configDir
+ * @param string $staticDir
+ */
+ public function __construct(string $baseDir, string $configDir, string $staticDir)
{
- $this->baseDir = $basePath;
- $this->configDir = $this->baseDir . DIRECTORY_SEPARATOR . self::CONFIG_DIR;
- $this->staticDir = $this->baseDir . DIRECTORY_SEPARATOR . self::STATIC_DIR;
+ $this->baseDir = $baseDir;
+ $this->configDir = $configDir;
+ $this->staticDir = $staticDir;
}
/**
* expected local.config.php
*
* @param Cache $config The config cache to load to
+ * @param array $server The $_SERVER array
* @param bool $raw Setup the raw config format
*
* @throws Exception
*/
- public function setupCache(Cache $config, $raw = false)
+ public function setupCache(Cache $config, array $server = [], bool $raw = false)
{
// Load static config files first, the order is important
$config->load($this->loadStaticConfig('defaults'), Cache::SOURCE_FILE);
// Now load every other config you find inside the 'config/' directory
$this->loadCoreConfig($config);
+ $config->load($this->loadEnvConfig($server), Cache::SOURCE_ENV);
+
// In case of install mode, add the found basepath (because there isn't a basepath set yet
if (!$raw && empty($config->get('system', 'basepath'))) {
// Setting at least the basepath we know
- $config->set('system', 'basepath', $this->baseDir);
+ $config->set('system', 'basepath', $this->baseDir, Cache::SOURCE_FILE);
}
}
$filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
$name . DIRECTORY_SEPARATOR . // openstreetmap/
- self::CONFIG_DIR . DIRECTORY_SEPARATOR . // config/
+ 'config'. DIRECTORY_SEPARATOR . // config/
$name . ".config.php"; // openstreetmap.config.php
if (file_exists($filepath)) {
}
}
+ /**
+ * Tries to load environment specific variables, based on the `env.config.php` mapping table
+ *
+ * @param array $server The $_SERVER variable
+ *
+ * @return array The config array (empty if no config was found)
+ *
+ * @throws Exception if the configuration file isn't readable
+ */
+ public function loadEnvConfig(array $server)
+ {
+ $filepath = $this->staticDir . DIRECTORY_SEPARATOR . // /var/www/html/static/
+ "env.config.php"; // env.config.php
+
+ if (!file_exists($filepath)) {
+ return [];
+ }
+
+ $envConfig = $this->loadConfigFile($filepath);
+
+ $return = [];
+
+ foreach ($envConfig as $envKey => $configStructure) {
+ if (isset($server[$envKey])) {
+ $return[$configStructure[0]][$configStructure[1]] = $server[$envKey];
+ }
+ }
+
+ return $return;
+ }
+
/**
* Get the config files of the config-directory
*