]> git.mxchange.org Git - friendica.git/commitdiff
Move path determination logic into the `ConfigFactory`
authorPhilipp <admin@philipp.info>
Sun, 12 Sep 2021 11:44:29 +0000 (13:44 +0200)
committerPhilipp <admin@philipp.info>
Sun, 12 Sep 2021 11:44:29 +0000 (13:44 +0200)
src/App.php
src/Factory/ConfigFactory.php
src/Module/Admin/Summary.php
src/Util/ConfigFileLoader.php
static/dependencies.config.php
tests/src/Core/Cache/DatabaseCacheTest.php
tests/src/Core/Lock/DatabaseLockDriverTest.php
tests/src/Model/ProcessTest.php
tests/src/Model/Storage/DatabaseStorageTest.php
tests/src/Util/Config/ConfigFileLoaderTest.php

index 67b2592bd3e841b340724e4cf6a5ddd562c743eb..7c7496c3baf138a0b47d66b9241d2653d39354eb 100644 (file)
@@ -25,6 +25,7 @@ use Exception;
 use Friendica\App\Arguments;
 use Friendica\App\BaseURL;
 use Friendica\App\Module;
+use Friendica\Factory\ConfigFactory;
 use Friendica\Module\Maintenance;
 use Friendica\Security\Authentication;
 use Friendica\Core\Config\Cache;
@@ -352,7 +353,7 @@ class App
                        $this->profiler->update($this->config);
 
                        Core\Hook::loadHooks();
-                       $loader = new ConfigFileLoader($this->getBasePath(), $_SERVER);
+                       $loader = (new ConfigFactory())->createConfigFileLoader($this->getBasePath(), $_SERVER);
                        Core\Hook::callAll('load_config', $loader);
                }
 
