Skip to content

Commit ad274fd

Browse files
Merge pull request #18635 from MauricioFauth/dbi-global-singleton
Replace DatabaseInterface global var with a singleton object
2 parents 6c04d72 + 83b1d6b commit ad274fd

293 files changed

Lines changed: 2523 additions & 4460 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

libraries/classes/Application.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use PhpMyAdmin\Middleware\CurrentServerGlobalSetting;
2020
use PhpMyAdmin\Middleware\DatabaseAndTableSetting;
2121
use PhpMyAdmin\Middleware\DatabaseServerVersionChecking;
22-
use PhpMyAdmin\Middleware\DbiLoading;
2322
use PhpMyAdmin\Middleware\EncryptedQueryParamsHandling;
2423
use PhpMyAdmin\Middleware\ErrorHandling;
2524
use PhpMyAdmin\Middleware\GlobalConfigSetting;
@@ -109,7 +108,6 @@ public function run(bool $isSetupPage = false): void
109108
$requestHandler->add(new SetupPageRedirection($this->config, $this->responseFactory));
110109
$requestHandler->add(new MinimumCommonRedirection($this->config, $this->responseFactory));
111110
$requestHandler->add(new LanguageAndThemeCookieSaving($this->config));
112-
$requestHandler->add(new DbiLoading());
113111
$requestHandler->add(new LoginCookieValiditySetting($this->config));
114112
$requestHandler->add(new Authentication($this->config, $this->template, $this->responseFactory));
115113
$requestHandler->add(new DatabaseServerVersionChecking($this->config, $this->template, $this->responseFactory));

