Skip to content

Commit 45b58d7

Browse files
committed
Refactor JavaScriptMessagesController to return a Response object
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent f72a07a commit 45b58d7

3 files changed

Lines changed: 32 additions & 26 deletions

File tree

‎libraries/classes/Controllers/JavaScriptMessagesController.php‎

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,50 @@
44

55
namespace PhpMyAdmin\Controllers;
66

7+
use Fig\Http\Message\StatusCodeInterface;
8+
use JsonException;
9+
use PhpMyAdmin\Http\Factory\ResponseFactory;
10+
use PhpMyAdmin\Http\Response;
11+
712
use function __;
813
use function _pgettext;
914
use function gmdate;
10-
use function header;
1115
use function json_encode;
12-
use function json_last_error_msg;
16+
use function sprintf;
1317
use function time;
1418

19+
use const JSON_THROW_ON_ERROR;
20+
1521
/**
1622
* Exporting of translated messages from PHP to JavaScript.
1723
*/
1824
final class JavaScriptMessagesController
1925
{
20-
/** @var array<string, string> */
21-
private array $messages;
22-
23-
public function __construct()
26+
public function __construct(private readonly ResponseFactory $responseFactory)
2427
{
25-
$this->messages = $this->setMessages();
2628
}
2729

28-
public function __invoke(): void
30+
public function __invoke(): Response
2931
{
30-
$this->setHTTPHeaders();
31-
$messages = json_encode($this->messages);
32-
if ($messages === false) {
33-
echo '// Error when encoding messages: ' . json_last_error_msg();
32+
$response = $this->responseFactory->createResponse()
33+
->withHeader('Content-Type', 'text/javascript; charset=UTF-8')
34+
->withHeader('Expires', sprintf('%s GMT', gmdate('D, d M Y H:i:s', time() + 3600)));
3435

35-
return;
36+
try {
37+
$messages = json_encode($this->getMessages(), JSON_THROW_ON_ERROR);
38+
} catch (JsonException $exception) {
39+
$response->getBody()->write(sprintf('// Error when encoding messages: %s', $exception->getMessage()));
40+
41+
return $response->withStatus(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR);
3642
}
3743

38-
echo 'window.Messages = ' . $messages . ';';
39-
}
44+
$response->getBody()->write(sprintf('window.Messages = %s;', $messages));
4045

41-
private function setHTTPHeaders(): void
42-
{
43-
// Send correct type.
44-
header('Content-Type: text/javascript; charset=UTF-8');
45-
// Cache output in client
46-
// the nocache query parameter makes sure that this file is reloaded when config changes.
47-
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 3600) . ' GMT');
46+
return $response;
4847
}
4948

5049
/** @return array<string, string> */
51-
private function setMessages(): array
50+
private function getMessages(): array
5251
{
5352
return [
5453
/* For confirmations */

‎libraries/services_controllers.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use PhpMyAdmin\Controllers\UserPasswordController;
4040
use PhpMyAdmin\Controllers\VersionCheckController;
4141
use PhpMyAdmin\Controllers\View;
42+
use PhpMyAdmin\Http\Factory\ResponseFactory;
4243
use PhpMyAdmin\Plugins\AuthenticationPluginFactory;
4344
use PhpMyAdmin\Theme\ThemeManager;
4445

@@ -465,7 +466,10 @@
465466
'class' => Import\StatusController::class,
466467
'arguments' => ['$template' => '@template'],
467468
],
468-
JavaScriptMessagesController::class => ['class' => JavaScriptMessagesController::class],
469+
JavaScriptMessagesController::class => [
470+
'class' => JavaScriptMessagesController::class,
471+
'arguments' => ['@' . ResponseFactory::class],
472+
],
469473
LicenseController::class => [
470474
'class' => LicenseController::class,
471475
'arguments' => ['$response' => '@response', '$template' => '@template'],

‎test/classes/Controllers/JavaScriptMessagesControllerTest.php‎

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

55
namespace PhpMyAdmin\Tests\Controllers;
66

7+
use Fig\Http\Message\StatusCodeInterface;
78
use PhpMyAdmin\Controllers\JavaScriptMessagesController;
9+
use PhpMyAdmin\Http\Factory\ResponseFactory;
810
use PHPUnit\Framework\Attributes\CoversClass;
911
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
1012
use PHPUnit\Framework\TestCase;
@@ -19,8 +21,9 @@ class JavaScriptMessagesControllerTest extends TestCase
1921
#[RunInSeparateProcess]
2022
public function testIndex(): void
2123
{
22-
(new JavaScriptMessagesController())();
23-
$actual = $this->getActualOutputForAssertion();
24+
$response = (new JavaScriptMessagesController(ResponseFactory::create()))();
25+
$actual = (string) $response->getBody();
26+
$this->assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
2427

2528
$this->assertStringStartsWith('window.Messages = {', $actual);
2629
$this->assertStringEndsWith('};', $actual);

0 commit comments

Comments
 (0)