Skip to content

Commit 7e9ce8d

Browse files
Merge pull request #18616 from MauricioFauth/server-request-route
Refactor the `ServerRequest::getRoute()` method
2 parents e9be991 + 93fa48e commit 7e9ce8d

13 files changed

Lines changed: 145 additions & 58 deletions

File tree

libraries/classes/Application.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@
6161

6262
class Application
6363
{
64-
private static ServerRequest|null $request = null;
65-
6664
public function __construct(
6765
private readonly ErrorHandler $errorHandler,
6866
private readonly Config $config,
@@ -91,7 +89,9 @@ public function run(bool $isSetupPage = false): void
9189
$runner = new RequestHandlerRunner(
9290
$requestHandler,
9391
new SapiEmitter(),
94-
static fn (): ServerRequestInterface => self::getRequest()->withAttribute('isSetupPage', $isSetupPage),
92+
static function () use ($isSetupPage): ServerRequestInterface {
93+
return ServerRequestFactory::create()->fromGlobals()->withAttribute('isSetupPage', $isSetupPage);
94+
},
9595
function (Throwable $throwable): ResponseInterface {
9696
$response = $this->responseFactory->createResponse(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR);
9797
$response->getBody()->write(sprintf('An error occurred: %s', $throwable->getMessage()));
@@ -126,6 +126,7 @@ public function handle(ServerRequest $request): Response|null
126126
}
127127

128128
$route = $request->getRoute();
129+
Routing::$route = $route;
129130

130131
$isMinimumCommon = $isSetupPage || $route === '/import-status' || $route === '/url' || $route === '/messages';
131132

@@ -567,15 +568,6 @@ private function connectToDatabaseServer(
567568
$dbi->connect($currentServer, Connection::TYPE_USER, Connection::TYPE_CONTROL);
568569
}
569570

570-
public static function getRequest(): ServerRequest
571-
{
572-
if (self::$request === null) {
573-
self::$request = ServerRequestFactory::create()->fromGlobals();
574-
}
575-
576-
return self::$request;
577-
}
578-
579571
private function setupPageBootstrap(Config $config): void
580572
{
581573
// use default error handler

libraries/classes/Controllers/HomeController.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44

55
namespace PhpMyAdmin\Controllers;
66

7+
use Fig\Http\Message\RequestMethodInterface;
8+
use Fig\Http\Message\StatusCodeInterface;
79
use PhpMyAdmin\Charsets;
810
use PhpMyAdmin\CheckUserPrivileges;
911
use PhpMyAdmin\Config;
1012
use PhpMyAdmin\ConfigStorage\Relation;
1113
use PhpMyAdmin\DatabaseInterface;
1214
use PhpMyAdmin\Git;
1315
use PhpMyAdmin\Html\Generator;
16+
use PhpMyAdmin\Http\Factory\ResponseFactory;
17+
use PhpMyAdmin\Http\Response;
1418
use PhpMyAdmin\Http\ServerRequest;
19+
use PhpMyAdmin\Identifiers\DatabaseName;
20+
use PhpMyAdmin\Identifiers\TableName;
1521
use PhpMyAdmin\LanguageManager;
1622
use PhpMyAdmin\Message;
1723
use PhpMyAdmin\RecentFavoriteTable;
@@ -50,19 +56,24 @@ public function __construct(
5056
private Config $config,
5157
private ThemeManager $themeManager,
5258
private DatabaseInterface $dbi,
59+
private readonly ResponseFactory $responseFactory,
5360
) {
5461
parent::__construct($response, $template);
5562
}
5663

57-
public function __invoke(ServerRequest $request): void
64+
public function __invoke(ServerRequest $request): Response|null
5865
{
66+
if ($this->shouldRedirectToDatabaseOrTablePage($request)) {
67+
return $this->redirectToDatabaseOrTablePage($request);
68+
}
69+
5970
$GLOBALS['server'] ??= null;
6071
$GLOBALS['message'] ??= null;
6172
$GLOBALS['show_query'] ??= null;
6273
$GLOBALS['errorUrl'] ??= null;
6374

6475
if ($request->isAjax() && ! empty($_REQUEST['access_time'])) {
65-
return;
76+
return null;
6677
}
6778

6879
$this->addScriptFiles(['home.js']);
@@ -239,6 +250,8 @@ public function __invoke(ServerRequest $request): void
239250
'themes' => $this->themeManager->getThemesArray(),
240251
'errors' => $this->errors,
241252
]);
253+
254+
return null;
242255
}
243256

244257
private function checkRequirements(): void
@@ -460,4 +473,29 @@ private function checkPhpExtensionsRequirements(): void
460473
'severity' => 'notice',
461474
];
462475
}
476+
477+
/** @see https://docs.phpmyadmin.net/en/latest/faq.html#faq1-34 phpMyAdmin FAQ 1.34 */
478+
private function shouldRedirectToDatabaseOrTablePage(ServerRequest $request): bool
479+
{
480+
return ! $request->isAjax()
481+
&& $request->getMethod() === RequestMethodInterface::METHOD_GET
482+
&& $request->hasQueryParam('db')
483+
&& DatabaseName::tryFrom($request->getQueryParam('db')) !== null;
484+
}
485+
486+
/** @see https://docs.phpmyadmin.net/en/latest/faq.html#faq1-34 phpMyAdmin FAQ 1.34 */
487+
private function redirectToDatabaseOrTablePage(ServerRequest $request): Response
488+
{
489+
$db = DatabaseName::from($request->getQueryParam('db'));
490+
$table = TableName::tryFrom($request->getQueryParam('table'));
491+
$route = '/database/structure';
492+
$params = ['db' => $db->getName()];
493+
if ($table !== null) {
494+
$route = '/sql';
495+
$params['table'] = $table->getName();
496+
}
497+
498+
return $this->responseFactory->createResponse(StatusCodeInterface::STATUS_FOUND)
499+
->withHeader('Location', './index.php?route=' . $route . Url::getCommonRaw($params, '&'));
500+
}
463501
}

