Skip to content

Commit 8bbb1ed

Browse files
committed
Remove ThemeManager::getInstance() method
Remove singleton from the Theme\ThemeManager class and uses DI where possible. Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 3f09bc2 commit 8bbb1ed

14 files changed

Lines changed: 59 additions & 72 deletions

libraries/classes/Common.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,14 @@ public static function run(bool $isSetupPage = false): void
191191
$GLOBALS['cfg'] = $config->settings;
192192
$settings = $config->getSettings();
193193

194-
/* setup themes LABEL_theme_setup */
195-
196-
$GLOBALS['theme'] = ThemeManager::initializeTheme();
194+
/** @var ThemeManager $themeManager */
195+
$themeManager = $container->get(ThemeManager::class);
196+
$GLOBALS['theme'] = $themeManager->initializeTheme();
197197

198198
$GLOBALS['dbi'] = null;
199199

200200
if ($isMinimumCommon) {
201-
$config->loadUserPreferences(true);
202-
$container->set('theme_manager', ThemeManager::getInstance());
201+
$config->loadUserPreferences($themeManager, true);
203202
Tracker::enable();
204203

205204
if ($route === '/url') {
@@ -223,7 +222,7 @@ public static function run(bool $isSetupPage = false): void
223222
*/
224223
$config->setCookie('pma_lang', (string) $GLOBALS['lang']);
225224

226-
ThemeManager::getInstance()->setThemeCookie();
225+
$themeManager->setThemeCookie();
227226

228227
$GLOBALS['dbi'] = DatabaseInterface::load();
229228
$container->set(DatabaseInterface::class, $GLOBALS['dbi']);
@@ -306,9 +305,7 @@ public static function run(bool $isSetupPage = false): void
306305
$container->set('response', ResponseRenderer::getInstance());
307306

308307
// load user preferences
309-
$config->loadUserPreferences();
310-
311-
$container->set('theme_manager', ThemeManager::getInstance());
308+
$config->loadUserPreferences($themeManager);
312309

313310
/* Tell tracker that it can actually work */
314311
Tracker::enable();

libraries/classes/Config.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ private function setConnectionCollation(): void
399399
* Loads user preferences and merges them with current config
400400
* must be called after control connection has been established
401401
*/
402-
public function loadUserPreferences(bool $isMinimumCommon = false): void
402+
public function loadUserPreferences(ThemeManager $themeManager, bool $isMinimumCommon = false): void
403403
{
404404
// index.php should load these settings, so that phpmyadmin.css.php
405405
// will have everything available in session cache
@@ -445,29 +445,28 @@ public function loadUserPreferences(bool $isMinimumCommon = false): void
445445
// in frames
446446

447447
// save theme
448-
$tmanager = ThemeManager::getInstance();
449-
if ($tmanager->getThemeCookie() || isset($_REQUEST['set_theme'])) {
448+
if ($themeManager->getThemeCookie() || isset($_REQUEST['set_theme'])) {
450449
if (
451450
(! isset($configData['ThemeDefault'])
452-
&& $tmanager->theme->getId() !== 'original')
451+
&& $themeManager->theme->getId() !== 'original')
453452
|| isset($configData['ThemeDefault'])
454-
&& $configData['ThemeDefault'] != $tmanager->theme->getId()
453+
&& $configData['ThemeDefault'] != $themeManager->theme->getId()
455454
) {
456455
$this->setUserValue(
457456
null,
458457
'ThemeDefault',
459-
$tmanager->theme->getId(),
458+
$themeManager->theme->getId(),
460459
'original',
461460
);
462461
}
463462
} else {
464463
// no cookie - read default from settings
465464
if (
466-
$this->settings['ThemeDefault'] != $tmanager->theme->getId()
467-
&& $tmanager->checkTheme($this->settings['ThemeDefault'])
465+
$this->settings['ThemeDefault'] != $themeManager->theme->getId()
466+
&& $themeManager->checkTheme($this->settings['ThemeDefault'])
468467
) {
469-
$tmanager->setActiveTheme($this->settings['ThemeDefault']);
470-
$tmanager->setThemeCookie();
468+
$themeManager->setActiveTheme($this->settings['ThemeDefault']);
469+
$themeManager->setThemeCookie();
471470
}
472471
}
473472

libraries/classes/Controllers/Preferences/ExportController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpMyAdmin\Http\ServerRequest;
1313
use PhpMyAdmin\ResponseRenderer;
1414
use PhpMyAdmin\Template;
15+
use PhpMyAdmin\Theme\ThemeManager;
1516
use PhpMyAdmin\TwoFactor;
1617
use PhpMyAdmin\Url;
1718
use PhpMyAdmin\UserPreferences;
@@ -27,6 +28,7 @@ public function __construct(
2728
private UserPreferences $userPreferences,
2829
private Relation $relation,
2930
private Config $config,
31+
private ThemeManager $themeManager,
3032
) {
3133
parent::__construct($response, $template);
3234
}
@@ -62,7 +64,7 @@ public function __invoke(ServerRequest $request): void
6264
$twoFactor->save();
6365
if ($result === true) {
6466
// reload config
65-
$this->config->loadUserPreferences();
67+
$this->config->loadUserPreferences($this->themeManager);
6668
$GLOBALS['tabHash'] = $request->getParsedBodyParam('tab_hash');
6769
$GLOBALS['hash'] = ltrim($GLOBALS['tabHash'], '#');
6870
$this->userPreferences->redirect('index.php?route=/preferences/export', null, $GLOBALS['hash']);

libraries/classes/Controllers/Preferences/FeaturesController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpMyAdmin\Http\ServerRequest;
1313
use PhpMyAdmin\ResponseRenderer;
1414
use PhpMyAdmin\Template;
15+
use PhpMyAdmin\Theme\ThemeManager;
1516
use PhpMyAdmin\TwoFactor;
1617
use PhpMyAdmin\Url;
1718
use PhpMyAdmin\UserPreferences;
@@ -27,6 +28,7 @@ public function __construct(
2728
private UserPreferences $userPreferences,
2829
private Relation $relation,
2930
private Config $config,
31+
private ThemeManager $themeManager,
3032
) {
3133
parent::__construct($response, $template);
3234
}
@@ -62,7 +64,7 @@ public function __invoke(ServerRequest $request): void
6264
$twoFactor->save();
6365
if ($result === true) {
6466
// reload config
65-
$this->config->loadUserPreferences();
67+
$this->config->loadUserPreferences($this->themeManager);
6668
$GLOBALS['tabHash'] = $request->getParsedBodyParam('tab_hash');
6769
$GLOBALS['hash'] = ltrim($GLOBALS['tabHash'], '#');
6870
$this->userPreferences->redirect('index.php?route=/preferences/features', null, $GLOBALS['hash']);

libraries/classes/Controllers/Preferences/ImportController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpMyAdmin\Http\ServerRequest;
1313
use PhpMyAdmin\ResponseRenderer;
1414
use PhpMyAdmin\Template;
15+
use PhpMyAdmin\Theme\ThemeManager;
1516
use PhpMyAdmin\TwoFactor;
1617
use PhpMyAdmin\Url;
1718
use PhpMyAdmin\UserPreferences;
@@ -27,6 +28,7 @@ public function __construct(
2728
private UserPreferences $userPreferences,
2829
private Relation $relation,
2930
private Config $config,
31+
private ThemeManager $themeManager,
3032
) {
3133
parent::__construct($response, $template);
3234
}
@@ -62,7 +64,7 @@ public function __invoke(ServerRequest $request): void
6264
$twoFactor->save();
6365
if ($result === true) {
6466
// reload config
65-
$this->config->loadUserPreferences();
67+
$this->config->loadUserPreferences($this->themeManager);
6668
$GLOBALS['tabHash'] = $request->getParsedBodyParam('tab_hash');
6769
$GLOBALS['hash'] = ltrim($GLOBALS['tabHash'], '#');
6870
$this->userPreferences->redirect('index.php?route=/preferences/import', null, $GLOBALS['hash']);

libraries/classes/Controllers/Preferences/MainPanelController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpMyAdmin\Http\ServerRequest;
1313
use PhpMyAdmin\ResponseRenderer;
1414
use PhpMyAdmin\Template;
15+
use PhpMyAdmin\Theme\ThemeManager;
1516
use PhpMyAdmin\TwoFactor;
1617
use PhpMyAdmin\Url;
1718
use PhpMyAdmin\UserPreferences;
@@ -27,6 +28,7 @@ public function __construct(
2728
private UserPreferences $userPreferences,
2829
private Relation $relation,
2930
private Config $config,
31+
private ThemeManager $themeManager,
3032
) {
3133
parent::__construct($response, $template);
3234
}
@@ -62,7 +64,7 @@ public function __invoke(ServerRequest $request): void
6264
$twoFactor->save();
6365
if ($result === true) {
6466
// reload config
65-
$this->config->loadUserPreferences();
67+
$this->config->loadUserPreferences($this->themeManager);
6668
$GLOBALS['tabHash'] = $request->getParsedBodyParam('tab_hash');
6769
$GLOBALS['hash'] = ltrim($GLOBALS['tabHash'], '#');
6870
$this->userPreferences->redirect('index.php?route=/preferences/main-panel', null, $GLOBALS['hash']);

libraries/classes/Controllers/Preferences/ManageController.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function __construct(
5151
private UserPreferences $userPreferences,
5252
private Relation $relation,
5353
private Config $config,
54+
private ThemeManager $themeManager,
5455
) {
5556
parent::__construct($response, $template);
5657
}
@@ -190,14 +191,13 @@ public function __invoke(ServerRequest $request): void
190191

191192
// check for ThemeDefault
192193
$redirectParams = [];
193-
$tmanager = ThemeManager::getInstance();
194194
if (
195195
isset($configuration['ThemeDefault'])
196-
&& $tmanager->theme->getId() != $configuration['ThemeDefault']
197-
&& $tmanager->checkTheme($configuration['ThemeDefault'])
196+
&& $this->themeManager->theme->getId() != $configuration['ThemeDefault']
197+
&& $this->themeManager->checkTheme($configuration['ThemeDefault'])
198198
) {
199-
$tmanager->setActiveTheme($configuration['ThemeDefault']);
200-
$tmanager->setThemeCookie();
199+
$this->themeManager->setActiveTheme($configuration['ThemeDefault']);
200+
$this->themeManager->setThemeCookie();
201201
}
202202

203203
if (isset($configuration['lang']) && $configuration['lang'] != $GLOBALS['lang']) {
@@ -225,7 +225,7 @@ public function __invoke(ServerRequest $request): void
225225
}
226226

227227
// reload config
228-
$this->config->loadUserPreferences();
228+
$this->config->loadUserPreferences($this->themeManager);
229229
$this->userPreferences->redirect($GLOBALS['return_url'] ?? '', $redirectParams);
230230

231231
return;

libraries/classes/Controllers/Preferences/NavigationController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpMyAdmin\Http\ServerRequest;
1313
use PhpMyAdmin\ResponseRenderer;
1414
use PhpMyAdmin\Template;
15+
use PhpMyAdmin\Theme\ThemeManager;
1516
use PhpMyAdmin\TwoFactor;
1617
use PhpMyAdmin\Url;
1718
use PhpMyAdmin\UserPreferences;
@@ -27,6 +28,7 @@ public function __construct(
2728
private UserPreferences $userPreferences,
2829
private Relation $relation,
2930
private Config $config,
31+
private ThemeManager $themeManager,
3032
) {
3133
parent::__construct($response, $template);
3234
}
@@ -62,7 +64,7 @@ public function __invoke(ServerRequest $request): void
6264
$twoFactor->save();
6365
if ($result === true) {
6466
// reload config
65-
$this->config->loadUserPreferences();
67+
$this->config->loadUserPreferences($this->themeManager);
6668
$GLOBALS['tabHash'] = $request->getParsedBodyParam('tab_hash');
6769
$GLOBALS['hash'] = ltrim($GLOBALS['tabHash'], '#');
6870
$this->userPreferences->redirect('index.php?route=/preferences/navigation', null, $GLOBALS['hash']);

libraries/classes/Controllers/Preferences/SqlController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpMyAdmin\Http\ServerRequest;
1313
use PhpMyAdmin\ResponseRenderer;
1414
use PhpMyAdmin\Template;
15+
use PhpMyAdmin\Theme\ThemeManager;
1516
use PhpMyAdmin\TwoFactor;
1617
use PhpMyAdmin\Url;
1718
use PhpMyAdmin\UserPreferences;
@@ -27,6 +28,7 @@ public function __construct(
2728
private UserPreferences $userPreferences,
2829
private Relation $relation,
2930
private Config $config,
31+
private ThemeManager $themeManager,
3032
) {
3133
parent::__construct($response, $template);
3234
}
@@ -62,7 +64,7 @@ public function __invoke(ServerRequest $request): void
6264
$twoFactor->save();
6365
if ($result === true) {
6466
// reload config
65-
$this->config->loadUserPreferences();
67+
$this->config->loadUserPreferences($this->themeManager);
6668
$GLOBALS['tabHash'] = $request->getParsedBodyParam('tab_hash');
6769
$GLOBALS['hash'] = ltrim($GLOBALS['tabHash'], '#');
6870
$this->userPreferences->redirect('index.php?route=/preferences/sql', null, $GLOBALS['hash']);

libraries/classes/Theme/ThemeManager.php

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626
*/
2727
class ThemeManager
2828
{
29-
/**
30-
* ThemeManager instance
31-
*/
32-
private static ThemeManager|null $instance = null;
33-
3429
/** @var string file-system path to the theme folder */
3530
private string $themesPath;
3631

@@ -46,7 +41,7 @@ class ThemeManager
4641
public bool $perServer = false;
4742

4843
/** @var string name of active theme */
49-
public string $activeTheme = '';
44+
public string $activeTheme;
5045

5146
/** @var Theme Theme active theme */
5247
public Theme $theme;
@@ -58,18 +53,19 @@ class ThemeManager
5853

5954
public function __construct()
6055
{
61-
$this->themes = [];
6256
$this->themeDefault = self::FALLBACK_THEME;
63-
$this->activeTheme = '';
57+
$this->activeTheme = $this->themeDefault;
6458
$this->themesPath = self::getThemesFsDir();
6559
$this->themesPathUrl = self::getThemesDir();
60+
$this->theme = new Theme();
61+
}
6662

63+
public function initializeTheme(): Theme
64+
{
6765
$this->setThemePerServer($GLOBALS['cfg']['ThemePerServer']);
6866

6967
$this->loadThemes();
7068

71-
$this->theme = new Theme();
72-
7369
$configThemeExists = $this->checkTheme($GLOBALS['cfg']['ThemeDefault']);
7470
if (! $configThemeExists) {
7571
trigger_error(
@@ -94,20 +90,12 @@ public function __construct()
9490
$this->theme->setColorMode($colorMode);
9591
}
9692

97-
return;
93+
return $this->theme;
9894
}
9995

10096
$this->setActiveTheme(self::FALLBACK_THEME);
101-
}
10297

103-
/**
104-
* Returns the singleton ThemeManager object
105-
*
106-
* @return ThemeManager The instance
107-
*/
108-
public static function getInstance(): ThemeManager
109-
{
110-
return self::$instance ??= new ThemeManager();
98+
return $this->theme;
11199
}
112100

113101
/**
@@ -286,13 +274,6 @@ public function getThemesArray(): array
286274
return $themes;
287275
}
288276

289-
public static function initializeTheme(): Theme
290-
{
291-
$themeManager = self::getInstance();
292-
293-
return $themeManager->theme;
294-
}
295-
296277
/**
297278
* Return the themes directory with a trailing slash
298279
*/

0 commit comments

Comments
 (0)