libraries/classes/Command/CacheWarmupCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private function warmUpTwigCache(
122122
$config->loadAndCheck(CONFIG_FILE);
123123
$GLOBALS['cfg']['environment'] = $environment;
124124
$config->set('environment', $GLOBALS['cfg']['environment']);
125-
$GLOBALS['dbi'] = new DatabaseInterface(new DbiDummy());
125+
DatabaseInterface::$instance = new DatabaseInterface(new DbiDummy());
126126
$tmpDir = ROOT_PATH . 'twig-templates';
127127
$twig = Template::getTwigEnvironment($tmpDir);
128128

libraries/classes/Config.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,12 @@ public function load(string|null $source = null): bool
383383
private function setConnectionCollation(): void
384384
{
385385
$collationConnection = $this->get('DefaultConnectionCollation');
386-
if (empty($collationConnection) || $collationConnection === $GLOBALS['dbi']->getDefaultCollation()) {
386+
$dbi = DatabaseInterface::getInstance();
387+
if (empty($collationConnection) || $collationConnection === $dbi->getDefaultCollation()) {
387388
return;
388389
}
389390

390-
$GLOBALS['dbi']->setCollation($collationConnection);
391+
$dbi->setCollation($collationConnection);
391392
}
392393

393394
/**
@@ -408,7 +409,8 @@ public function loadUserPreferences(ThemeManager $themeManager, bool $isMinimumC
408409
! isset($_SESSION['cache'][$cacheKey]['userprefs'])
409410
|| $_SESSION['cache'][$cacheKey]['config_mtime'] < $this->sourceMtime
410411
) {
411-
$userPreferences = new UserPreferences($GLOBALS['dbi'], new Relation($GLOBALS['dbi']), new Template());
412+
$dbi = DatabaseInterface::getInstance();
413+
$userPreferences = new UserPreferences($dbi, new Relation($dbi), new Template());
412414
$prefs = $userPreferences->load();
413415
$_SESSION['cache'][$cacheKey]['userprefs'] = $userPreferences->apply($prefs['config_data']);
414416
$_SESSION['cache'][$cacheKey]['userprefs_mtime'] = $prefs['mtime'];
@@ -507,7 +509,8 @@ public function setUserValue(
507509
mixed $newCfgValue,
508510
string|null $defaultValue = null,
509511
): bool|Message {
510-
$userPreferences = new UserPreferences($GLOBALS['dbi'], new Relation($GLOBALS['dbi']), new Template());
512+
$dbi = DatabaseInterface::getInstance();
513+
$userPreferences = new UserPreferences($dbi, new Relation($dbi), new Template());
511514
$result = true;
512515
// use permanent user preferences if possible
513516
$prefsType = $this->get('user_preferences');

libraries/classes/ConfigStorage/UserGroups.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ public static function getHtmlForListingUsersofAGroup(
4545
$userGroupSpecialChars = htmlspecialchars($userGroup);
4646
$usersTable = Util::backquote($configurableMenusFeature->database)
4747
. '.' . Util::backquote($configurableMenusFeature->users);
48+
$dbi = DatabaseInterface::getInstance();
4849
$sqlQuery = 'SELECT `username` FROM ' . $usersTable
49-
. ' WHERE `usergroup`=' . $GLOBALS['dbi']->quoteString($userGroup, Connection::TYPE_CONTROL);
50-
$result = $GLOBALS['dbi']->tryQueryAsControlUser($sqlQuery);
50+
. ' WHERE `usergroup`=' . $dbi->quoteString($userGroup, Connection::TYPE_CONTROL);
51+
$result = $dbi->tryQueryAsControlUser($sqlQuery);
5152
if ($result) {
5253
$i = 0;
5354
while ($row = $result->fetchRow()) {
@@ -73,7 +74,7 @@ public static function getHtmlForUserGroupsTable(ConfigurableMenusFeature $confi
7374
$groupTable = Util::backquote($configurableMenusFeature->database)
7475
. '.' . Util::backquote($configurableMenusFeature->userGroups);
7576
$sqlQuery = 'SELECT * FROM ' . $groupTable . ' ORDER BY `usergroup` ASC';
76-
$result = $GLOBALS['dbi']->tryQueryAsControlUser($sqlQuery);
77+
$result = DatabaseInterface::getInstance()->tryQueryAsControlUser($sqlQuery);
7778
$userGroups = [];
7879
$userGroupsValues = [];
7980
$action = Url::getFromRoute('/server/privileges');
@@ -203,9 +204,10 @@ public static function getHtmlToEditUserGroup(
203204
if ($userGroup !== null) {
204205
$groupTable = Util::backquote($configurableMenusFeature->database)
205206
. '.' . Util::backquote($configurableMenusFeature->userGroups);
207+
$dbi = DatabaseInterface::getInstance();
206208
$sqlQuery = 'SELECT * FROM ' . $groupTable
207-
. ' WHERE `usergroup`=' . $GLOBALS['dbi']->quoteString($userGroup, Connection::TYPE_CONTROL);
208-
$result = $GLOBALS['dbi']->tryQueryAsControlUser($sqlQuery);
209+
. ' WHERE `usergroup`=' . $dbi->quoteString($userGroup, Connection::TYPE_CONTROL);
210+
$result = $dbi->tryQueryAsControlUser($sqlQuery);
209211
if ($result) {
210212
foreach ($result as $row) {
211213
$key = $row['tab'];
@@ -296,10 +298,11 @@ public static function edit(
296298
$groupTable = Util::backquote($configurableMenusFeature->database)
297299
. '.' . Util::backquote($configurableMenusFeature->userGroups);
298300

301+
$dbi = DatabaseInterface::getInstance();
299302
if (! $new) {
300303
$sqlQuery = 'DELETE FROM ' . $groupTable
301-
. ' WHERE `usergroup`=' . $GLOBALS['dbi']->quoteString($userGroup, Connection::TYPE_CONTROL) . ';';
302-
$GLOBALS['dbi']->queryAsControlUser($sqlQuery);
304+
. ' WHERE `usergroup`=' . $dbi->quoteString($userGroup, Connection::TYPE_CONTROL) . ';';
305+
$dbi->queryAsControlUser($sqlQuery);
303306
}
304307

305308
$sqlQuery = 'INSERT INTO ' . $groupTable
@@ -315,14 +318,14 @@ public static function edit(
315318

316319
$tabName = $tabGroupName . '_' . $tab;
317320
$allowed = isset($_POST[$tabName]) && $_POST[$tabName] === 'Y';
318-
$sqlQuery .= '(' . $GLOBALS['dbi']->quoteString($userGroup, Connection::TYPE_CONTROL)
319-
. ', ' . $GLOBALS['dbi']->quoteString($tabName, Connection::TYPE_CONTROL) . ", '"
321+
$sqlQuery .= '(' . $dbi->quoteString($userGroup, Connection::TYPE_CONTROL)
322+
. ', ' . $dbi->quoteString($tabName, Connection::TYPE_CONTROL) . ", '"
320323
. ($allowed ? 'Y' : 'N') . "')";
321324
$first = false;
322325
}
323326
}
324327

325328
$sqlQuery .= ';';
326-
$GLOBALS['dbi']->queryAsControlUser($sqlQuery);
329+
$dbi->queryAsControlUser($sqlQuery);
327330
}
328331
}

libraries/classes/Console.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ public function disable(): void
5858
public static function getBookmarkContent(): string
5959
{
6060
$template = new Template();
61-
$relation = new Relation($GLOBALS['dbi']);
61+
$dbi = DatabaseInterface::getInstance();
62+
$relation = new Relation($dbi);
6263
$bookmarkFeature = $relation->getRelationParameters()->bookmarkFeature;
6364
if ($bookmarkFeature === null) {
6465
return '';
6566
}
6667

67-
$bookmarks = Bookmark::getList($bookmarkFeature, $GLOBALS['dbi'], $GLOBALS['cfg']['Server']['user']);
68+
$bookmarks = Bookmark::getList($bookmarkFeature, $dbi, $GLOBALS['cfg']['Server']['user']);
6869
$countBookmarks = count($bookmarks);
6970
if ($countBookmarks > 0) {
7071
$welcomeMessage = sprintf(

libraries/classes/Controllers/Database/Structure/CopyFormController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpMyAdmin\Controllers\Database\Structure;
66

77
use PhpMyAdmin\Controllers\AbstractController;
8+
use PhpMyAdmin\DatabaseInterface;
89
use PhpMyAdmin\Http\ServerRequest;
910

1011
use function __;
@@ -27,7 +28,7 @@ public function __invoke(ServerRequest $request): void
2728
$urlParams['selected'][] = $selectedValue;
2829
}
2930

30-
$databasesList = $GLOBALS['dbi']->getDatabaseList();
31+
$databasesList = DatabaseInterface::getInstance()->getDatabaseList();
3132
foreach ($databasesList as $key => $databaseName) {
3233
if ($databaseName == $GLOBALS['db']) {
3334
$databasesList->offsetUnset($key);

libraries/classes/Controllers/DatabaseController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
namespace PhpMyAdmin\Controllers;
66

7+
use PhpMyAdmin\DatabaseInterface;
78
use PhpMyAdmin\Http\ServerRequest;
89

910
final class DatabaseController extends AbstractController
1011
{
1112
public function __invoke(ServerRequest $request): void
1213
{
13-
$this->response->addJSON(['databases' => $GLOBALS['dbi']->getDatabaseList()]);
14+
$this->response->addJSON(['databases' => DatabaseInterface::getInstance()->getDatabaseList()]);
1415
}
1516
}

libraries/classes/Core.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public static function warnMissingExtension(
134134
*/
135135
public static function getTableCount(string $db): int
136136
{
137-
$tables = $GLOBALS['dbi']->tryQuery('SHOW TABLES FROM ' . Util::backquote($db) . ';');
137+
$tables = DatabaseInterface::getInstance()->tryQuery('SHOW TABLES FROM ' . Util::backquote($db) . ';');
138138

139139
if ($tables) {
140140
return $tables->numRows();

libraries/classes/Database/MultiTableQuery.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,13 @@ public static function displayResults(string $sqlQuery, string $db): string
7575

7676
$goto = Url::getFromRoute('/database/multi-table-query');
7777

78-
$relation = new Relation($GLOBALS['dbi']);
78+
$dbi = DatabaseInterface::getInstance();
79+
$relation = new Relation($dbi);
7980
$sql = new Sql(
80-
$GLOBALS['dbi'],
81+
$dbi,
8182
$relation,
82-
new RelationCleanup($GLOBALS['dbi'], $relation),
83-
new Operations($GLOBALS['dbi'], $relation),
83+
new RelationCleanup($dbi, $relation),
84+
new Operations($dbi, $relation),
8485
new Transformations(),
8586
new Template(),
8687
);

libraries/classes/DatabaseInterface.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
*/
7575
class DatabaseInterface implements DbalInterface
7676
{
77+
public static self|null $instance = null;
78+
7779
/**
7880
* Force STORE_RESULT method, ignored by classic MySQL.
7981
*/
@@ -143,6 +145,16 @@ public function __construct(private DbiExtension $extension)
143145
$this->types = new Types($this);
144146
}
145147

148+
/** @deprecated Use dependency injection instead. */
149+
public static function getInstance(): self
150+
{
151+
if (self::$instance === null) {
152+
self::$instance = new self(new DbiMysqli());
153+
}
154+
155+
return self::$instance;
156+
}
157+
146158
/**
147159
* runs a query
148160
*
@@ -1993,20 +2005,6 @@ public function setVersion(array $version): void
19932005
$this->isPercona = stripos($this->versionComment, 'percona') !== false;
19942006
}
19952007

1996-
/**
1997-
* Load correct database driver
1998-
*
1999-
* @param DbiExtension|null $extension Force the use of an alternative extension
2000-
*/
2001-
public static function load(DbiExtension|null $extension = null): self
2002-
{
2003-
if ($extension !== null) {
2004-
return new self($extension);
2005-
}
2006-
2007-
return new self(new DbiMysqli());
2008-
}
2009-
20102008
/**
20112009
* Prepare an SQL statement for execution.
20122010
*

0 commit comments

Comments
 (0)