Skip to content

Commit 2a26c2c

Browse files
Merge pull request phpmyadmin#20183 from MauricioFauth/ResponseRenderer-di-refactor
Extract ResponseRenderer dependency from some classes
2 parents 928e4d5 + 0317757 commit 2a26c2c

12 files changed

Lines changed: 102 additions & 112 deletions

phpstan-baseline.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11037,7 +11037,7 @@ parameters:
1103711037
-
1103811038
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
1103911039
identifier: empty.notAllowed
11040-
count: 13
11040+
count: 12
1104111041
path: src/Server/Privileges.php
1104211042

1104311043
-
@@ -11073,7 +11073,7 @@ parameters:
1107311073
-
1107411074
message: '#^Only booleans are allowed in a negated boolean, PhpMyAdmin\\Dbal\\ResultInterface\|false given\.$#'
1107511075
identifier: booleanNot.exprNotBoolean
11076-
count: 13
11076+
count: 11
1107711077
path: src/Server/Privileges.php
1107811078

1107911079
-

psalm-baseline.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7275,6 +7275,10 @@
72757275
<PossiblyNullArgument>
72767276
<code><![CDATA[$srReplicaAction]]></code>
72777277
</PossiblyNullArgument>
7278+
<PossiblyUnusedReturnValue>
7279+
<code><![CDATA[bool]]></code>
7280+
<code><![CDATA[bool]]></code>
7281+
</PossiblyUnusedReturnValue>
72787282
<UnusedFunctionCall>
72797283
<code><![CDATA[strtok]]></code>
72807284
<code><![CDATA[strtok]]></code>
@@ -7519,7 +7523,6 @@
75197523
<code><![CDATA[empty($_POST['pma_pw'])]]></code>
75207524
<code><![CDATA[empty($_POST['pma_pw2'])]]></code>
75217525
<code><![CDATA[empty($_POST['userGroup'])]]></code>
7522-
<code><![CDATA[empty($_REQUEST['ajax_page_request'])]]></code>
75237526
<code><![CDATA[empty($row['password'])]]></code>
75247527
<code><![CDATA[empty($row['password'])]]></code>
75257528
</RiskyTruthyFalsyComparison>

src/Controllers/Server/PrivilegesController.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use PhpMyAdmin\Controllers\InvocableController;
1111
use PhpMyAdmin\Current;
1212
use PhpMyAdmin\Dbal\DatabaseInterface;
13+
use PhpMyAdmin\Exceptions\UpdateAuthPluginFailure;
14+
use PhpMyAdmin\Exceptions\UserPasswordUpdateFailure;
1315
use PhpMyAdmin\Html\Generator;
1416
use PhpMyAdmin\Http\Response;
1517
use PhpMyAdmin\Http\ServerRequest;
@@ -262,11 +264,23 @@ public function __invoke(ServerRequest $request): Response
262264
* Updates the password
263265
*/
264266
if ($request->hasBodyParam('change_pw')) {
265-
Current::$message = $serverPrivileges->updatePassword(
266-
$errorUrl,
267-
$serverPrivileges->username ?? '',
268-
$serverPrivileges->hostname ?? '',
269-
);
267+
try {
268+
Current::$message = $serverPrivileges->updatePassword(
269+
$serverPrivileges->username ?? '',
270+
$serverPrivileges->hostname ?? '',
271+
);
272+
} catch (UpdateAuthPluginFailure | UserPasswordUpdateFailure $exception) {
273+
if ($request->isAjax()) {
274+
$this->response->setRequestStatus(false);
275+
$this->response->addJSON('message', $exception->getMessage());
276+
277+
return $this->response->response();
278+
}
279+
280+
$this->response->addHTML($exception->getMessage() . Generator::getBackUrlHtml($errorUrl));
281+
282+
return $this->response->response();
283+
}
270284
}
271285