index 9a20100b2fe6da880d4877171c88c1c18a25b38a..cf55640960f0c02f19191f8be52257c14329a048 100644 (file)
@@ -30,6 +30,45 @@ use Friendica\Util\ConfigFileLoader;
 
 class ConfigFactory
 {
+       /**
+        * The key of the $_SERVER variable to override the config directory
+        *
+        * @var string
+        */
+       const CONFIG_DIR_ENV = 'FRIENDICA_CONFIG_DIR';
+
+       /**
+        * 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';
+
+       /**
+        * @param string $basePath The basepath of FRIENDICA
+        * @param array $serer the $_SERVER array
+        *
+        * @return ConfigFileLoader
+        */
+       public function createConfigFileLoader(string $basePath, array $server = [])
+       {
+               if (!empty($server[self::CONFIG_DIR_ENV]) && is_dir($server[self::CONFIG_DIR_ENV])) {
+                       $configDir = $server[self::CONFIG_DIR_ENV];
+               } else {
+                       $configDir = $basePath . DIRECTORY_SEPARATOR . self::CONFIG_DIR;
+               }
+               $staticDir = $basePath . DIRECTORY_SEPARATOR . self::STATIC_DIR;
+
+               return new ConfigFileLoader($basePath, $configDir, $staticDir);
+       }
+
        /**
         * @param ConfigFileLoader $loader The Config Cache loader (INI/config/.htconfig)
         *
index 9bd902b35429d5f5c11f4fbd03f86bc4f39667d3..2d081f407752b0c9ef0fa8f83fe92526ad252679 100644 (file)
@@ -29,6 +29,7 @@ use Friendica\Core\Update;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
 use Friendica\DI;
+use Friendica\Factory\ConfigFactory;
 use Friendica\Model\Register;
 use Friendica\Module\BaseAdmin;
 use Friendica\Network\HTTPException\InternalServerErrorException;
@@ -151,7 +152,7 @@ class Summary extends BaseAdmin
                }
 
                // check legacy basepath settings
-               $configLoader = new ConfigFileLoader($a->getBasePath(), $_SERVER);
+               $configLoader = (new ConfigFactory())->createConfigFileLoader($a->getBasePath(), $_SERVER);
                $configCache = new Cache();
                $configLoader->setupCache($configCache);
                $confBasepath = $configCache->get('system', 'basepath');
index f2aa9124fc1da2de8613d0927a688f1f290a07a1..aabefc18bba112f0dcfae08ed775c5b570225a19 100644 (file)
@@ -35,27 +35,6 @@ use Friendica\Core\Config\Cache;
  */
 class ConfigFileLoader
 {
-       /**
-        * The key of the $_SERVER variable to override the config directory
-        *
-        * @var string
-        */
-       const CONFIG_DIR_ENV = 'FRIENDICA_CONFIG_DIR';
-
-       /**
-        * 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
         *
@@ -90,15 +69,16 @@ class ConfigFileLoader
         */
        private $staticDir;
 
-       public function __construct(string $basePath, array $server)
+       /**
+        * @param string $baseDir   The base
+        * @param string $configDir
+        * @param string $staticDir
+        */
+       public function __construct(string $baseDir, string $configDir, string $staticDir)
        {
-               $this->baseDir = $basePath;
-               if (!empty($server[self::CONFIG_DIR_ENV]) && is_dir($server[self::CONFIG_DIR_ENV])) {
-                       $this->configDir = $server[self::CONFIG_DIR_ENV];
-               } else {
-                       $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;
        }
 
        /**
@@ -113,7 +93,7 @@ class ConfigFileLoader
         *
         * @throws Exception
         */
-       public function setupCache(Cache $config, array $server = [], $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);
@@ -196,7 +176,7 @@ class ConfigFileLoader
                $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)) {
@@ -217,9 +197,8 @@ class ConfigFileLoader
         */
        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
+               $filepath = $this->staticDir . DIRECTORY_SEPARATOR .   // /var/www/html/static/
+                                       "env.config.php";                          // env.config.php
 
                if (!file_exists($filepath)) {
                        return [];
index 79b7f13648c7d0722c867868eea6b2feb95d5c38..bf7b412c2ff4c198eb1157a117e7ae305afc5bf5 100644 (file)
@@ -74,10 +74,12 @@ return [
                ]
        ],
        Util\ConfigFileLoader::class => [
-               'shared'          => true,
-               'constructParams' => [
-                       [Dice::INSTANCE => '$basepath'],
-                       $_SERVER,
+               'instanceOf' => Factory\ConfigFactory::class,
+               'call'       => [
+                       ['createConfigFileLoader', [
+                               [Dice::INSTANCE => '$basepath'],
+                               $_SERVER,
+                       ], Dice::CHAIN_CALL],
                ],
        ],
        Config\Cache::class          => [
index a89714a8538258b2e33c04ebb25953b20cf0f40a..37859ef785ab3228a5c180f1ab0035ccc7c1f94b 100644 (file)
@@ -55,7 +55,7 @@ class DatabaseCacheTest extends CacheTest
 
                // load real config to avoid mocking every config-entry which is related to the Database class
                $configFactory = new ConfigFactory();
-               $loader = new ConfigFileLoader($this->root->url(), []);
+               $loader = (new ConfigFactory())->createConfigFileLoader($this->root->url(), []);
                $configCache = $configFactory->createCache($loader);
 
                $dba = new StaticDatabase($configCache, $profiler, $logger);
index 9e995c123e616583b390d9f7ec70d118c88e4eb3..c51d153efce13da26fdab419995b02449f07fba6 100644 (file)
@@ -57,7 +57,7 @@ class DatabaseLockDriverTest extends LockTest
 
                // load real config to avoid mocking every config-entry which is related to the Database class
                $configFactory = new ConfigFactory();
-               $loader        = new ConfigFileLoader($this->root->url(), []);
+               $loader        = (new ConfigFactory())->createConfigFileLoader($this->root->url(), []);
                $configCache   = $configFactory->createCache($loader);
 
                $dba = new StaticDatabase($configCache, $profiler, $logger);
index 66a02eced3e445f08004cd352bac0551eac9942f..a4739d7f5b1495652bcb6f6124f6af8dce764873 100644 (file)
@@ -33,7 +33,7 @@ class ProcessTest extends DatabaseTest
 
                // load real config to avoid mocking every config-entry which is related to the Database class
                $configFactory = new ConfigFactory();
-               $loader        = new ConfigFileLoader($this->root->url(), []);
+               $loader        = (new ConfigFactory())->createConfigFileLoader($this->root->url(), []);
                $configCache   = $configFactory->createCache($loader);
 
                $this->dba = new StaticDatabase($configCache, $profiler, $logger);
index 8fd8c8c42c8ee5678258aceccd51cb150c7c525d..aa25d1cc7f994360bebe4e49ae9fb4501fae1617 100644 (file)
@@ -55,7 +55,7 @@ class DatabaseStorageTest extends StorageTest
 
                // load real config to avoid mocking every config-entry which is related to the Database class
                $configFactory = new ConfigFactory();
-               $loader = new ConfigFileLoader($this->root->url(), []);
+               $loader = (new ConfigFactory())->createConfigFileLoader($this->root->url(), []);
                $configCache = $configFactory->createCache($loader);
 
                $dba = new StaticDatabase($configCache, $profiler, $logger);
index c69d94679d2bfbf2f95eadff6a587769aa914d95..cb3ae840cee9b5c0eb956777db3225e19db53ef2 100644 (file)
@@ -22,6 +22,7 @@
 namespace Friendica\Test\src\Util\Config;
 
 use Friendica\Core\Config\Cache;
+use Friendica\Factory\ConfigFactory;
 use Friendica\Test\MockedTest;
 use Friendica\Test\Util\VFSTrait;
 use Friendica\Util\ConfigFileLoader;
@@ -45,7 +46,11 @@ class ConfigFileLoaderTest extends MockedTest
        {
                $this->delConfigFile('local.config.php');
 
-               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
+               $configFileLoader = new ConfigFileLoader(
+                       $this->root->url(),
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
+               );
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -67,7 +72,11 @@ class ConfigFileLoaderTest extends MockedTest
                        ->at($this->root->getChild('config'))
                        ->setContent('<?php return true;');
 
-               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
+               $configFileLoader = new ConfigFileLoader(
+                       $this->root->url(),
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
+               );
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -91,7 +100,11 @@ class ConfigFileLoaderTest extends MockedTest
                        ->at($this->root->getChild('config'))
                        ->setContent(file_get_contents($file));
 
-               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
+               $configFileLoader = new ConfigFileLoader(
+                       $this->root->url(),
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
+               );
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -123,7 +136,11 @@ class ConfigFileLoaderTest extends MockedTest
                        ->at($this->root->getChild('config'))
                        ->setContent(file_get_contents($file));
 
-               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
+               $configFileLoader = new ConfigFileLoader(
+                       $this->root->url(),
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
+               );
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -154,7 +171,11 @@ class ConfigFileLoaderTest extends MockedTest
                        ->at($this->root)
                        ->setContent(file_get_contents($file));
 
-               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
+               $configFileLoader = new ConfigFileLoader(
+                       $this->root->url(),
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
+               );
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -203,7 +224,11 @@ class ConfigFileLoaderTest extends MockedTest
                        ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
                        ->setContent(file_get_contents($file));
 
-               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
+               $configFileLoader = new ConfigFileLoader(
+                       $this->root->url(),
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
+               );
 
                $conf = $configFileLoader->loadAddonConfig('test');
 
@@ -235,7 +260,11 @@ class ConfigFileLoaderTest extends MockedTest
                                ->at($this->root->getChild('config'))
                         ->setContent(file_get_contents($fileDir . 'B.config.php'));
 
-               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
+               $configFileLoader = new ConfigFileLoader(
+                       $this->root->url(),
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
+               );
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -264,7 +293,11 @@ class ConfigFileLoaderTest extends MockedTest
                         ->at($this->root->getChild('config'))
                         ->setContent(file_get_contents($fileDir . 'B.ini.php'));
 
-               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
+               $configFileLoader = new ConfigFileLoader(
+                       $this->root->url(),
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
+               );
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -293,7 +326,11 @@ class ConfigFileLoaderTest extends MockedTest
                         ->at($this->root->getChild('config'))
                         ->setContent(file_get_contents($fileDir . 'B.ini.php'));
 
-               $configFileLoader = new ConfigFileLoader($this->root->url(), []);
+               $configFileLoader = new ConfigFileLoader(
+                       $this->root->url(),
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::CONFIG_DIR,
+                       $this->root->url() . DIRECTORY_SEPARATOR . ConfigFactory::STATIC_DIR
+               );
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -309,7 +346,7 @@ class ConfigFileLoaderTest extends MockedTest
        {
                $this->delConfigFile('local.config.php');
 
-               $configFileLoader = new ConfigFileLoader($this->root->url(), ['FRIENDICA_CONFIG_DIR' => '/a/wrong/dir/']);
+               $configFileLoader = (new ConfigFactory())->createConfigFileLoader($this->root->url(), ['FRIENDICA_CONFIG_DIR' => '/a/wrong/dir/']);
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);
@@ -334,7 +371,7 @@ class ConfigFileLoaderTest extends MockedTest
                                 ->at($this->root->getChild('config2'))
                                 ->setContent(file_get_contents($fileDir . 'B.config.php'));
 
-               $configFileLoader = new ConfigFileLoader($this->root->url(), ['FRIENDICA_CONFIG_DIR' => $this->root->getChild('config2')->url()]);
+               $configFileLoader = (new ConfigFactory())->createConfigFileLoader($this->root->url(), ['FRIENDICA_CONFIG_DIR' => $this->root->getChild('config2')->url()]);
                $configCache = new Cache();
 
                $configFileLoader->setupCache($configCache);