Skip to content

Commit

Permalink
Extract controllers from AjaxController
Browse files Browse the repository at this point in the history
Extracts the config set and get to the ConfigController, the databases
list to the DatabaseController, the tables list to the TableController
and the columns list to the ColumnController.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Oct 12, 2020
1 parent bd99f52 commit 111cb5d
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 132 deletions.
6 changes: 3 additions & 3 deletions js/src/export.js
Expand Up @@ -833,7 +833,7 @@ Export.createAliasModal = function (event) {
'ajax_request': true,
'server': CommonParams.get('server')
};
$.post('index.php?route=/ajax/list-databases', params, function (response) {
$.post('index.php?route=/databases', params, function (response) {
if (response.success === true) {
$.each(response.databases, function (idx, value) {
var option = $('<option></option>');
Expand Down Expand Up @@ -950,7 +950,7 @@ AJAX.registerOnload('export.js', function () {
'server': CommonParams.get('server'),
'db': database,
};
var url = 'index.php?route=/ajax/list-tables';
var url = 'index.php?route=/tables';
$.post(url, params, function (response) {
if (response.success === true) {
$.each(response.tables, function (idx, value) {
Expand All @@ -975,7 +975,7 @@ AJAX.registerOnload('export.js', function () {
'db': database,
'table': table,
};
var url = 'index.php?route=/ajax/list-columns';
var url = 'index.php?route=/columns';
$.post(url, params, function (response) {
if (response.success === true) {
$.each(response.columns, function (idx, value) {
Expand Down
4 changes: 2 additions & 2 deletions js/src/functions.js
Expand Up @@ -5113,7 +5113,7 @@ Functions.configSet = function (key, value) {
var serialized = JSON.stringify(value);
localStorage.setItem(key, serialized);
$.ajax({
url: 'index.php?route=/ajax/config-set',
url: 'index.php?route=/config/set',
type: 'POST',
dataType: 'json',
data: {
Expand Down Expand Up @@ -5163,7 +5163,7 @@ Functions.configGet = function (key, cached) {
// processing cannot continue until that value is found.
// Another solution is to provide a callback as a parameter.
async: false,
url: 'index.php?route=/ajax/config-get',
url: 'index.php?route=/config/get',
type: 'POST',
dataType: 'json',
data: {
Expand Down
110 changes: 0 additions & 110 deletions libraries/classes/Controllers/AjaxController.php

This file was deleted.

38 changes: 38 additions & 0 deletions libraries/classes/Controllers/ColumnController.php
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Controllers;

use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Message;
use PhpMyAdmin\Response;
use PhpMyAdmin\Template;

final class ColumnController extends AbstractController
{
/** @var DatabaseInterface */
private $dbi;

/**
* @param Response $response
* @param DatabaseInterface $dbi
*/
public function __construct($response, Template $template, $dbi)
{
parent::__construct($response, $template);
$this->dbi = $dbi;
}

public function all(): void
{
if (! isset($_POST['db'], $_POST['table'])) {
$this->response->setRequestStatus(false);
$this->response->addJSON(['message' => Message::error()]);

return;
}

$this->response->addJSON(['columns' => $this->dbi->getColumnNames($_POST['db'], $_POST['table'])]);
}
}
57 changes: 57 additions & 0 deletions libraries/classes/Controllers/ConfigController.php
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Controllers;

use PhpMyAdmin\Config;
use PhpMyAdmin\Message;
use PhpMyAdmin\Response;
use PhpMyAdmin\Template;
use function json_decode;

final class ConfigController extends AbstractController
{
/** @var Config */
private $config;

/**
* @param Response $response
*/
public function __construct($response, Template $template, Config $config)
{
parent::__construct($response, $template);
$this->config = $config;
}

public function get(): void
{
if (! isset($_POST['key'])) {
$this->response->setRequestStatus(false);
$this->response->addJSON(['message' => Message::error()]);

return;
}

$this->response->addJSON(['value' => $this->config->get($_POST['key'])]);
}

public function set(): void
{
if (! isset($_POST['key'], $_POST['value'])) {
$this->response->setRequestStatus(false);
$this->response->addJSON(['message' => Message::error()]);

return;
}

$result = $this->config->setUserValue(null, $_POST['key'], json_decode($_POST['value']));

if ($result === true) {
return;
}

$this->response->setRequestStatus(false);
$this->response->addJSON(['message' => $result]);
}
}
15 changes: 15 additions & 0 deletions libraries/classes/Controllers/DatabaseController.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Controllers;

final class DatabaseController extends AbstractController
{
public function all(): void
{
global $dblist;

$this->response->addJSON(['databases' => $dblist->databases]);
}
}
38 changes: 38 additions & 0 deletions libraries/classes/Controllers/TableController.php
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Controllers;

use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Message;
use PhpMyAdmin\Response;
use PhpMyAdmin\Template;

final class TableController extends AbstractController
{
/** @var DatabaseInterface */
private $dbi;

/**
* @param Response $response
* @param DatabaseInterface $dbi
*/
public function __construct($response, Template $template, $dbi)
{
parent::__construct($response, $template);
$this->dbi = $dbi;
}

public function all(): void
{
if (! isset($_POST['db'])) {
$this->response->setRequestStatus(false);
$this->response->addJSON(['message' => Message::error()]);

return;
}

$this->response->addJSON(['tables' => $this->dbi->getTables($_POST['db'])]);
}
}
19 changes: 11 additions & 8 deletions libraries/routes.php
Expand Up @@ -3,10 +3,11 @@
declare(strict_types=1);

use FastRoute\RouteCollector;
use PhpMyAdmin\Controllers\AjaxController;
use PhpMyAdmin\Controllers\BrowseForeignersController;
use PhpMyAdmin\Controllers\ChangeLogController;
use PhpMyAdmin\Controllers\CheckRelationsController;
use PhpMyAdmin\Controllers\ColumnController;
use PhpMyAdmin\Controllers\ConfigController;
use PhpMyAdmin\Controllers\Database\CentralColumnsController;
use PhpMyAdmin\Controllers\Database\DataDictionaryController;
use PhpMyAdmin\Controllers\Database\DesignerController;
Expand All @@ -24,6 +25,7 @@
use PhpMyAdmin\Controllers\Database\StructureController;
use PhpMyAdmin\Controllers\Database\TrackingController;
use PhpMyAdmin\Controllers\Database\TriggersController;
use PhpMyAdmin\Controllers\DatabaseController;
use PhpMyAdmin\Controllers\ErrorReportController;
use PhpMyAdmin\Controllers\ExportController;
use PhpMyAdmin\Controllers\ExportTemplateController;
Expand Down Expand Up @@ -87,6 +89,7 @@
use PhpMyAdmin\Controllers\Table\TrackingController as TableTrackingController;
use PhpMyAdmin\Controllers\Table\TriggersController as TableTriggersController;
use PhpMyAdmin\Controllers\Table\ZoomSearchController;
use PhpMyAdmin\Controllers\TableController;
use PhpMyAdmin\Controllers\ThemesController;
use PhpMyAdmin\Controllers\TransformationOverviewController;
use PhpMyAdmin\Controllers\TransformationWrapperController;
Expand All @@ -107,16 +110,14 @@
$routes->addRoute(['GET', 'POST'], '/recent-table', [HomeController::class, 'reloadRecentTablesList']);
$routes->addRoute(['GET', 'POST'], '/git-revision', [HomeController::class, 'gitRevision']);
});
$routes->addGroup('/ajax', static function (RouteCollector $routes): void {
$routes->post('/list-databases', [AjaxController::class, 'databases']);
$routes->post('/list-tables', [AjaxController::class, 'tables']);
$routes->post('/list-columns', [AjaxController::class, 'columns']);
$routes->post('/config-get', [AjaxController::class, 'getConfig']);
$routes->post('/config-set', [AjaxController::class, 'setConfig']);
});
$routes->addRoute(['GET', 'POST'], '/browse-foreigners', [BrowseForeignersController::class, 'index']);
$routes->get('/changelog', [ChangeLogController::class, 'index']);
$routes->addRoute(['GET', 'POST'], '/check-relations', [CheckRelationsController::class, 'index']);
$routes->post('/columns', [ColumnController::class, 'all']);
$routes->addGroup('/config', static function (RouteCollector $routes): void {
$routes->post('/get', [ConfigController::class, 'get']);
$routes->post('/set', [ConfigController::class, 'set']);
});
$routes->addGroup('/database', static function (RouteCollector $routes): void {
$routes->addGroup('/central-columns', static function (RouteCollector $routes): void {
$routes->addRoute(['GET', 'POST'], '', [CentralColumnsController::class, 'index']);
Expand Down Expand Up @@ -174,6 +175,7 @@
$routes->addRoute(['GET', 'POST'], '/tracking', [TrackingController::class, 'index']);
$routes->addRoute(['GET', 'POST'], '/triggers', [TriggersController::class, 'index']);
});
$routes->post('/databases', [DatabaseController::class, 'all']);
$routes->addRoute(['GET', 'POST'], '/error-report', [ErrorReportController::class, 'index']);
$routes->addGroup('/export', static function (RouteCollector $routes): void {
$routes->addRoute(['GET', 'POST'], '', [ExportController::class, 'index']);
Expand Down Expand Up @@ -313,6 +315,7 @@
$routes->addRoute(['GET', 'POST'], '/triggers', [TableTriggersController::class, 'index']);
$routes->addRoute(['GET', 'POST'], '/zoom-search', [ZoomSearchController::class, 'index']);
});
$routes->post('/tables', [TableController::class, 'all']);
$routes->get('/themes', [ThemesController::class, 'index']);
$routes->addGroup('/transformation', static function (RouteCollector $routes): void {
$routes->addRoute(['GET', 'POST'], '/overview', [TransformationOverviewController::class, 'index']);
Expand Down

0 comments on commit 111cb5d

Please sign in to comment.