Browse Source

Refactoring ConfigCacheLoader methods

tags/2019.03^2
Philipp Holzer 5 months ago
parent
commit
6a9d73f7d9
No account linked to committer's email address

+ 1
- 1
bin/auth_ejabberd.php View File

@@ -54,7 +54,7 @@ chdir($directory);
54 54
 
55 55
 require dirname(__DIR__) . '/vendor/autoload.php';
56 56
 
57
-$basedir = BasePath::create(dirname(__DIR__));
57
+$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
58 58
 $configLoader = new Config\ConfigCacheLoader($basedir);
59 59
 $config = Factory\ConfigFactory::createCache($configLoader);
60 60
 $logger = Factory\LoggerFactory::create('auth_ejabberd', $config);

+ 1
- 1
bin/console.php View File

@@ -7,7 +7,7 @@ use Friendica\Core\Config;
7 7
 use Friendica\Factory;
8 8
 use Friendica\Util\BasePath;
9 9
 
10
-$basedir = BasePath::create(dirname(__DIR__));
10
+$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
11 11
 $configLoader = new Config\ConfigCacheLoader($basedir);
12 12
 $config = Factory\ConfigFactory::createCache($configLoader);
13 13
 $logger = Factory\LoggerFactory::create('console', $config);

+ 1
- 1
bin/daemon.php View File

@@ -33,7 +33,7 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
33 33
 
34 34
 require dirname(__DIR__) . '/vendor/autoload.php';
35 35
 
36
-$basedir = BasePath::create(dirname(__DIR__));
36
+$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
37 37
 $configLoader = new Config\ConfigCacheLoader($basedir);
38 38
 $config = Factory\ConfigFactory::createCache($configLoader);
39 39
 $logger = Factory\LoggerFactory::create('daemon', $config);

+ 1
- 1
bin/worker.php View File

@@ -31,7 +31,7 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
31 31
 
32 32
 require dirname(__DIR__) . '/vendor/autoload.php';
33 33
 
34
-$basedir = BasePath::create(dirname(__DIR__));
34
+$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
35 35
 $configLoader = new Config\ConfigCacheLoader($basedir);
36 36
 $config = Factory\ConfigFactory::createCache($configLoader);
37 37
 $logger = Factory\LoggerFactory::create('worker', $config);

+ 1
- 1
index.php View File

@@ -15,7 +15,7 @@ if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
15 15
 
16 16
 require __DIR__ . '/vendor/autoload.php';
17 17
 
18
-$basedir = BasePath::create(__DIR__);
18
+$basedir = BasePath::create(__DIR__, $_SERVER);
19 19
 $configLoader = new Config\ConfigCacheLoader($basedir);
20 20
 $config = Factory\ConfigFactory::createCache($configLoader);
21 21
 $logger = Factory\LoggerFactory::create('index', $config);

+ 1
- 1
src/App.php View File

@@ -387,7 +387,7 @@ class App
387 387
 			Core\Hook::loadHooks();
388 388
 			$loader = new ConfigCacheLoader($this->basePath);
389 389
 			Core\Hook::callAll('load_config', $loader);
390
-			$this->config->loadConfigArray($loader->loadAddonConfig(), true);
390
+			$this->config->loadConfigArray($loader->loadCoreConfig('addon'), true);
391 391
 		}
392 392
 
393 393
 		$this->loadDefaultTimezone();

+ 2
- 12
src/Core/Config/ConfigCache.php View File

@@ -11,24 +11,14 @@ namespace Friendica\Core\Config;
11 11
  */
12 12
 class ConfigCache implements IConfigCache, IPConfigCache
