Skip to content

Commit

Permalink
Create a dedicated controller to update Console config
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Mar 29, 2024
1 parent 775a9a4 commit 65c6f18
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 5 deletions.
4 changes: 4 additions & 0 deletions app/services_controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@
'class' => Console\Bookmark\RefreshController::class,
'arguments' => ['$response' => '@response', '$template' => '@template', '$console' => '@console'],
],
Console\UpdateConfigController::class => [
'class' => Console\UpdateConfigController::class,
'arguments' => ['$response' => '@response', '$template' => '@template', '$config' => '@config'],
],
Database\CentralColumns\PopulateColumnsController::class => [
'class' => Database\CentralColumns\PopulateColumnsController::class,
'arguments' => [
Expand Down
5 changes: 5 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,11 @@
<code><![CDATA[$request]]></code>
</UnusedParam>
</file>
<file src="src/Controllers/Console/UpdateConfigController.php">
<PossiblyUnusedMethod>
<code><![CDATA[__construct]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/Controllers/Database/CentralColumns/PopulateColumnsController.php">
<MixedArgument>
<code><![CDATA[$request->getParsedBodyParam('selectedTable')]]></code>
Expand Down
36 changes: 34 additions & 2 deletions resources/js/src/modules/console/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { setConfigValue } from '../functions/config.ts';
import $ from 'jquery';
import { ajaxShowMessage } from '../ajax-message.ts';
import { CommonParams } from '../common.ts';

/**
* @link https://docs.phpmyadmin.net/en/latest/config.html#console-settings
Expand Down Expand Up @@ -65,7 +67,7 @@ export const Config = {
*/
set: function (key, value): void {
this[key] = value;
setConfigValue('Console/' + key, value);
setConfigValue(key, value);
},

/**
Expand All @@ -86,3 +88,33 @@ export const Config = {
}
}
};

/**
* @param {'StartHistory'|'AlwaysExpand'|'CurrentQuery'|'EnterExecutes'|'DarkTheme'|'Mode'|'Height'|'GroupQueries'|'OrderBy'|'Order'} key
* @param {boolean|string|number} value
*/
function setConfigValue (key, value): void {
// Updating value in local storage.
const serialized = JSON.stringify(value);
localStorage.setItem('Console/' + key, serialized);

$.ajax({
url: 'index.php?route=/console/update-config',
type: 'POST',
dataType: 'json',
data: {
'ajax_request': true,
key: key,
server: CommonParams.get('server'),
value: serialized,
},
success: function (data) {
if (data.success !== true) {
// Try to find a message to display
if (data.error || data.message) {
ajaxShowMessage(data.error || data.message);
}
}
}
});
}
56 changes: 56 additions & 0 deletions src/Controllers/Console/UpdateConfigController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Controllers\Console;

use PhpMyAdmin\Config;
use PhpMyAdmin\Controllers\AbstractController;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;

use function in_array;
use function is_string;
use function json_decode;

final class UpdateConfigController extends AbstractController
{
public function __construct(ResponseRenderer $response, Template $template, private Config $config)
{
parent::__construct($response, $template);
}

public function __invoke(ServerRequest $request): void
{
$validKeys = [
'StartHistory',
'AlwaysExpand',
'CurrentQuery',
'EnterExecutes',
'DarkTheme',
'Mode',
'Height',
'GroupQueries',
'OrderBy',
'Order',
];
$key = $request->getParsedBodyParam('key');
$value = $request->getParsedBodyParam('value');
if (! in_array($key, $validKeys, true) || ! is_string($value)) {
$this->response->setRequestStatus(false);
$this->response->addJSON(['message' => Message::error()]);

return;
}

$result = $this->config->setUserValue(null, 'Console/' . $key, json_decode($value));
if ($result === true) {
return;
}

$this->response->setRequestStatus(false);
$this->response->addJSON(['message' => $result]);
}
}
10 changes: 7 additions & 3 deletions src/Routing/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpMyAdmin\Controllers\ColumnController;
use PhpMyAdmin\Controllers\Config;
use PhpMyAdmin\Controllers\Console\Bookmark;
use PhpMyAdmin\Controllers\Console\UpdateConfigController;
use PhpMyAdmin\Controllers\Database;
use PhpMyAdmin\Controllers\DatabaseController;
use PhpMyAdmin\Controllers\ErrorReportController;
Expand Down Expand Up @@ -57,9 +58,12 @@ public static function collect(RouteCollector $routes): void
$routes->post('/get', Config\GetConfigController::class);
$routes->post('/set', Config\SetConfigController::class);
});
$routes->addGroup('/console/bookmark', static function (RouteCollector $routes): void {
$routes->post('/add', Bookmark\AddController::class);
$routes->get('/refresh', Bookmark\RefreshController::class);
$routes->addGroup('/console', static function (RouteCollector $routes): void {
$routes->addGroup('/bookmark', static function (RouteCollector $routes): void {
$routes->post('/add', Bookmark\AddController::class);
$routes->get('/refresh', Bookmark\RefreshController::class);
});
$routes->post('/update-config', UpdateConfigController::class);
});
$routes->addGroup('/database', static function (RouteCollector $routes): void {
$routes->addGroup('/central-columns', static function (RouteCollector $routes): void {
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/Routing/RoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpMyAdmin\Controllers\ColumnController;
use PhpMyAdmin\Controllers\Config;
use PhpMyAdmin\Controllers\Console\Bookmark;
use PhpMyAdmin\Controllers\Console\UpdateConfigController;
use PhpMyAdmin\Controllers\Database;
use PhpMyAdmin\Controllers\Database\Structure\CentralColumns;
use PhpMyAdmin\Controllers\DatabaseController;
Expand Down Expand Up @@ -164,6 +165,7 @@ public function testCollect(): void
'/config/get' => Config\GetConfigController::class,
'/config/set' => Config\SetConfigController::class,
'/console/bookmark/add' => Bookmark\AddController::class,
'/console/update-config' => UpdateConfigController::class,
'/database/central-columns' => Database\CentralColumnsController::class,
'/database/central-columns/populate' => Database\CentralColumns\PopulateColumnsController::class,
'/database/designer' => Database\DesignerController::class,
Expand Down

0 comments on commit 65c6f18

Please sign in to comment.