]> git.mxchange.org Git - friendica.git/commitdiff
Introduce "static/env.config.php" for environment variable mapping to config cache...
authorPhilipp <admin@philipp.info>
Tue, 6 Oct 2020 18:06:52 +0000 (20:06 +0200)
committerPhilipp <admin@philipp.info>
Tue, 6 Oct 2020 18:43:29 +0000 (20:43 +0200)
- Added new database.port config value (used for MYSQL_PORT)
- Removed now obsolete db environment variable functionality
- Added functionality to load env variables (overwrites DB based cached)

src/Core/Config/Cache.php
src/Database/Database.php
src/Factory/ConfigFactory.php
src/Util/ConfigFileLoader.php
static/defaults.config.php
static/dependencies.config.php
static/env.config.php [new file with mode: 0644]
tests/src/Core/Config/CacheTest.php

index b78b57f57de1dffd5c4f44c775bf6a1500b0d68a..25b25550e02182c748e3f1d76809a2bd464e54e4 100644 (file)
@@ -36,6 +36,8 @@ class Cache
        const SOURCE_DB = 1;
        /** @var int Indicates that the cache entry is set by a server environment variable - High Priority */
        const SOURCE_ENV = 3;
+       /** @var int Indicates that the cache entry is fixed and must not be changed */
+       const SOURCE_FIX = 4;
 
        /** @var int Default value for a config source */
        const SOURCE_DEFAULT = self::SOURCE_FILE;
index e25323b382feed5b7e40a5d33f2040bcfdc7dcfa..727c2df93ccbcd4c1d2b6c80a0eb7b6d8f2ff61d 100644 (file)
@@ -66,7 +66,7 @@ class Database
        protected $testmode       = false;
        private $relation       = [];
 
-       public function __construct(Cache $configCache, Profiler $profiler, LoggerInterface $logger, array $server = [])
+       public function __construct(Cache $configCache, Profiler $profiler, LoggerInterface $logger)
        {
                // We are storing these values for being able to perform a reconnect
                $this->configCache   = $configCache;
index 954f893959229624e098ca2ac4ce09a6387d2110..3110490fd3280749592da3fc28adf41bbb4288ca 100644 (file)
@@ -37,10 +37,10 @@ class ConfigFactory
         *
         * @throws Exception
         */
-       public function createCache(ConfigFileLoader $loader)
+       public function createCache(ConfigFileLoader $loader, array $server = [])
        {
                $configCache = new Cache();
-               $loader->setupCache($configCache);
+               $loader->setupCache($configCache, $server);
 
                return $configCache;
        }
index 54c2ebe0c0fd6e4b3bb8a5bf89df4534eb09d1ac..60e82c04f1c1b3731d7023659a5dc3b6cda9a453 100644 (file)
@@ -97,11 +97,12 @@ class ConfigFileLoader
         * 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 = [], $raw = false)
        {
                // Load static config files first, the order is important
                $config->load($this->loadStaticConfig('defaults'), Cache::SOURCE_FILE);
@@ -114,10 +115,12 @@ class ConfigFileLoader
                // 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);
                }
        }
 
@@ -192,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
         *
index 11731f214894f18d937ed4e65f019cbfe5765ea3..18b70d3c152f6e000adeb4c28880589abcc5e873 100644 (file)
@@ -32,6 +32,11 @@ return [
                // Can contain the port number with the syntax "hostname:port".
                'hostname' => '',
 
+               // port (Integer)
+               // Port of the database server.
+               // Can be used instead of adding a port number to the hostname
+               'port' => null,
+
                // user (String)
                // Database user name. Please don't use "root".
                'username' => '',
index b1a54786ac10cd68a4549fe717ef271c155db80d..ca9b788534b5e593a66fa2491c1b31eb35a98967 100644 (file)
@@ -75,13 +75,13 @@ return [
        Util\ConfigFileLoader::class => [
                'shared'          => true,
                'constructParams' => [
-                       [Dice::INSTANCE => '$basepath'],
+                       [Dice::INSTANCE => '$basepath']
                ],
        ],
        Config\Cache::class          => [
                'instanceOf' => Factory\ConfigFactory::class,
                'call'       => [
-                       ['createCache', [], Dice::CHAIN_CALL],
+                       ['createCache', [$_SERVER], Dice::CHAIN_CALL],
                ],
        ],
        App\Mode::class              => [
@@ -105,7 +105,6 @@ return [
        Database::class                         => [
                'constructParams' => [
                        [Dice::INSTANCE => \Psr\Log\NullLogger::class],
-                       $_SERVER,
                ],
        ],
        /**
diff --git a/static/env.config.php b/static/env.config.php
new file mode 100644 (file)
index 0000000..e2b9d2b
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @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/>.
+ *
+ * Main mapping table of environment variables to correct config values
+ *
+ */
+
+return [
+       'MYSQL_HOST' => ['database', 'hostname'],
+       'MYSQL_USERNAME' => ['database', 'username'],
+       'MYSQL_USER' => ['database', 'username'],
+       'MYSQL_PORT' => ['database', 'port'],
+       'MYSQL_PASSWORD' => ['database', 'password'],
+       'MYSQL_DATABASE' => ['database', 'database'],
+];
index 02b98c7ec16f13960eaf8a96192be2ca2f220513..1acdd1f4fc98682cafb49f66b24f69f91ba6f776 100644 (file)
@@ -94,6 +94,19 @@ class CacheTest extends MockedTest
 
                $this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
                $this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
+
+               // Don't overwrite server ENV variables - even in load mode
+               $configCache->load($data, Cache::SOURCE_DB);
+
+               $this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
+               $this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
+
+               // Overwrite ENV variables with ENV variables
+               $configCache->load($data, Cache::SOURCE_ENV);
+
+               $this->assertConfigValues($data, $configCache);
+               $this->assertNotEquals($override['system']['test'], $configCache->get('system', 'test'));
+               $this->assertNotEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
        }
 
        /**