13 13
 {
14
-	/**
15
-	 * NEVER, EVER use this public config array outside of the class
16
-	 * It is only public due to backward compatibility to .htconfig.php
17
-	 *
18
-	 * @var array The cached config array
19
-	 */
20
-	public $config;
14
+	private $config;
21 15
 
22 16
 	/**
23 17
 	 * @param array $config    A initial config array
24 18
 	 */
25 19
 	public function __construct(array $config = [])
26 20
 	{
27
-		$this->config = [];
28
-
29
-		if (isset($config)) {
30
-			$this->loadConfigArray($config, true);
31
-		}
21
+		$this->config = $config;
32 22
 	}
33 23
 
34 24
 	/**

+ 40
- 31
src/Core/Config/ConfigCacheLoader.php View File

@@ -40,8 +40,8 @@ class ConfigCacheLoader
40 40
 		// Setting at least the basepath we know
41 41
 		$config->set('system', 'basepath', $this->baseDir);
42 42
 
43
-		$config->loadConfigArray($this->loadConfigFile('defaults'));
44
-		$config->loadConfigArray($this->loadConfigFile('settings'));
43
+		$config->loadConfigArray($this->loadCoreConfig('defaults'));
44
+		$config->loadConfigArray($this->loadCoreConfig('settings'));
45 45
 
46 46
 		// Legacy .htconfig.php support
47 47
 		if (file_exists($this->baseDir  . '/.htpreconfig.php')) {
@@ -82,16 +82,11 @@ class ConfigCacheLoader
82 82
 			}
83 83
 		}
84 84
 
85
-		if (file_exists($this->baseDir . '/config/local.config.php')) {
86
-			$config->loadConfigArray($this->loadConfigFile('local'), true);
87
-		} elseif (file_exists($this->baseDir . '/config/local.ini.php')) {
88
-			$config->loadConfigArray($this->loadINIConfigFile('local'), true);
89
-		}
85
+		$config->loadConfigArray($this->loadCoreConfig('local'), true);
90 86
 	}
91 87
 
92 88
 	/**
93
-	 * Tries to load the specified legacy configuration file into the App->config array.
94
-	 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
89
+	 * Tries to load the specified legacy configuration file into the ConfigCache (@see ConfigCache ).
95 90
 	 *
96 91
 	 * @deprecated since version 2018.12
97 92
 	 * @param string $filename
@@ -119,8 +114,7 @@ class ConfigCacheLoader
119 114
 	}
120 115
 
121 116
 	/**
122
-	 * Tries to load the specified configuration file into the App->config array.
123
-	 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
117
+	 * Tries to load the specified configuration file and returns the config array.
124 118
 	 *
125 119
 	 * The config format is PHP array and the template for configuration files is the following:
126 120
 	 *
@@ -130,19 +124,13 @@ class ConfigCacheLoader
130 124
 	 *      ],
131 125
 	 * ];
132 126
 	 *
133
-	 * @param string $filename
134
-	 * @param bool   $addon     True, if a config for an addon should be loaded
135
-	 * @return array The configuration
136
-	 * @throws \Exception
127
+	 * @param  string $filepath The filepath of the
128
+	 * @return array The config array0
129
+	 *
130
+	 * @throws \Exception if the config cannot get loaded.
137 131
 	 */
138
-	public function loadConfigFile($filename, $addon = false)
132
+	private function loadConfigFile($filepath)
139 133
 	{
140
-		if ($addon) {
141
-			$filepath = $this->baseDir . Addon::DIRECTORY . $filename . self::SUBDIRECTORY . $filename . ".config.php";
142
-		} else {
143
-			$filepath = $this->configDir . $filename . ".config.php";
144
-		}
145
-
146 134
 		if (!file_exists($filepath)) {
147 135
 			throw new \Exception('Error loading non-existent config file ' . $filepath);
148 136
 		}
@@ -157,22 +145,43 @@ class ConfigCacheLoader
157 145
 	}
158 146
 
159 147
 	/**
160
-	 * Loads addons configuration files
148
+	 * Tries to load the specified core-configuration and returns the config array.
149
+	 *
150
+	 * @param string $name The name of the configuration
151
+	 *
152
+	 * @return array The config array (empty if no config found)
153
+	 *
154
+	 * @throws \Exception if the configuration file isn't readable
155
+	 */
156
+	public function loadCoreConfig($name)
157
+	{
158
+		if (file_exists($this->configDir . $name . '.config.php')) {
159
+			return $this->loadConfigFile($this->configDir . $name . '.config.php');
160
+		} elseif (file_exists($this->configDir . $name . '.ini.php')) {
161
+			return $this->loadINIConfigFile($this->configDir . $name . '.ini.php');
162
+		} else {
163
+			return [];
164
+		}
165
+	}
166
+
167
+	/**
168
+	 * Tries to load the specified addon-configuration and returns the config array.
161 169
 	 *
162 170
 	 * First loads all activated addons default configuration through the load_config hook, then load the local.config.php
163 171
 	 * again to overwrite potential local addon configuration.
164 172
 	 *
165
-	 * @return array The config array
173
+	 * @param string $name The name of the configuration
166 174
 	 *
167
-	 * @throws \Exception
175
+	 * @return array The config array (empty if no config found)
176
+	 *
177
+	 * @throws \Exception if the configuration file isn't readable
168 178
 	 */
169
-	public function loadAddonConfig()
179
+	public function loadAddonConfig($name)
170 180
 	{
171
-		// Load the local addon config file to overwritten default addon config values
172
-		if (file_exists($this->configDir . 'addon.config.php')) {
173
-			return $this->loadConfigFile('addon');
174
-		} elseif (file_exists($this->configDir . 'addon.ini.php')) {
175
-			return $this->loadINIConfigFile('addon');
181
+		$filepath = $this->baseDir . Addon::DIRECTORY . $name . self::SUBDIRECTORY . $name . ".config.php";
182
+
183
+		if (file_exists($filepath)) {
184
+			return $this->loadConfigFile($filepath);
176 185
 		} else {
177 186
 			return [];
178 187
 		}

+ 8
- 8
src/Util/BasePath.php View File

@@ -12,23 +12,24 @@ class BasePath
12 12
 	 * It first checks for the internal variable, then for DOCUMENT_ROOT and
13 13
 	 * finally for PWD
14 14
 	 *
15
-	 * @param string|null $basepath
15
+	 * @param string|null $basePath The default base path
16
+	 * @param array       $server   server arguments
16 17
 	 *
17 18
 	 * @return string
18 19
 	 *
19 20
 	 * @throws \Exception if directory isn't usable
20 21
 	 */
21
-	public static function create($basepath)
22
+	public static function create($basePath, $server = [])
22 23
 	{
23
-		if (!$basepath && !empty($_SERVER['DOCUMENT_ROOT'])) {
24
-			$basepath = $_SERVER['DOCUMENT_ROOT'];
24
+		if (!$basePath && !empty($server['DOCUMENT_ROOT'])) {
25
+			$basePath = $server['DOCUMENT_ROOT'];
25 26
 		}
26 27
 
27
-		if (!$basepath && !empty($_SERVER['PWD'])) {
28
-			$basepath = $_SERVER['PWD'];
28
+		if (!$basePath && !empty($server['PWD'])) {
29
+			$basePath = $server['PWD'];
29 30
 		}
30 31
 
31
-		return self::getRealPath($basepath);
32
+		return self::getRealPath($basePath);
32 33
 	}
33 34
 
34 35
 	/**
@@ -52,7 +53,6 @@ class BasePath
52 53
 		}
53 54
 	}
54 55
 
55
-
56 56
 	/**
57 57
 	 * @brief Checks if a given directory is usable for the system
58 58
 	 *

Loading…
Cancel
Save