Visitar URL original
Merge pull request #20009 from MauricioFauth/header-refactor · phpmyadmin/phpmyadmin@b9942b9 · GitHub
Skip to content

Commit b9942b9

Browse files
Merge pull request #20009 from MauricioFauth/header-refactor
Extract dependencies from Header
2 parents c2a0d7d + 6db52b0 commit b9942b9

7 files changed

Lines changed: 36 additions & 38 deletions

File tree

‎app/services.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use PhpMyAdmin\Export\TemplateModel;
2828
use PhpMyAdmin\FileListing;
2929
use PhpMyAdmin\FlashMessenger;
30+
use PhpMyAdmin\Header;
3031
use PhpMyAdmin\Http\Factory\ResponseFactory;
3132
use PhpMyAdmin\Http\Middleware;
3233
use PhpMyAdmin\Import\Import;
@@ -110,6 +111,10 @@
110111
'expression_language' => ['class' => ExpressionLanguage::class],
111112
'file_listing' => ['class' => FileListing::class],
112113
FlashMessenger::class => ['class' => FlashMessenger::class],
114+
Header::class => [
115+
'class' => Header::class,
116+
'arguments' => ['@template', '@' . Console::class, '@config', '@dbi', '@relation', '@user_preferences'],
117+
],
113118
'http_request' => ['class' => HttpRequest::class],
114119
ResponseFactory::class => [
115120
'class' => ResponseFactory::class,

‎phpstan-baseline.neon‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7125,15 +7125,6 @@ parameters:
71257125
count: 1
71267126
path: src/Git.php
71277127

7128-
-
7129-
message: '''
7130-
#^Call to deprecated method getInstance\(\) of class PhpMyAdmin\\Dbal\\DatabaseInterface\:
7131-
Use dependency injection instead\.$#
7132-
'''
7133-
identifier: staticMethod.deprecated
7134-
count: 4
7135-
path: src/Header.php
7136-
71377128
-
71387129
message: '#^Cannot access offset ''pftext'' on mixed\.$#'
71397130
identifier: offsetAccess.nonOffsetAccessible

‎psalm-baseline.xml‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4762,12 +4762,6 @@
47624762
</RiskyTruthyFalsyComparison>
47634763
</file>
47644764
<file src="src/Header.php">
4765-
<DeprecatedMethod>
4766-
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
4767-
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
4768-
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
4769-
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
4770-
</DeprecatedMethod>
47714765
<MixedAssignment>
47724766
<code><![CDATA[$pftext]]></code>
47734767
</MixedAssignment>
@@ -6207,7 +6201,6 @@
62076201
</PossiblyNullReference>
62086202
<PossiblyUnusedReturnValue>
62096203
<code><![CDATA[bool]]></code>
6210-
<code><![CDATA[bool]]></code>
62116204
</PossiblyUnusedReturnValue>
62126205
<RiskyTruthyFalsyComparison>
62136206
<code><![CDATA[empty($column->collation)]]></code>

‎src/Header.php‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,26 @@ class Header
5555
*/
5656
private bool $warningsEnabled = true;
5757

58-
private UserPreferences $userPreferences;
59-
6058
private bool $isTransformationWrapper = false;
6159

6260
public function __construct(
6361
private readonly Template $template,
6462
private readonly Console $console,
6563
private readonly Config $config,
64+
private readonly DatabaseInterface $dbi,
65+
private readonly Relation $relation,
66+
private readonly UserPreferences $userPreferences,
6667
) {
67-
$dbi = DatabaseInterface::getInstance();
68-
$relation = new Relation($dbi);
69-
$this->menu = new Menu($dbi, $this->template, $this->config, $relation, Current::$database, Current::$table);
68+
$this->menu = new Menu(
69+
$this->dbi,
70+
$this->template,
71+
$this->config,
72+
$this->relation,
73+
Current::$database,
74+
Current::$table,
75+
);
7076
$this->scripts = new Scripts($this->template);
7177
$this->addDefaultScripts();
72-
73-
$this->userPreferences = new UserPreferences($dbi, $relation, $this->template);
7478
}
7579

7680
private function isMenuEnabled(): bool
@@ -79,8 +83,7 @@ private function isMenuEnabled(): bool
7983
return $this->isMenuEnabled;
8084
}
8185

82-
$dbi = DatabaseInterface::getInstance();
83-
$this->isMenuEnabled = $dbi->isConnected();
86+
$this->isMenuEnabled = $this->dbi->isConnected();
8487

8588
return $this->isMenuEnabled;
8689
}
@@ -132,7 +135,7 @@ public function getJsParams(): array
132135
'confirm' => $this->config->settings['Confirm'],
133136
'LoginCookieValidity' => $this->config->settings['LoginCookieValidity'],
134137
'session_gc_maxlifetime' => (int) ini_get('session.gc_maxlifetime'),
135-
'logged_in' => DatabaseInterface::getInstance()->isConnected(),
138+
'logged_in' => $this->dbi->isConnected(),
136139
'is_https' => $this->config->isHttps(),
137140
'rootPath' => $this->config->getRootPath(),
138141
'arg_separator' => Url::getArgSeparator(),
@@ -256,9 +259,8 @@ public function getDisplay(): array
256259
);
257260
$this->scripts->addFiles($this->console->getScripts());
258261

