Skip to content

Commit 8228122

Browse files
Merge pull request #17897 from MauricioFauth/config-load
Extract config load and check from Config's constructor
2 parents e5cbbca + b2eb6bf commit 8228122

17 files changed

Lines changed: 48 additions & 32 deletions

libraries/classes/Command/CacheWarmupCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use function str_replace;
2727

2828
use const CACHE_DIR;
29+
use const CONFIG_FILE;
2930

3031
final class CacheWarmupCommand extends Command
3132
{
@@ -121,7 +122,8 @@ private function warmUpTwigCache(
121122
$GLOBALS['config'] = $GLOBALS['config'] ?? null;
122123

123124
$output->writeln('Warming up the twig cache', OutputInterface::VERBOSITY_VERBOSE);
124-
$GLOBALS['config'] = new Config(CONFIG_FILE);
125+
$GLOBALS['config'] = new Config();
126+
$GLOBALS['config']->loadAndCheck(CONFIG_FILE);
125127
$GLOBALS['cfg']['environment'] = $environment;
126128
$GLOBALS['config']->set('environment', $GLOBALS['cfg']['environment']);
127129
$GLOBALS['dbi'] = new DatabaseInterface(new DbiDummy());

libraries/classes/Common.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use function trigger_error;
4848
use function urldecode;
4949

50+
use const CONFIG_FILE;
5051
use const E_USER_ERROR;
5152

5253
final class Common
@@ -126,6 +127,7 @@ public static function run(bool $isSetupPage = false): void
126127

127128
/** @var Config $config */
128129
$config = $GLOBALS['containerBuilder']->get('config');
130+
$config->loadAndCheck(CONFIG_FILE);
129131
$GLOBALS['config'] = $config;
130132

131133
if ($route !== '/messages') {

libraries/classes/Config.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,17 @@ class Config
104104
public $done = false;
105105

106106
/**
107-
* @param string $source source to read config from
107+
* @param string|null $source source to read config from
108108
*/
109-
public function __construct(?string $source = null)
109+
public function loadAndCheck(?string $source = null): void
110110
{
111111
$this->settings = ['is_setup' => false];
112112

113113
// functions need to refresh in case of config file changed goes in
114114
// PhpMyAdmin\Config::load()
115115
$this->load($source);
116116

117-
// other settings, independent from config file, comes in
117+
// other settings, independent of config file, comes in
118118
$this->checkSystem();
119119

120120
$this->baseSettings = $this->settings;

libraries/classes/Controllers/Preferences/ManageController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use function urlencode;
3636
use function var_export;
3737

38+
use const CONFIG_FILE;
3839
use const JSON_PRETTY_PRINT;
3940
use const PHP_URL_PATH;
4041
use const UPLOAD_ERR_OK;

libraries/classes/Controllers/Setup/MainController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use function in_array;
1717
use function sprintf;
1818

19+
use const CONFIG_FILE;
20+
1921
final class MainController
2022
{
2123
public function __invoke(ServerRequest $request): void

libraries/services.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
],
2121
'config' => [
2222
'class' => PhpMyAdmin\Config::class,
23-
'arguments' => [CONFIG_FILE],
2423
],
2524
'central_columns' => [
2625
'class' => PhpMyAdmin\Database\CentralColumns::class,

scripts/console

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ if (! class_exists(Application::class)) {
2828

2929
$containerBuilder = Core::getContainerBuilder();
3030
$cfg['environment'] = 'production';
31-
$config = new Config(CONFIG_FILE);
31+
$config = new Config();
32+
$config->loadAndCheck(CONFIG_FILE);
3233
$config->set('environment', $cfg['environment']);
3334
$dbi = new DatabaseInterface(new DbiDummy());
3435

test/classes/AbstractTestCase.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,17 @@ protected function createDbiDummy(): DbiDummy
180180
return new DbiDummy();
181181
}
182182

183+
protected function createConfig(): Config
184+
{
185+
$config = new Config();
186+
$config->loadAndCheck();
187+
188+
return $config;
189+
}
190+
183191
protected function setGlobalConfig(): void
184192
{
185-
$GLOBALS['config'] = new Config();
193+
$GLOBALS['config'] = $this->createConfig();
186194
$GLOBALS['config']->checkServers();
187195
$GLOBALS['config']->set('environment', 'development');
188196
$GLOBALS['cfg'] = $GLOBALS['config']->settings;

test/classes/Config/FormDisplayTemplateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected function setUp(): void
2626
{
2727
parent::setUp();
2828
parent::setLanguage();
29-
$this->config = new Config();
29+
$this->config = $this->createConfig();
3030
$this->formDisplayTemplate = new FormDisplayTemplate($this->config);
3131
}
3232

test/classes/ConfigTest.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use function tempnam;
3131
use function unlink;
3232

33+
use const CONFIG_FILE;
3334
use const DIRECTORY_SEPARATOR;
3435
use const INFO_MODULES;
3536
use const PHP_EOL;
@@ -56,15 +57,17 @@ protected function setUp(): void
5657
parent::setTheme();
5758
$GLOBALS['dbi'] = $this->createDatabaseInterface();
5859
$_SERVER['HTTP_USER_AGENT'] = '';
59-
$this->object = new Config();
60+
$this->object = $this->createConfig();
6061
$GLOBALS['server'] = 0;
6162
$_SESSION['git_location'] = '.git';
6263
$_SESSION['is_git_revision'] = true;
63-
$GLOBALS['config'] = new Config(CONFIG_FILE);
64+
$GLOBALS['config'] = new Config();
65+
$GLOBALS['config']->loadAndCheck(CONFIG_FILE);
6466
$GLOBALS['cfg']['ProxyUrl'] = '';
6567

6668
//for testing file permissions
67-
$this->permTestObj = new Config(ROOT_PATH . 'config.sample.inc.php');
69+
$this->permTestObj = new Config();
70+
$this->permTestObj->loadAndCheck(ROOT_PATH . 'config.sample.inc.php');
6871
}
6972

7073
/**
@@ -83,7 +86,7 @@ protected function tearDown(): void
8386
*/
8487
public function testLoadConfigs(): void
8588
{
86-
$defaultConfig = new Config();
89+
$defaultConfig = $this->createConfig();
8790
$tmpConfig = tempnam('./', 'config.test.inc.php');
8891
if ($tmpConfig === false) {
8992
$this->markTestSkipped('Creating a temporary file does not work');
@@ -94,15 +97,17 @@ public function testLoadConfigs(): void
9497
// end of setup
9598

9699
// Test loading an empty file does not change the default config
97-
$config = new Config($tmpConfig);
100+
$config = new Config();
101+
$config->loadAndCheck($tmpConfig);
98102
$this->assertSame($defaultConfig->settings, $config->settings);
99103

100104
$contents = '<?php' . PHP_EOL
101105
. '$cfg[\'ProtectBinary\'] = true;';
102106
file_put_contents($tmpConfig, $contents);
103107

104108
// Test loading a config changes the setup
105-
$config = new Config($tmpConfig);
109+
$config = new Config();
110+
$config->loadAndCheck($tmpConfig);
106111
$defaultConfig->settings['ProtectBinary'] = true;
107112
$this->assertSame($defaultConfig->settings, $config->settings);
108113
$defaultConfig->settings['ProtectBinary'] = 'blob';
@@ -117,7 +122,7 @@ public function testLoadConfigs(): void
117122
*/
118123
public function testLoadInvalidConfigs(): void
119124
{
120-
$defaultConfig = new Config();
125+
$defaultConfig = $this->createConfig();
121126
$tmpConfig = tempnam('./', 'config.test.inc.php');
122127
if ($tmpConfig === false) {
123128
$this->markTestSkipped('Creating a temporary file does not work');
@@ -128,15 +133,17 @@ public function testLoadInvalidConfigs(): void
128133
// end of setup
129134

130135
// Test loading an empty file does not change the default config
131-
$config = new Config($tmpConfig);
136+
$config = new Config();
137+
$config->loadAndCheck($tmpConfig);
132138
$this->assertSame($defaultConfig->settings, $config->settings);
133139

134140
$contents = '<?php' . PHP_EOL
135141
. '$cfg[\'fooBar\'] = true;';
136142
file_put_contents($tmpConfig, $contents);
137143

138144
// Test loading a custom key config changes the setup
139-
$config = new Config($tmpConfig);
145+
$config = new Config();
146+
$config->loadAndCheck($tmpConfig);
140147
$defaultConfig->settings['fooBar'] = true;
141148
// Equals because of the key sorting
142149
$this->assertEquals($defaultConfig->settings, $config->settings);
@@ -151,7 +158,8 @@ public function testLoadInvalidConfigs(): void
151158
file_put_contents($tmpConfig, $contents);
152159

153160
// Test loading a custom key config changes the setup
154-
$config = new Config($tmpConfig);
161+
$config = new Config();
162+
$config->loadAndCheck($tmpConfig);
155163
$defaultConfig->settings['ValidKey'] = true;
156164
// Equals because of the key sorting
157165
$this->assertEquals($defaultConfig->settings, $config->settings);

0 commit comments

Comments
 (0)