Skip to content

Commit d2a5202

Browse files
Merge pull request #18636 from MauricioFauth/route-variables
Pass route variables as a ServerRequest attribute
2 parents 5ec8f42 + 5e00948 commit d2a5202

8 files changed

Lines changed: 75 additions & 63 deletions

File tree

libraries/classes/Controllers/Server/ShowEngineController.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,57 @@
1212
use PhpMyAdmin\Template;
1313
use PhpMyAdmin\Url;
1414

15+
use function is_array;
16+
use function is_string;
17+
1518
/**
1619
* Displays details about a given Storage Engine.
1720
*/
1821
final class ShowEngineController extends AbstractController
1922
{
23+
private string $engine = '';
24+
private string $page = '';
25+
2026
public function __construct(ResponseRenderer $response, Template $template, private DatabaseInterface $dbi)
2127
{
2228
parent::__construct($response, $template);
2329
}
2430

25-
/**
26-
* @param array $params
27-
* @psalm-param array{engine: string, page?: string} $params
28-
*/
29-
public function __invoke(ServerRequest $request, array $params): void
31+
public function __invoke(ServerRequest $request): void
3032
{
33+
$this->setEngineAndPageProperties($request->getAttribute('routeVars'));
34+
3135
$GLOBALS['errorUrl'] = Url::getFromRoute('/');
3236

3337
if ($this->dbi->isSuperUser()) {
3438
$this->dbi->selectDb('mysql');
3539
}
3640

37-
$page = $params['page'] ?? '';
38-
3941
$engine = [];
40-
if (StorageEngine::isValid($params['engine'])) {
41-
$storageEngine = StorageEngine::getEngine($params['engine']);
42+
if (StorageEngine::isValid($this->engine)) {
43+
$storageEngine = StorageEngine::getEngine($this->engine);
4244
$engine = [
43-
'engine' => $params['engine'],
45+
'engine' => $this->engine,
4446
'title' => $storageEngine->getTitle(),
4547
'help_page' => $storageEngine->getMysqlHelpPage(),
4648
'comment' => $storageEngine->getComment(),
4749
'info_pages' => $storageEngine->getInfoPages(),
4850
'support' => $storageEngine->getSupportInformationMessage(),
4951
'variables' => $storageEngine->getHtmlVariables(),
50-
'page' => ! empty($page) ? $storageEngine->getPage($page) : '',
52+
'page' => $this->page !== '' ? $storageEngine->getPage($this->page) : '',
5153
];
5254
}
5355

54-
$this->render('server/engines/show', ['engine' => $engine, 'page' => $page]);
56+
$this->render('server/engines/show', ['engine' => $engine, 'page' => $this->page]);
57+
}
58+
59+
private function setEngineAndPageProperties(mixed $routeVars): void
60+
{
61+
if (! is_array($routeVars)) {
62+
return;
63+
}
64+
65+
$this->engine = isset($routeVars['engine']) && is_string($routeVars['engine']) ? $routeVars['engine'] : '';
66+
$this->page = isset($routeVars['page']) && is_string($routeVars['page']) ? $routeVars['page'] : '';
5567
}
5668
}

libraries/classes/Controllers/Server/Status/Processes/KillController.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use PhpMyAdmin\Template;
1414

1515
use function __;
16+
use function is_array;
17+
use function is_numeric;
1618

1719
final class KillController extends AbstractController
1820
{
@@ -25,15 +27,14 @@ public function __construct(
2527
parent::__construct($response, $template, $data);
2628
}
2729

28-
/** @param mixed[] $params Request parameters */
29-
public function __invoke(ServerRequest $request, array $params): void
30+
public function __invoke(ServerRequest $request): void
3031
{
3132
if (! $request->isAjax()) {
3233
return;
3334
}
3435

35-
$kill = (int) $params['id'];
36-
$query = $this->dbi->getKillQuery($kill);
36+
$processId = $this->getProcessId($request->getAttribute('routeVars'));
37+
$query = $this->dbi->getKillQuery($processId);
3738

3839
if ($this->dbi->tryQuery($query)) {
3940
$message = Message::success(
@@ -49,8 +50,17 @@ public function __invoke(ServerRequest $request, array $params): void
4950
$this->response->setRequestStatus(false);
5051
}
5152

52-
$message->addParam($kill);
53+
$message->addParam($processId);
5354

5455
$this->response->addJSON(['message' => $message]);
5556
}
57+
58+
private function getProcessId(mixed $routeVars): int
59+
{
60+
if (is_array($routeVars) && isset($routeVars['id']) && is_numeric($routeVars['id'])) {
61+
return (int) $routeVars['id'];
62+
}
63+
64+
return 0;
65+
}
5666
}

libraries/classes/Controllers/Server/Variables/GetVariableController.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use PhpMyAdmin\Util;
1414

1515
use function implode;
16+
use function is_array;
17+
use function is_string;
1618

1719
final class GetVariableController extends AbstractController
1820
{
@@ -21,24 +23,25 @@ public function __construct(ResponseRenderer $response, Template $template, priv
2123
parent::__construct($response, $template);
2224
}
2325

24-
/** @param mixed[] $params Request parameters */
25-
public function __invoke(ServerRequest $request, array $params): void
26+
public function __invoke(ServerRequest $request): void
2627
{
2728
if (! $request->isAjax()) {
2829
return;
2930
}
3031

32+
$name = $this->getName($request->getAttribute('routeVars'));
33+
3134
// Send with correct charset
3235
$this->response->addHeader('Content-Type', 'text/html; charset=UTF-8');
3336
$varValue = $this->dbi->fetchSingleRow(
3437
'SHOW GLOBAL VARIABLES WHERE Variable_name='
35-
. $this->dbi->quoteString($params['name']) . ';',
38+
. $this->dbi->quoteString($name) . ';',
3639
DatabaseInterface::FETCH_NUM,
3740
);
3841

3942
$json = ['message' => $varValue[1]];
4043

41-
$variableType = ServerVariablesProvider::getImplementation()->getVariableType($params['name']);
44+
$variableType = ServerVariablesProvider::getImplementation()->getVariableType($name);
4245

4346
if ($variableType === 'byte') {
4447
/** @var string[] $bytes */
@@ -48,4 +51,13 @@ public function __invoke(ServerRequest $request, array $params): void
4851

4952
$this->response->addJSON($json);
5053
}
54+
55+
private function getName(mixed $routeVars): string
56+
{
57+
if (is_array($routeVars) && isset($routeVars['name']) && is_string($routeVars['name'])) {
58+
return $routeVars['name'];
59+
}
60+
61+
return '';
62+
}
5163
}

libraries/classes/Controllers/Server/Variables/SetVariableController.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use function __;
1616
use function htmlspecialchars;
1717
use function implode;
18+
use function is_array;
1819
use function is_numeric;
20+
use function is_string;
1921
use function mb_strtolower;
2022
use function preg_match;
2123
use function trim;
@@ -29,17 +31,15 @@ public function __construct(ResponseRenderer $response, Template $template, priv
2931

3032
/**
3133
* Handle the AJAX request for setting value for a single variable
32-
*
33-
* @param mixed[] $vars Request parameters
3434
*/
35-
public function __invoke(ServerRequest $request, array $vars): void
35+
public function __invoke(ServerRequest $request): void
3636
{
3737
if (! $request->isAjax()) {
3838
return;
3939
}
4040

4141
$value = (string) $request->getParsedBodyParam('varValue');
42-
$variableName = (string) $vars['name'];
42+
$variableName = $this->getName($request->getAttribute('routeVars'));
4343
$matches = [];
4444
$variableType = ServerVariablesProvider::getImplementation()->getVariableType($variableName);
4545

@@ -118,4 +118,13 @@ private function formatVariable(string $name, int|string $value): array
118118

119119
return [$formattedValue, $isHtmlFormatted];
120120
}
121+
122+
private function getName(mixed $routeVars): string
123+
{
124+
if (is_array($routeVars) && isset($routeVars['name']) && is_string($routeVars['name'])) {
125+
return $routeVars['name'];
126+
}
127+
128+
return '';
129+
}
121130
}

libraries/classes/Routing/Routing.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,11 @@ public static function callControllerForRoute(
162162

163163
/** @psalm-var class-string $controllerName */
164164
$controllerName = $routeInfo[1];
165-
/** @var array<string, string> $vars */
166-
$vars = $routeInfo[2];
167165

168-
/** @psalm-var callable(ServerRequest=, array<string, string>=): (Response|null) $controller */
166+
/** @psalm-var callable(ServerRequest): (Response|null) $controller */
169167
$controller = $container->get($controllerName);
170168

171-
return $controller($request, $vars);
169+
return $controller($request->withAttribute('routeVars', $routeInfo[2]));
172170
}
173171

174172
/** @psalm-assert-if-true array[] $dispatchData */

phpstan-baseline.neon

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5865,11 +5865,6 @@ parameters:
58655865
count: 1
58665866
path: libraries/classes/Controllers/Server/ReplicationController.php
58675867

5868-
-
5869-
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
5870-
count: 1
5871-
path: libraries/classes/Controllers/Server/ShowEngineController.php
5872-
58735868
-
58745869
message: "#^Parameter \\#1 \\$requiredData of method PhpMyAdmin\\\\Server\\\\Status\\\\Monitor\\:\\:getJsonForChartingData\\(\\) expects string, mixed given\\.$#"
58755870
count: 1
@@ -5905,11 +5900,6 @@ parameters:
59055900
count: 2
59065901
path: libraries/classes/Controllers/Server/Status/Monitor/SlowLogController.php
59075902

5908-
-
5909-
message: "#^Cannot cast mixed to int\\.$#"
5910-
count: 1
5911-
path: libraries/classes/Controllers/Server/Status/Processes/KillController.php
5912-
59135903
-
59145904
message: "#^Only booleans are allowed in an if condition, PhpMyAdmin\\\\Dbal\\\\ResultInterface\\|false given\\.$#"
59155905
count: 1
@@ -6025,24 +6015,14 @@ parameters:
60256015
count: 2
60266016
path: libraries/classes/Controllers/Server/Variables/GetVariableController.php
60276017

6028-
-
6029-
message: "#^Parameter \\#1 \\$name of method PhpMyAdmin\\\\Providers\\\\ServerVariables\\\\ServerVariablesProviderInterface\\:\\:getVariableType\\(\\) expects string, mixed given\\.$#"
6030-
count: 1
6031-
path: libraries/classes/Controllers/Server/Variables/GetVariableController.php
6032-
6033-
-
6034-
message: "#^Parameter \\#1 \\$str of method PhpMyAdmin\\\\DatabaseInterface\\:\\:quoteString\\(\\) expects string, mixed given\\.$#"
6035-
count: 1
6036-
path: libraries/classes/Controllers/Server/Variables/GetVariableController.php
6037-
60386018
-
60396019
message: "#^Parameter \\#1 \\$value of static method PhpMyAdmin\\\\Util\\:\\:formatByteDown\\(\\) expects float\\|int\\|string\\|null, mixed given\\.$#"
60406020
count: 1
60416021
path: libraries/classes/Controllers/Server/Variables/GetVariableController.php
60426022

60436023
-
60446024
message: "#^Cannot cast mixed to string\\.$#"
6045-
count: 2
6025+
count: 1
60466026
path: libraries/classes/Controllers/Server/Variables/SetVariableController.php
60476027

60486028
-

psalm-baseline.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,11 +2701,6 @@
27012701
<code>__construct</code>
27022702
</PossiblyUnusedMethod>
27032703
</file>
2704-
<file src="libraries/classes/Controllers/Server/ShowEngineController.php">
2705-
<UnusedParam>
2706-
<code>$request</code>
2707-
</UnusedParam>
2708-
</file>
27092704
<file src="libraries/classes/Controllers/Server/SqlController.php">
27102705
<InvalidArrayOffset>
27112706
<code><![CDATA[$GLOBALS['errorUrl']]]></code>
@@ -2921,8 +2916,6 @@
29212916
</file>
29222917
<file src="libraries/classes/Controllers/Server/Variables/GetVariableController.php">
29232918
<MixedArgument>
2924-
<code><![CDATA[$params['name']]]></code>
2925-
<code><![CDATA[$params['name']]]></code>
29262919
<code>$varValue[1]</code>
29272920
</MixedArgument>
29282921
<PossiblyNullArrayAccess>

test/classes/Controllers/Server/ShowEngineControllerTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PhpMyAdmin\Controllers\Server\ShowEngineController;
88
use PhpMyAdmin\DatabaseInterface;
99
use PhpMyAdmin\Html\MySQLDocumentation;
10-
use PhpMyAdmin\Http\ServerRequest;
10+
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
1111
use PhpMyAdmin\StorageEngine;
1212
use PhpMyAdmin\Template;
1313
use PhpMyAdmin\Tests\AbstractTestCase;
@@ -48,13 +48,11 @@ public function testShowEngine(): void
4848
$GLOBALS['cfg']['Server']['DisableIS'] = false;
4949

5050
$response = new ResponseRenderer();
51-
$request = $this->createMock(ServerRequest::class);
5251
$this->dummyDbi->addSelectDb('mysql');
52+
$request = ServerRequestFactory::create()->createServerRequest('GET', 'http://example.com/')
53+
->withAttribute('routeVars', ['engine' => 'Pbxt', 'page' => 'page']);
5354

54-
(new ShowEngineController($response, new Template(), DatabaseInterface::getInstance()))($request, [
55-
'engine' => 'Pbxt',
56-
'page' => 'page',
57-
]);
55+
(new ShowEngineController($response, new Template(), DatabaseInterface::getInstance()))($request);
5856

5957
$this->dummyDbi->assertAllSelectsConsumed();
6058
$actual = $response->getHTMLResult();

0 commit comments

Comments
 (0)