259-
$dbi = DatabaseInterface::getInstance();
260262
if ($this->isMenuEnabled() && Current::$server > 0) {
261-
$navigation = (new Navigation($this->template, new Relation($dbi), $dbi, $this->config))->getDisplay();
263+
$navigation = (new Navigation($this->template, $this->relation, $this->dbi, $this->config))->getDisplay();
262264
}
263265

264266
$customHeader = self::renderHeader();
@@ -277,7 +279,7 @@ public function getDisplay(): array
277279

278280
$console = $this->console->getDisplay();
279281
$messages = $this->getMessage();
280-
$isLoggedIn = $dbi->isConnected();
282+
$isLoggedIn = $this->dbi->isConnected();
281283

282284
$this->scripts->addFile('datetimepicker.js');
283285
$this->scripts->addFile('validator-messages.js');
@@ -303,8 +305,8 @@ public function getDisplay(): array
303305
'theme_color_mode' => $theme->getColorMode(),
304306
'theme_color_modes' => $theme->getColorModes(),
305307
'theme_id' => $theme->getId(),
306-
'current_user' => $dbi->getCurrentUserAndHost(),
307-
'is_mariadb' => $dbi->isMariaDB(),
308+
'current_user' => $this->dbi->getCurrentUserAndHost(),
309+
'is_mariadb' => $this->dbi->isMariaDB(),
308310
];
309311
}
310312

‎src/ResponseRenderer.php‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace PhpMyAdmin;
66

77
use Fig\Http\Message\StatusCodeInterface;
8-
use PhpMyAdmin\Console\Console;
98
use PhpMyAdmin\Container\ContainerBuilder;
109
use PhpMyAdmin\Dbal\DatabaseInterface;
1110
use PhpMyAdmin\Error\ErrorHandler;
@@ -164,7 +163,7 @@ public static function getInstance(): ResponseRenderer
164163
return self::$instance;
165164
}
166165

167-
$console = ContainerBuilder::getContainer()->get(Console::class);
166+
$header = ContainerBuilder::getContainer()->get(Header::class);
168167

169168
$config = Config::getInstance();
170169
$template = new Template($config);
@@ -173,7 +172,7 @@ public static function getInstance(): ResponseRenderer
173172
self::$instance = new ResponseRenderer(
174173
$config,
175174
$template,
176-
new Header($template, $console, $config),
175+
$header,
177176
new Footer($template, $config),
178177
ErrorHandler::getInstance(),
179178
$dbi,

‎tests/unit/HeaderTest.php‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PhpMyAdmin\Header;
1515
use PhpMyAdmin\Message;
1616
use PhpMyAdmin\Template;
17+
use PhpMyAdmin\UserPreferences;
1718
use PHPUnit\Framework\Attributes\CoversClass;
1819
use PHPUnit\Framework\Attributes\DataProvider;
1920
use PHPUnit\Framework\Attributes\Medium;
@@ -56,15 +57,19 @@ protected function setUp(): void
5657
private function getNewHeaderInstance(): Header
5758
{
5859
$dbi = DatabaseInterface::getInstance();
59-
$relation = new Relation($dbi);
60-
$template = new Template();
6160
$config = Config::getInstance();
61+
$relation = new Relation($dbi, $config);
62+
$template = new Template($config);
6263
$history = new History($dbi, $relation, $config);
64+
$userPreferences = new UserPreferences($dbi, $relation, $template);
6365

6466
return new Header(
6567
$template,
6668
new Console($relation, $template, new BookmarkRepository($dbi, $relation), $history),
6769
$config,
70+
$dbi,
71+
$relation,
72+
$userPreferences,
6873
);
6974
}
7075

@@ -83,7 +88,8 @@ public function testEnable(): void
8388
$template = new Template($config);
8489
$history = new History($dbi, $relation, $config);
8590
$console = new Console($relation, $template, new BookmarkRepository($dbi, $relation), $history);
86-
$header = new Header($template, $console, $config);
91+
$userPreferences = new UserPreferences($dbi, $relation, $template);
92+
$header = new Header($template, $console, $config, $dbi, $relation, $userPreferences);
8793

8894
$header->setBodyId('PMA_header_id');
8995
$actual = $header->getDisplay();

‎tests/unit/Stubs/ResponseRenderer.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use PhpMyAdmin\Http\Response;
2626
use PhpMyAdmin\Message;
2727
use PhpMyAdmin\Template;
28+
use PhpMyAdmin\UserPreferences;
2829

2930
use function is_array;
3031
use function json_encode;
@@ -59,11 +60,12 @@ public function __construct()
5960
$relation = new Relation($dbi);
6061
$history = new History($dbi, $relation, $config);
6162
$console = new Console($relation, $template, new BookmarkRepository($dbi, $relation), $history);
63+
$userPreferences = new UserPreferences($dbi, $relation, $template);
6264

6365
parent::__construct(
6466
$config,
6567
$template,
66-
new Header($template, $console, $config),
68+
new Header($template, $console, $config, $dbi, $relation, $userPreferences),
6769
new Footer($template, $config),
6870
ErrorHandler::getInstance(),
6971
$dbi,

0 commit comments

Comments
 (0)