libraries/classes/DatabaseInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PhpMyAdmin\Query\Compatibility;
2020
use PhpMyAdmin\Query\Generator as QueryGenerator;
2121
use PhpMyAdmin\Query\Utilities;
22+
use PhpMyAdmin\Routing\Routing;
2223
use PhpMyAdmin\SqlParser\Context;
2324
use PhpMyAdmin\Tracking\Tracker;
2425
use PhpMyAdmin\Utils\SessionCache;
@@ -225,7 +226,7 @@ public function tryQuery(
225226
sprintf(
226227
'SQL[%s?route=%s]: %0.3f(W:%d,C:%s,L:0x%02X) > %s',
227228
basename($_SERVER['SCRIPT_NAME']),
228-
Application::getRequest()->getRoute(),
229+
Routing::$route,
229230
$this->lastQueryExecutionTime,
230231
$this->getWarningCount($connectionType),
231232
$cacheAffectedRows ? 'y' : 'n',

libraries/classes/Footer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace PhpMyAdmin;
99

1010
use PhpMyAdmin\ConfigStorage\Relation;
11+
use PhpMyAdmin\Routing\Routing;
1112
use Traversable;
1213

1314
use function basename;
@@ -126,7 +127,7 @@ public function getSelfUrl(): string
126127
$GLOBALS['server'] ??= null;
127128

128129
$params = [];
129-
$params['route'] = Application::getRequest()->getRoute();
130+
$params['route'] = Routing::$route;
130131

131132
if (isset($GLOBALS['db']) && strlen($GLOBALS['db']) > 0) {
132133
$params['db'] = $GLOBALS['db'];

libraries/classes/Http/ServerRequest.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -274,29 +274,9 @@ public function isPost(): bool
274274
/** @psalm-return non-empty-string */
275275
public function getRoute(): string
276276
{
277-
$getParams = $this->getQueryParams();
278-
$postParams = $this->getParsedBody();
279-
$route = '/';
280-
if (isset($getParams['route']) && is_string($getParams['route']) && $getParams['route'] !== '') {
281-
$route = $getParams['route'];
282-
} elseif (
283-
is_array($postParams)
284-
&& isset($postParams['route'])
285-
&& is_string($postParams['route'])
286-
&& $postParams['route'] !== ''
287-
) {
288-
$route = $postParams['route'];
289-
}
290-
291-
/**
292-
* See FAQ 1.34.
293-
*
294-
* @see https://docs.phpmyadmin.net/en/latest/faq.html#faq1-34
295-
*/
296-
$db = isset($getParams['db']) && is_string($getParams['db']) ? $getParams['db'] : '';
297-
if ($route === '/' && $db !== '') {
298-
$table = isset($getParams['table']) && is_string($getParams['table']) ? $getParams['table'] : '';
299-
$route = $table === '' ? '/database/structure' : '/sql';
277+
$route = $this->getQueryParam('route') ?? $this->getParsedBodyParam('route');
278+
if (! is_string($route) || $route === '') {
279+
return '/';
300280
}
301281

302282
return $route;

libraries/classes/Menu.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpMyAdmin\ConfigStorage\Relation;
1111
use PhpMyAdmin\Dbal\Connection;
1212
use PhpMyAdmin\Query\Utilities;
13+
use PhpMyAdmin\Routing\Routing;
1314
use PhpMyAdmin\Tracking\Tracker;
1415
use PhpMyAdmin\Utils\SessionCache;
1516

@@ -208,7 +209,7 @@ private function getBreadcrumbs(): string
208209
*/
209210
private function getTableTabs(): array
210211
{
211-
$route = Application::getRequest()->getRoute();
212+
$route = Routing::$route;
212213

213214
$isSystemSchema = Utilities::isSystemSchema($this->db);
214215
$tableIsView = $this->dbi->getTable($this->db, $this->table)
@@ -320,7 +321,7 @@ private function getTableTabs(): array
320321
*/
321322
private function getDbTabs(): array
322323
{
323-
$route = Application::getRequest()->getRoute();
324+
$route = Routing::$route;
324325

325326
$isSystemSchema = Utilities::isSystemSchema($this->db);
326327
$numTables = count($this->dbi->getTables($this->db));
@@ -435,7 +436,7 @@ private function getDbTabs(): array
435436
*/
436437
private function getServerTabs(): array
437438
{
438-
$route = Application::getRequest()->getRoute();
439+
$route = Routing::$route;
439440

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

libraries/classes/Plugins/Auth/AuthenticationCookie.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
namespace PhpMyAdmin\Plugins\Auth;
99

10-
use PhpMyAdmin\Application;
1110
use PhpMyAdmin\Config;
1211
use PhpMyAdmin\Core;
1312
use PhpMyAdmin\Exceptions\SessionHandlerException;
1413
use PhpMyAdmin\LanguageManager;
1514
use PhpMyAdmin\Message;
1615
use PhpMyAdmin\Plugins\AuthenticationPlugin;
1716
use PhpMyAdmin\ResponseRenderer;
17+
use PhpMyAdmin\Routing\Routing;
1818
use PhpMyAdmin\Server\Select;
1919
use PhpMyAdmin\Session;
2020
use PhpMyAdmin\Template;
@@ -142,7 +142,7 @@ public function showLoginForm(): never
142142
}
143143

144144
$formParams = [];
145-
$formParams['route'] = Application::getRequest()->getRoute();
145+
$formParams['route'] = Routing::$route;
146146

147147
if (strlen($GLOBALS['db'])) {
148148
$formParams['db'] = $GLOBALS['db'];
@@ -462,7 +462,7 @@ public function rememberCredentials(): void
462462

463463
// any parameters to pass?
464464
$urlParams = [];
465-
$urlParams['route'] = Application::getRequest()->getRoute();
465+
$urlParams['route'] = Routing::$route;
466466

467467
if (strlen($GLOBALS['db']) > 0) {
468468
$urlParams['db'] = $GLOBALS['db'];

libraries/classes/Routing/Routing.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151
*/
5252
class Routing
5353
{
54+
/**
55+
* @deprecated Use {@see ServerRequest::getRoute()} instead.
56+
*
57+
* @psalm-var non-empty-string
58+
*/
59+
public static string $route = '/';
60+
5461
public const ROUTES_CACHE_FILE = CACHE_DIR . 'routes.cache.php';
5562

5663
public static function skipCache(): bool

libraries/services_controllers.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@
468468
'$config' => '@config',
469469
'$themeManager' => '@' . ThemeManager::class,
470470
'$dbi' => '@dbi',
471+
'$responseFactory' => '@' . ResponseFactory::class,
471472
],
472473
],
473474
Import\ImportController::class => [

psalm-baseline.xml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
</MixedAssignment>
1818
</file>
1919
<file src="libraries/classes/Application.php">
20+
<DeprecatedProperty>
21+
<code>Routing::$route</code>
22+
</DeprecatedProperty>
2023
<InvalidArrayOffset>
2124
<code><![CDATA[$GLOBALS['back']]]></code>
2225
</InvalidArrayOffset>
@@ -2040,9 +2043,6 @@
20402043
<PossiblyNullArrayOffset>
20412044
<code><![CDATA[$GLOBALS['language_stats']]]></code>
20422045
</PossiblyNullArrayOffset>
2043-
<PossiblyUnusedMethod>
2044-
<code>__construct</code>
2045-
</PossiblyUnusedMethod>
20462046
<RedundantCondition>
20472047
<code><![CDATA[is_string($GLOBALS['cfg']['blowfish_secret'])]]></code>
20482048
</RedundantCondition>
@@ -5270,6 +5270,9 @@
52705270
<code>escapeString</code>
52715271
<code>escapeString</code>
52725272
</DeprecatedMethod>
5273+
<DeprecatedProperty>
5274+
<code>Routing::$route</code>
5275+
</DeprecatedProperty>
52735276
<InvalidOperand>
52745277
<code><![CDATA[$row['Data_free']]]></code>
52755278
<code><![CDATA[$row['Data_length']]]></code>
@@ -6199,6 +6202,9 @@
61996202
</MixedOperand>
62006203
</file>
62016204
<file src="libraries/classes/Footer.php">
6205+
<DeprecatedProperty>
6206+
<code>Routing::$route</code>
6207+
</DeprecatedProperty>
62026208
<InvalidArgument>
62036209
<code>$params</code>
62046210
</InvalidArgument>
@@ -7225,6 +7231,11 @@
72257231
</MixedOperand>
72267232
</file>
72277233
<file src="libraries/classes/Menu.php">
7234+
<DeprecatedProperty>
7235+
<code>Routing::$route</code>
7236+
<code>Routing::$route</code>
7237+
<code>Routing::$route</code>
7238+
</DeprecatedProperty>
72287239
<MixedAssignment>
72297240
<code>$binaryLogs</code>
72307241
</MixedAssignment>
@@ -8011,6 +8022,10 @@
80118022
</InvalidArrayOffset>
80128023
</file>
80138024
<file src="libraries/classes/Plugins/Auth/AuthenticationCookie.php">
8025+
<DeprecatedProperty>
8026+
<code>Routing::$route</code>
8027+
<code>Routing::$route</code>
8028+
</DeprecatedProperty>
80148029
<DocblockTypeContradiction>
80158030
<code>$resp == null</code>
80168031
</DocblockTypeContradiction>

0 commit comments

Comments
 (0)