From d07fa7e5f48dc4bf30d5309516f82fb29d27a065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Mon, 12 Feb 2024 13:09:29 -0300 Subject: [PATCH] Extract Config from Navigation class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- app/services.php | 2 +- psalm-baseline.xml | 9 +--- src/Header.php | 7 +-- src/Navigation/Navigation.php | 45 ++++++++++--------- .../Controllers/NavigationControllerTest.php | 4 +- tests/classes/Navigation/NavigationTest.php | 10 ++--- 6 files changed, 33 insertions(+), 44 deletions(-) diff --git a/app/services.php b/app/services.php index e76f0ceca3..a7aba92bb2 100644 --- a/app/services.php +++ b/app/services.php @@ -122,7 +122,7 @@ return [ ], 'navigation' => [ 'class' => Navigation::class, - 'arguments' => ['@template', '@relation', '@dbi'], + 'arguments' => ['@template', '@relation', '@dbi', '@config'], ], 'normalization' => [ 'class' => Normalization::class, diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 7ef540ebab..ee97a0ab26 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -6885,13 +6885,6 @@ - - Config::getInstance() - Config::getInstance() - Config::getInstance() - Config::getInstance() - Config::getInstance() - $hidden $hidden @@ -14227,6 +14220,8 @@ Config::getInstance() + Config::getInstance() + Config::getInstance() diff --git a/src/Header.php b/src/Header.php index 498c9d2294..3938e42da2 100644 --- a/src/Header.php +++ b/src/Header.php @@ -315,12 +315,7 @@ class Header $dbi = DatabaseInterface::getInstance(); if ($this->menuEnabled && Current::$server > 0) { - $nav = new Navigation( - $this->template, - new Relation($dbi), - $dbi, - ); - $navigation = $nav->getDisplay(); + $navigation = (new Navigation($this->template, new Relation($dbi), $dbi, $this->config))->getDisplay(); } $customHeader = Config::renderHeader(); diff --git a/src/Navigation/Navigation.php b/src/Navigation/Navigation.php index 5b3ad4a1fc..067c7974ce 100644 --- a/src/Navigation/Navigation.php +++ b/src/Navigation/Navigation.php @@ -43,9 +43,13 @@ class Navigation private NavigationTree $tree; private readonly UserPrivilegesFactory $userPrivilegesFactory; - public function __construct(private Template $template, private Relation $relation, private DatabaseInterface $dbi) - { - $this->tree = new NavigationTree($this->template, $this->dbi, $this->relation, Config::getInstance()); + public function __construct( + private Template $template, + private Relation $relation, + private DatabaseInterface $dbi, + private readonly Config $config, + ) { + $this->tree = new NavigationTree($this->template, $this->dbi, $this->relation, $this->config); $this->userPrivilegesFactory = new UserPrivilegesFactory($this->dbi); } @@ -58,9 +62,8 @@ class Navigation { $userPrivileges = $this->userPrivilegesFactory->getPrivileges(); - $config = Config::getInstance(); $logo = [ - 'is_displayed' => $config->settings['NavigationDisplayLogo'], + 'is_displayed' => $this->config->settings['NavigationDisplayLogo'], 'has_link' => false, 'link' => '#', 'attributes' => ' target="_blank" rel="noopener noreferrer"', @@ -70,13 +73,13 @@ class Navigation $response = ResponseRenderer::getInstance(); if (! $response->isAjax()) { $logo['source'] = $this->getLogoSource(); - $logo['has_link'] = $config->settings['NavigationLogoLink'] !== ''; - $logo['link'] = trim($config->settings['NavigationLogoLink']); + $logo['has_link'] = $this->config->settings['NavigationLogoLink'] !== ''; + $logo['link'] = trim($this->config->settings['NavigationLogoLink']); if (! Sanitize::checkLink($logo['link'], true)) { $logo['link'] = 'index.php'; } - if ($config->settings['NavigationLogoLinkWindow'] === 'main') { + if ($this->config->settings['NavigationLogoLinkWindow'] === 'main') { if (empty(parse_url($logo['link'], PHP_URL_HOST))) { $logo['link'] .= Url::getCommon( [], @@ -91,7 +94,7 @@ class Navigation } } - if ($config->settings['NavigationDisplayServers'] && count($config->settings['Servers']) > 1) { + if ($this->config->settings['NavigationDisplayServers'] && count($this->config->settings['Servers']) > 1) { $serverSelect = Select::render(true); } @@ -106,7 +109,7 @@ class Navigation } if (! $response->isAjax() || ! empty($_POST['full']) || ! empty($_POST['reload'])) { - if ($config->settings['ShowDatabasesNavigationAsTree']) { + if ($this->config->settings['ShowDatabasesNavigationAsTree']) { // provide database tree in navigation $navRender = $this->tree->renderState($userPrivileges); } else { @@ -120,19 +123,19 @@ class Navigation return $this->template->render('navigation/main', [ 'is_ajax' => $response->isAjax(), 'logo' => $logo, - 'config_navigation_width' => $config->settings['NavigationWidth'], - 'is_synced' => $config->settings['NavigationLinkWithMainPanel'], - 'is_highlighted' => $config->settings['NavigationTreePointerEnable'], - 'is_autoexpanded' => $config->settings['NavigationTreeAutoexpandSingleDb'], + 'config_navigation_width' => $this->config->settings['NavigationWidth'], + 'is_synced' => $this->config->settings['NavigationLinkWithMainPanel'], + 'is_highlighted' => $this->config->settings['NavigationTreePointerEnable'], + 'is_autoexpanded' => $this->config->settings['NavigationTreeAutoexpandSingleDb'], 'server' => Current::$server, - 'auth_type' => $config->selectedServer['auth_type'], - 'is_servers_displayed' => $config->settings['NavigationDisplayServers'], - 'servers' => $config->settings['Servers'], + 'auth_type' => $this->config->selectedServer['auth_type'], + 'is_servers_displayed' => $this->config->settings['NavigationDisplayServers'], + 'servers' => $this->config->settings['Servers'], 'server_select' => $serverSelect ?? '', 'navigation_tree' => $navRender, 'is_navigation_settings_enabled' => ! defined('PMA_DISABLE_NAVI_SETTINGS'), 'navigation_settings' => $navigationSettings ?? '', - 'is_drag_drop_import_enabled' => $config->settings['enable_drag_drop_import'] === true, + 'is_drag_drop_import_enabled' => $this->config->settings['enable_drag_drop_import'] === true, 'is_mariadb' => $this->dbi->isMariaDB(), ]); } @@ -159,7 +162,7 @@ class Navigation $sqlQuery = 'INSERT INTO ' . $navTable . '(`username`, `item_name`, `item_type`, `db_name`, `table_name`)' . ' VALUES (' - . $this->dbi->quoteString(Config::getInstance()->selectedServer['user'], ConnectionType::ControlUser) . ',' + . $this->dbi->quoteString($this->config->selectedServer['user'], ConnectionType::ControlUser) . ',' . $this->dbi->quoteString($itemName, ConnectionType::ControlUser) . ',' . $this->dbi->quoteString($itemType, ConnectionType::ControlUser) . ',' . $this->dbi->quoteString($dbName, ConnectionType::ControlUser) . ',' @@ -190,7 +193,7 @@ class Navigation $sqlQuery = 'DELETE FROM ' . $navTable . ' WHERE' . ' `username`=' - . $this->dbi->quoteString(Config::getInstance()->selectedServer['user'], ConnectionType::ControlUser) + . $this->dbi->quoteString($this->config->selectedServer['user'], ConnectionType::ControlUser) . ' AND `item_name`=' . $this->dbi->quoteString($itemName, ConnectionType::ControlUser) . ' AND `item_type`=' . $this->dbi->quoteString($itemType, ConnectionType::ControlUser) . ' AND `db_name`=' . $this->dbi->quoteString($dbName, ConnectionType::ControlUser); @@ -236,7 +239,7 @@ class Navigation . '.' . Util::backquote($navigationItemsHidingFeature->navigationHiding); $sqlQuery = 'SELECT `item_name`, `item_type` FROM ' . $navTable . ' WHERE `username`=' - . $this->dbi->quoteString(Config::getInstance()->selectedServer['user'], ConnectionType::ControlUser) + . $this->dbi->quoteString($this->config->selectedServer['user'], ConnectionType::ControlUser) . ' AND `db_name`=' . $this->dbi->quoteString($database, ConnectionType::ControlUser) . " AND `table_name`=''"; $result = $this->dbi->tryQueryAsControlUser($sqlQuery); diff --git a/tests/classes/Controllers/NavigationControllerTest.php b/tests/classes/Controllers/NavigationControllerTest.php index 6573c7acb9..daf89c4ef6 100644 --- a/tests/classes/Controllers/NavigationControllerTest.php +++ b/tests/classes/Controllers/NavigationControllerTest.php @@ -123,7 +123,7 @@ class NavigationControllerTest extends AbstractTestCase $navigationController = new NavigationController( $responseRenderer, $template, - new Navigation($template, $relation, $this->dbi), + new Navigation($template, $relation, $this->dbi, $config), $relation, new PageSettings(new UserPreferences($this->dbi, $relation, $template)), ); @@ -273,7 +273,7 @@ class NavigationControllerTest extends AbstractTestCase $navigationController = new NavigationController( $responseRenderer, $template, - new Navigation($template, $relation, $this->dbi), + new Navigation($template, $relation, $this->dbi, $config), $relation, new PageSettings(new UserPreferences($this->dbi, $relation, $template)), ); diff --git a/tests/classes/Navigation/NavigationTest.php b/tests/classes/Navigation/NavigationTest.php index fe6123358b..59b401f89e 100644 --- a/tests/classes/Navigation/NavigationTest.php +++ b/tests/classes/Navigation/NavigationTest.php @@ -46,11 +46,7 @@ class NavigationTest extends AbstractTestCase ]); (new ReflectionProperty(Relation::class, 'cache'))->setValue(null, $relationParameters); - $this->object = new Navigation( - new Template(), - new Relation($dbi), - $dbi, - ); + $this->object = new Navigation(new Template(), new Relation($dbi), $dbi, $config); } /** @@ -81,7 +77,7 @@ class NavigationTest extends AbstractTestCase ->willReturnCallback(static fn (string $string): string => "'" . $string . "'"); DatabaseInterface::$instance = $dbi; - $this->object = new Navigation(new Template(), new Relation($dbi), $dbi); + $this->object = new Navigation(new Template(), new Relation($dbi), $dbi, Config::getInstance()); $this->object->hideNavigationItem('itemName', 'itemType', 'db'); } @@ -103,7 +99,7 @@ class NavigationTest extends AbstractTestCase $dbi->expects(self::any())->method('quoteString') ->willReturnCallback(static fn (string $string): string => "'" . $string . "'"); DatabaseInterface::$instance = $dbi; - $this->object = new Navigation(new Template(), new Relation($dbi), $dbi); + $this->object = new Navigation(new Template(), new Relation($dbi), $dbi, Config::getInstance()); $this->object->unhideNavigationItem('itemName', 'itemType', 'db'); }