]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/ConfigFileLoader.php
Use HTTPRequestOptions constants for HTTPClient::get()
[friendica.git] / src / Util / ConfigFileLoader.php
index 8ca76f79c1c370c8f69e3e2167db9d29900a031f..0c416a189f8fe51ce2527f822e23b7f17d71fab4 100644 (file)
@@ -1,14 +1,32 @@
 <?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Util;
 
 use Exception;
-use Friendica\App;
 use Friendica\Core\Addon;
-use Friendica\Core\Config\Cache\ConfigCache;
+use Friendica\Core\Config\Cache;
 
 /**
- * The ConfigFileLoader loads config-files and stores them in a ConfigCache ( @see ConfigCache )
+ * The ConfigFileLoader loads config-files and stores them in a ConfigCache ( @see Cache )
  *
  * It is capable of loading the following config files:
  * - *.config.php   (current)
@@ -52,10 +70,6 @@ class ConfigFileLoader
         */
        const SAMPLE_END = '-sample';
 
-       /**
-        * @var App\Mode
-        */
-       private $appMode;
        /**
         * @var string
         */
@@ -69,12 +83,11 @@ class ConfigFileLoader
         */
        private $staticDir;
 
-       public function __construct($baseDir, App\Mode $mode)
+       public function __construct(string $basePath)
        {
-               $this->baseDir   = $baseDir;
-               $this->configDir = $baseDir . DIRECTORY_SEPARATOR . self::CONFIG_DIR;
-               $this->staticDir = $baseDir . DIRECTORY_SEPARATOR . self::STATIC_DIR;
-               $this->appMode   = $mode;
+               $this->baseDir   = $basePath;
+               $this->configDir = $this->baseDir . DIRECTORY_SEPARATOR . self::CONFIG_DIR;
+               $this->staticDir = $this->baseDir . DIRECTORY_SEPARATOR . self::STATIC_DIR;
        }
 
        /**
@@ -83,28 +96,31 @@ class ConfigFileLoader
         * First loads the default value for all the configuration keys, then the legacy configuration files, then the
         * expected local.config.php
         *
-        * @param ConfigCache $config The config cache to load to
-        * @param bool        $raw    Setup the raw config format
+        * @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(ConfigCache $config, $raw = false)
+       public function setupCache(Cache $config, array $server = [], $raw = false)
        {
                // Load static config files first, the order is important
-               $config->load($this->loadStaticConfig('defaults'));
-               $config->load($this->loadStaticConfig('settings'));
+               $config->load($this->loadStaticConfig('defaults'), Cache::SOURCE_FILE);
+               $config->load($this->loadStaticConfig('settings'), Cache::SOURCE_FILE);
 
                // try to load the legacy config first
-               $config->load($this->loadLegacyConfig('htpreconfig'), true);
-               $config->load($this->loadLegacyConfig('htconfig'), true);
+               $config->load($this->loadLegacyConfig('htpreconfig'), Cache::SOURCE_FILE);
+               $config->load($this->loadLegacyConfig('htconfig'), 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 && ($this->appMode->isInstall() || empty($config->get('system', 'basepath')))) {
+               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);
                }
        }
 
@@ -134,22 +150,22 @@ class ConfigFileLoader
        /**
         * Tries to load the specified core-configuration into the config cache.
         *
-        * @param ConfigCache $config The Config cache
+        * @param Cache $config The Config cache
         *
         * @return array The config array (empty if no config found)
         *
         * @throws Exception if the configuration file isn't readable
         */
-       private function loadCoreConfig(ConfigCache $config)
+       private function loadCoreConfig(Cache $config)
        {
                // try to load legacy ini-files first
                foreach ($this->getConfigFiles(true) as $configFile) {
-                       $config->load($this->loadINIConfigFile($configFile), true);
+                       $config->load($this->loadINIConfigFile($configFile), Cache::SOURCE_FILE);
                }
 
                // try to load supported config at last to overwrite it
                foreach ($this->getConfigFiles() as $configFile) {
-                       $config->load($this->loadConfigFile($configFile), true);
+                       $config->load($this->loadConfigFile($configFile), Cache::SOURCE_FILE);
                }
 
                return [];
@@ -179,6 +195,38 @@ class ConfigFileLoader
                }
        }
 
+       /**
+        * 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->baseDir . DIRECTORY_SEPARATOR .   // /var/www/html/
+                                       self::STATIC_DIR . DIRECTORY_SEPARATOR . // 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
         *