272286
/**
@@ -392,6 +406,8 @@ public function __invoke(ServerRequest $request): Response
392406
$this->response->addHTML($serverPrivileges->getHtmlForUserOverview(
393407
$userPrivileges,
394408
$request->getQueryParam('initial'),
409+
$request->isAjax(),
410+
$request->has('ajax_page_request'),
395411
));
396412
} elseif ($routinename !== '') {
397413
$this->response->addHTML(

src/Controllers/Server/ReplicationController.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
use PhpMyAdmin\Dbal\DatabaseInterface;
1212
use PhpMyAdmin\Http\Response;
1313
use PhpMyAdmin\Http\ServerRequest;
14+
use PhpMyAdmin\Message;
1415
use PhpMyAdmin\Replication\ReplicationGui;
1516
use PhpMyAdmin\Replication\ReplicationInfo;
1617
use PhpMyAdmin\ResponseRenderer;
1718
use PhpMyAdmin\Routing\Route;
19+
use PhpMyAdmin\Url;
1820
use PhpMyAdmin\UrlParams;
1921

2022
use function is_array;
@@ -61,7 +63,7 @@ public function __invoke(ServerRequest $request): Response
6163
$srSkipErrorsCount = $request->getParsedBodyParamAsStringOrNull('sr_skip_errors_count', '1');
6264
$srReplicaControlParam = $request->getParsedBodyParamAsStringOrNull('sr_replica_control_param');
6365

64-
$this->replicationGui->handleControlRequest(
66+
$message = $this->replicationGui->handleControlRequest(
6567
$request->getParsedBodyParam('sr_take_action') !== null,
6668
$request->getParsedBodyParam('replica_changeprimary') !== null,
6769
$request->getParsedBodyParam('sr_replica_server_control') !== null,
@@ -74,6 +76,16 @@ public function __invoke(ServerRequest $request): Response
7476
$request->getParsedBodyParamAsString('hostname', ''),
7577
(int) $request->getParsedBodyParamAsStringOrNull('text_port'),
7678
);
79+
if ($message instanceof Message) {
80+
if ($request->isAjax()) {
81+
$this->response->setRequestStatus($message->isSuccess());
82+
$this->response->addJSON('message', $message);
83+
} else {
84+
$this->response->redirect(
85+
'./index.php?route=/server/replication' . Url::getCommonRaw(UrlParams::$params, '&'),
86+
);
87+
}
88+
}
7789
}
7890

7991
$errorMessages = $this->replicationGui->getHtmlForErrorMessage();

src/Controllers/Table/AddFieldController.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,12 @@ public function __invoke(ServerRequest $request): Response
101101
return $this->response->response();
102102
}
103103

104-
$result = $createAddField->tryColumnCreationQuery(
104+
$errorMessageHtml = $createAddField->tryColumnCreationQuery(
105105
DatabaseName::from(Current::$database),
106106
Current::$sqlQuery,
107-
$errorUrl,
108107
);
109-
110-
if (! $result) {
111-
$errorMessageHtml = Generator::mysqlDie('', '', false);
112-
$this->response->addHTML($errorMessageHtml);
108+
if ($errorMessageHtml !== null) {
109+
$this->response->addHTML($errorMessageHtml . Generator::getBackUrlHtml($errorUrl));
113110
$this->response->setRequestStatus(false);
114111

115112
return $this->response->response();

src/CreateAddField.php

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -436,33 +436,18 @@ public function getColumnCreationQuery(
436436
*
437437
* @param DatabaseName $db current database
438438
* @param string $sqlQuery the query to run
439-
* @param string $errorUrl error page url
440439
*/
441-
public function tryColumnCreationQuery(
442-
DatabaseName $db,
443-
string $sqlQuery,
444-
string $errorUrl,
445-
): bool {
446-
// To allow replication, we first select the db to use and then run queries
447-
// on this db.
440+
public function tryColumnCreationQuery(DatabaseName $db, string $sqlQuery): string|null
441+
{
442+
// To allow replication, we first select the db to use and then run queries on this db.
448443
if (! $this->dbi->selectDb($db)) {
449-
$errorMessage = Generator::mysqlDie(
450-
$this->dbi->getError(),
451-
'USE ' . Util::backquote($db->getName()),
452-
false,
453-
);
454-
455-
$response = ResponseRenderer::getInstance();
456-
if ($response->isAjax()) {
457-
$response->setRequestStatus(false);
458-
$response->addJSON('message', $errorMessage);
459-
$response->callExit();
460-
}
444+
return Generator::mysqlDie($this->dbi->getError(), 'USE ' . Util::backquote($db->getName()), false);
445+
}
461446

462-
$response->addHTML($errorMessage . Generator::getBackUrlHtml($errorUrl));
463-
$response->callExit();
447+
if ($this->dbi->tryQuery($sqlQuery) === false) {
448+
return Generator::mysqlDie($this->dbi->getError(), $sqlQuery, false);
464449
}
465450

466-
return (bool) $this->dbi->tryQuery($sqlQuery);
451+
return null;
467452
}
468453
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\Exceptions;
6+
7+
use RuntimeException;
8+
9+
final class PdfCreationFailure extends RuntimeException
10+
{
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\Exceptions;
6+
7+
use RuntimeException;
8+
9+
final class UpdateAuthPluginFailure extends RuntimeException
10+
{
11+
}

src/Pdf.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use DateTimeImmutable;
1111
use Exception;
12+
use PhpMyAdmin\Exceptions\PdfCreationFailure;
1213
use PhpMyAdmin\I18n\LanguageManager;
1314
use TCPDF;
1415
use TCPDF_FONTS;
@@ -142,9 +143,6 @@ protected function _putpages(): void
142143
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
143144
public function Error(mixed $msg = ''): never
144145
{
145-
echo Message::error(
146-
__('Error while creating PDF:') . ' ' . $msg,
147-
)->getDisplay();
148-
ResponseRenderer::getInstance()->callExit();
146+
throw new PdfCreationFailure(__('Error while creating PDF:') . ' ' . $msg);
149147
}
150148
}

src/Replication/ReplicationGui.php

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PhpMyAdmin\Dbal\DatabaseInterface;
1313
use PhpMyAdmin\Message;
1414
use PhpMyAdmin\Query\Utilities;
15-
use PhpMyAdmin\ResponseRenderer;
1615
use PhpMyAdmin\Template;
1716
use PhpMyAdmin\Url;
1817
use PhpMyAdmin\UrlParams;
@@ -425,27 +424,21 @@ public function handleControlRequest(
425424
string $pmaPassword,
426425
string $hostname,
427426
int $port,
428-
): void {
427+
): Message|null {
429428
if (! $srTakeAction) {
430-
return;
429+
return null;
431430
}
432431

433-
$refresh = false;
434-
$result = false;
435-
$messageSuccess = '';
436-
$messageError = '';
437-
438432
if ($replicaChangePrimary && ! Config::getInstance()->settings['AllowArbitraryServer']) {
439433
$_SESSION['replication']['sr_action_status'] = 'error';
440434
$_SESSION['replication']['sr_action_info'] = __(
441435
'Connection to server is disabled, please enable'
442436
. ' $cfg[\'AllowArbitraryServer\'] in phpMyAdmin configuration.',
443437
);
444438
} elseif ($replicaChangePrimary) {
445-
$result = $this->handleRequestForReplicaChangePrimary($username, $pmaPassword, $hostname, $port);
439+
$this->handleRequestForReplicaChangePrimary($username, $pmaPassword, $hostname, $port);
446440
} elseif ($srReplicaServerControl) {
447441
$result = $this->handleRequestForReplicaServerControl($srReplicaAction, $srReplicaControlParam);
448-
$refresh = true;
449442

450443
switch ($srReplicaAction) {
451444
case 'start':
@@ -465,28 +458,13 @@ public function handleControlRequest(
465458
$messageError = __('Error.');
466459
break;
467460
}
468-
} elseif ($srReplicaSkipError) {
469-
$result = $this->handleRequestForReplicaSkipError($srSkipErrorsCount);
470-
}
471461

472-
if ($refresh) {
473-
$response = ResponseRenderer::getInstance();
474-
if ($response->isAjax()) {
475-
$response->setRequestStatus($result);
476-
$response->addJSON(
477-
'message',
478-
$result
479-
? Message::success($messageSuccess)
480-
: Message::error($messageError),
481-
);
482-
} else {
483-
$response->redirect(
484-
'./index.php?route=/server/replication' . Url::getCommonRaw(UrlParams::$params, '&'),
485-
);
486-
}
462+
return $result ? Message::success($messageSuccess) : Message::error($messageError);
463+
} elseif ($srReplicaSkipError) {
464+
$this->handleRequestForReplicaSkipError($srSkipErrorsCount);
487465
}
488466

489-
unset($refresh);
467+
return null;
490468
}
491469

492470
public function handleRequestForReplicaChangePrimary(

0 commit comments

Comments
 (0)