Skip to content

Commit

Permalink
Use DI for $dbi in Menu.php
Browse files Browse the repository at this point in the history
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
  • Loading branch information
kamil-tekiela committed Nov 28, 2021
1 parent b604a86 commit 7b95a4b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
4 changes: 2 additions & 2 deletions libraries/classes/Header.php
Expand Up @@ -118,7 +118,7 @@ class Header
*/
public function __construct()
{
global $db, $table;
global $db, $table, $dbi;

$this->template = new Template();

Expand All @@ -127,7 +127,7 @@ public function __construct()
$this->bodyId = '';
$this->title = '';
$this->console = new Console();
$this->menu = new Menu($db ?? '', $table ?? '');
$this->menu = new Menu($dbi, $db ?? '', $table ?? '');
$this->menuEnabled = true;
$this->warningsEnabled = true;
$this->scripts = new Scripts();
Expand Down
45 changes: 23 additions & 22 deletions libraries/classes/Menu.php
Expand Up @@ -32,6 +32,10 @@ class Menu
* @var string
*/
private $db;

/** @var DatabaseInterface */
private $dbi;

/**
* Table name
*
Expand All @@ -52,11 +56,10 @@ class Menu
* @param string $db Database name
* @param string $table Table name
*/
public function __construct($db, $table)
public function __construct(DatabaseInterface $dbi, string $db, string $table)
{
global $dbi;

$this->db = $db;
$this->dbi = $dbi;
$this->table = $table;
$this->relation = new Relation($dbi);
$this->template = new Template();
Expand Down Expand Up @@ -118,8 +121,6 @@ private function getMenu(): string
*/
private function getAllowedTabs($level)
{
global $dbi;

$cacheKey = 'menu-levels-' . $level;
if (SessionCache::has($cacheKey)) {
return SessionCache::get($cacheKey);
Expand All @@ -139,11 +140,11 @@ private function getAllowedTabs($level)
. " AND `tab` LIKE '" . $level . "%'"
. ' AND `usergroup` = (SELECT usergroup FROM '
. $userTable . " WHERE `username` = '"
. $dbi->escapeString($GLOBALS['cfg']['Server']['user']) . "')";
. $this->dbi->escapeString($GLOBALS['cfg']['Server']['user']) . "')";

$result = $this->relation->queryAsControlUser($sqlQuery, false);
if ($result) {
while ($row = $dbi->fetchAssoc($result)) {
while ($row = $this->dbi->fetchAssoc($result)) {
$tabName = mb_substr(
$row['tab'],
mb_strpos($row['tab'], '_') + 1
Expand All @@ -165,7 +166,7 @@ private function getAllowedTabs($level)
*/
private function getBreadcrumbs(): string
{
global $cfg, $dbi;
global $cfg;

$server = [];
$database = [];
Expand All @@ -187,7 +188,7 @@ private function getBreadcrumbs(): string
if (strlen((string) $this->table) > 0) {
$table['name'] = $this->table;
$table['url'] = Util::getUrlForOption($cfg['DefaultTabTable'], 'table');
$tableObj = $dbi->getTable($this->db, $this->table);
$tableObj = $this->dbi->getTable($this->db, $this->table);
$table['is_view'] = $tableObj->isView();
$table['comment'] = '';
if (! $table['is_view']) {
Expand Down Expand Up @@ -223,19 +224,19 @@ private function getBreadcrumbs(): string
*/
private function getTableTabs()
{
global $route, $dbi;
global $route;

$isSystemSchema = Utilities::isSystemSchema($this->db);
$tableIsView = $dbi->getTable($this->db, $this->table)
$tableIsView = $this->dbi->getTable($this->db, $this->table)
->isView();
$updatableView = false;
if ($tableIsView) {
$updatableView = $dbi->getTable($this->db, $this->table)
$updatableView = $this->dbi->getTable($this->db, $this->table)
->isUpdatableView();
}

$isSuperUser = $dbi->isSuperUser();
$isCreateOrGrantUser = $dbi->isGrantUser() || $dbi->isCreateUser();
$isSuperUser = $this->dbi->isSuperUser();
$isCreateOrGrantUser = $this->dbi->isGrantUser() || $this->dbi->isCreateUser();

$tabs = [];

Expand Down Expand Up @@ -345,12 +346,12 @@ private function getTableTabs()
*/
private function getDbTabs()
{
global $route, $dbi;
global $route;

$isSystemSchema = Utilities::isSystemSchema($this->db);
$numTables = count($dbi->getTables($this->db));
$isSuperUser = $dbi->isSuperUser();
$isCreateOrGrantUser = $dbi->isGrantUser() || $dbi->isCreateUser();
$numTables = count($this->dbi->getTables($this->db));
$isSuperUser = $this->dbi->isSuperUser();
$isCreateOrGrantUser = $this->dbi->isGrantUser() || $this->dbi->isCreateUser();

$relationParameters = $this->relation->getRelationParameters();

Expand Down Expand Up @@ -462,14 +463,14 @@ private function getDbTabs()
*/
private function getServerTabs()
{
global $route, $dbi;
global $route;

$isSuperUser = $dbi->isSuperUser();
$isCreateOrGrantUser = $dbi->isGrantUser() || $dbi->isCreateUser();
$isSuperUser = $this->dbi->isSuperUser();
$isCreateOrGrantUser = $this->dbi->isGrantUser() || $this->dbi->isCreateUser();
if (SessionCache::has('binary_logs')) {
$binaryLogs = SessionCache::get('binary_logs');
} else {
$binaryLogs = $dbi->fetchResult(
$binaryLogs = $this->dbi->fetchResult(
'SHOW MASTER LOGS',
'Log_name',
null,
Expand Down
8 changes: 4 additions & 4 deletions test/classes/MenuTest.php
Expand Up @@ -34,7 +34,7 @@ protected function setUp(): void
*/
public function testServer(): void
{
$menu = new Menu('', '');
$menu = new Menu($this->dbi, '', '');
$this->assertStringContainsString(
'floating_menubar',
$menu->getDisplay()
Expand All @@ -46,7 +46,7 @@ public function testServer(): void
*/
public function testDatabase(): void
{
$menu = new Menu('pma_test', '');
$menu = new Menu($this->dbi, 'pma_test', '');
$this->assertStringContainsString(
'floating_menubar',
$menu->getDisplay()
Expand All @@ -58,7 +58,7 @@ public function testDatabase(): void
*/
public function testTable(): void
{
$menu = new Menu('pma_test', 'table1');
$menu = new Menu($this->dbi, 'pma_test', 'table1');
$this->assertStringContainsString(
'floating_menubar',
$menu->getDisplay()
Expand All @@ -70,7 +70,7 @@ public function testTable(): void
*/
public function testSetTable(): void
{
$menu = new Menu('pma_test', '');
$menu = new Menu($this->dbi, 'pma_test', '');
$menu->setTable('table1');
$this->assertStringContainsString(
'table1',
Expand Down

0 comments on commit 7b95a4b

Please sign in to comment.