Skip to content

Commit

Permalink
Merge pull request #15953 from mauriciofauth/routes-refactor
Browse files Browse the repository at this point in the history
Refactor the route definition file 

- Move request and response handling from routes file to the controllers.
- Simplify the routes file by moving the controller creation to the `index.php` file.
- Other minor changes.
  • Loading branch information
MauricioFauth committed Feb 12, 2020
2 parents ddd46a6 + d85cca0 commit d0283d7
Show file tree
Hide file tree
Showing 53 changed files with 711 additions and 1,264 deletions.
5 changes: 3 additions & 2 deletions index.php
Expand Up @@ -60,6 +60,7 @@
$response->setHttpResponseCode(405);
Message::error(__('Error 405! Request method not allowed.'))->display();
} elseif ($routeInfo[0] === Dispatcher::FOUND) {
$handler = $routeInfo[1];
$handler($routeInfo[2]);
[$controllerName, $action] = $routeInfo[1];
$controller = $containerBuilder->get($controllerName);
$controller->$action($routeInfo[2]);
}
6 changes: 3 additions & 3 deletions js/database/multi_table_query.js
Expand Up @@ -12,7 +12,7 @@
/* global generateFromBlock, generateWhereBlock */ // js/database/query_generator.js

/**
* js file for handling AJAX and other events in /database/multi_table_query
* js file for handling AJAX and other events in /database/multi-table-query
*/

/**
Expand Down Expand Up @@ -70,7 +70,7 @@ AJAX.registerOnload('database/multi_table_query.js', function () {
$.ajax({
type: 'GET',
async: false,
url: 'index.php?route=/database/multi_table_query/tables',
url: 'index.php?route=/database/multi-table-query/tables',
data: {
'server': sessionStorage.server,
'db': $('#db_name').val(),
Expand Down Expand Up @@ -132,7 +132,7 @@ AJAX.registerOnload('database/multi_table_query.js', function () {
};
$.ajax({
type: 'POST',
url: 'index.php?route=/database/multi_table_query/query',
url: 'index.php?route=/database/multi-table-query/query',
data: data,
success: function (data) {
var $resultsDom = $(data.message);
Expand Down
4 changes: 2 additions & 2 deletions js/export.js
Expand Up @@ -933,7 +933,7 @@ AJAX.registerOnload('export.js', function () {
'server': CommonParams.get('server'),
'db': database,
};
var url = 'index.php?route=/ajax/list-tables/' + encodeURIComponent(database);
var url = 'index.php?route=/ajax/list-tables';
$.post(url, params, function (response) {
if (response.success === true) {
$.each(response.tables, function (idx, value) {
Expand All @@ -958,7 +958,7 @@ AJAX.registerOnload('export.js', function () {
'db': database,
'table': table,
};
var url = 'index.php?route=/ajax/list-columns/' + encodeURIComponent(database) + '/' + encodeURIComponent(table);
var url = 'index.php?route=/ajax/list-columns';
$.post(url, params, function (response) {
if (response.success === true) {
$.each(response.columns, function (idx, value) {
Expand Down
84 changes: 38 additions & 46 deletions libraries/classes/Controllers/AjaxController.php
Expand Up @@ -33,78 +33,70 @@ public function __construct($response, $dbi, Template $template, $config)
$this->config = $config;
}

/**
* @return array JSON
*/
public function databases(): array
public function databases(): void
{
global $dblist;

return ['databases' => $dblist->databases];
$this->response->addJSON(['databases' => $dblist->databases]);
}

/**
* @param array $params Request parameters
*
* @return array JSON
*/
public function tables(array $params): array
public function tables(): void
{
return ['tables' => $this->dbi->getTables($params['database'])];
if (! isset($_POST['db'])) {
$this->response->setRequestStatus(false);
$this->response->addJSON(['message' => Message::error()]);
return;
}

$this->response->addJSON(['tables' => $this->dbi->getTables($_POST['db'])]);
}

/**
* @param array $params Request parameters
*
* @return array JSON
*/
public function columns(array $params): array
public function columns(): void
{
return [
if (! isset($_POST['db'], $_POST['table'])) {
$this->response->setRequestStatus(false);
$this->response->addJSON(['message' => Message::error()]);
return;
}

$this->response->addJSON([
'columns' => $this->dbi->getColumnNames(
$params['database'],
$params['table']
$_POST['db'],
$_POST['table']
),
];
]);
}

/**
* @param array $params Request parameters
*
* @return array JSON
*/
public function getConfig(array $params): array
public function getConfig(): void
{
if (! isset($params['key'])) {
if (! isset($_POST['key'])) {
$this->response->setRequestStatus(false);
return ['message' => Message::error()];
$this->response->addJSON(['message' => Message::error()]);
return;
}

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

/**
* @param array $params Request parameters
*
* @return array
*/
public function setConfig(array $params): array
public function setConfig(): void
{
if (! isset($params['key'], $params['value'])) {
if (! isset($_POST['key'], $_POST['value'])) {
$this->response->setRequestStatus(false);
return ['message' => Message::error()];
$this->response->addJSON(['message' => Message::error()]);
return;
}

$result = $this->config->setUserValue(
null,
$params['key'],
json_decode($params['value'])
$_POST['key'],
json_decode($_POST['value'])
);
$json = [];
if ($result !== true) {
$this->response->setRequestStatus(false);
$json['message'] = $result;

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

$this->response->setRequestStatus(false);
$this->response->addJSON(['message' => $result]);
}
}
23 changes: 14 additions & 9 deletions libraries/classes/Controllers/BrowseForeignersController.php
Expand Up @@ -37,15 +37,20 @@ public function __construct($response, $dbi, Template $template, $browseForeigne
$this->relation = $relation;
}

/**
* @param array $params Request parameters
*
* @return string HTML
*/
public function index(array $params): string
public function index(): void
{
$params = [
'db' => $_POST['db'] ?? null,
'table' => $_POST['table'] ?? null,
'field' => $_POST['field'] ?? null,
'fieldkey' => $_POST['fieldkey'] ?? null,
'data' => $_POST['data'] ?? null,
'foreign_showAll' => $_POST['foreign_showAll'] ?? null,
'foreign_filter' => $_POST['foreign_filter'] ?? null,
];

if (! isset($params['db'], $params['table'], $params['field'])) {
return '';
return;
}

$this->response->getFooter()->setMinimal();
Expand All @@ -69,13 +74,13 @@ public function index(array $params): string
true
);

return $this->browseForeigners->getHtmlForRelationalFieldSelection(
$this->response->addHTML($this->browseForeigners->getHtmlForRelationalFieldSelection(
$params['db'],
$params['table'],
$params['field'],
$foreignData,
$params['fieldkey'] ?? '',
$params['data'] ?? ''
);
));
}
}
13 changes: 8 additions & 5 deletions libraries/classes/Controllers/CheckRelationsController.php
Expand Up @@ -28,13 +28,16 @@ public function __construct($response, $dbi, Template $template, Relation $relat
$this->relation = $relation;
}

/**
* @param array $params Request parameters
*/
public function index(array $params): string
public function index(): void
{
global $db;

$params = [
'create_pmadb' => $_POST['create_pmadb'] ?? null,
'fixall_pmadb' => $_POST['fixall_pmadb'] ?? null,
'fix_pmadb' => $_POST['fix_pmadb'] ?? null,
];

// If request for creating the pmadb
if (isset($params['create_pmadb']) && $this->relation->createPmaDatabase()) {
$this->relation->fixPmaTables('phpmyadmin');
Expand All @@ -51,6 +54,6 @@ public function index(array $params): string
$this->relation->fixPmaTables($cfgRelation['db']);
}

return $this->relation->getRelationsParamDiagnostic($cfgRelation);
$this->response->addHTML($this->relation->getRelationsParamDiagnostic($cfgRelation));
}
}
Expand Up @@ -39,12 +39,9 @@ public function __construct($response, $dbi, Template $template, $db, $relation,
$this->transformations = $transformations;
}

/**
* @param array $params Request parameters
*/
public function index(array $params): string
public function index(): void
{
$this->db = $params['database'];
Util::checkParameters(['db'], true);

$header = $this->response->getHeader();
$header->enablePrintView();
Expand Down Expand Up @@ -141,10 +138,10 @@ public function index(array $params): string
];
}

return $this->template->render('database/data_dictionary/index', [
$this->response->addHTML($this->template->render('database/data_dictionary/index', [
'database' => $this->db,
'comment' => $comment,
'tables' => $tables,
]);
]));
}
}
Expand Up @@ -13,7 +13,7 @@
*/
class MultiTableQueryController extends AbstractController
{
public function index(): string
public function index(): void
{
$header = $this->response->getHeader();
$scripts = $header->getScripts();
Expand All @@ -23,35 +23,35 @@ public function index(): string

$queryInstance = new MultiTableQuery($this->dbi, $this->template, $this->db);

return $queryInstance->getFormHtml();
$this->response->addHTML($queryInstance->getFormHtml());
}

/**
* @param array $params Request parameters
*/
public function displayResults(array $params): void
public function displayResults(): void
{
global $pmaThemeImage;

$params = [
'sql_query' => $_POST['sql_query'],
'db' => $_POST['db'] ?? $_GET['db'] ?? null,
];

MultiTableQuery::displayResults(
$params['sql_query'],
$params['db'],
$pmaThemeImage
);
}

/**
* @param array $params Request parameters
*
* @return array JSON
*/
public function table(array $params): array
public function table(): void
{
$params = [
'tables' => $_GET['tables'],
'db' => $_GET['db'] ?? null,
];
$constrains = $this->dbi->getForeignKeyConstrains(
$params['db'],
$params['tables']
);

return ['foreignKeyConstrains' => $constrains];
$this->response->addJSON(['foreignKeyConstrains' => $constrains]);
}
}
Expand Up @@ -164,9 +164,9 @@ public function index(): void

$secondaryTabs = [
'multi' => [
'link' => Url::getFromRoute('/database/multi_table_query'),
'link' => Url::getFromRoute('/database/multi-table-query'),
'text' => __('Multi-table query'),
'active' => $route === '/database/multi_table_query',
'active' => $route === '/database/multi-table-query',
],
'qbe' => [
'link' => Url::getFromRoute('/database/qbe'),
Expand Down
7 changes: 3 additions & 4 deletions libraries/classes/Controllers/Database/RoutinesController.php
Expand Up @@ -38,15 +38,14 @@ public function __construct($response, $dbi, Template $template, $db, CheckUserP
$this->checkUserPrivileges = $checkUserPrivileges;
}

/**
* @param array $params Request parameters
*/
public function index(array $params): void
public function index(): void
{
global $_PMA_RTE, $db, $table, $tables, $num_tables, $total_num_tables, $sub_part, $is_show_stats;
global $db_is_system_schema, $tooltip_truename, $tooltip_aliasname, $pos, $url_query;
global $errors, $titles;

$params = ['type' => $_REQUEST['type'] ?? null];

$this->checkUserPrivileges->getPrivileges();

$_PMA_RTE = 'RTN';
Expand Down
Expand Up @@ -10,10 +10,7 @@
*/
class SqlAutoCompleteController extends AbstractController
{
/**
* @return array JSON
*/
public function index(): array
public function index(): void
{
global $cfg, $db, $sql_autocomplete;

Expand All @@ -31,6 +28,6 @@ public function index(): array
}
}
}
return ['tables' => json_encode($sql_autocomplete)];
$this->response->addJSON(['tables' => json_encode($sql_autocomplete)]);
}
}
10 changes: 3 additions & 7 deletions libraries/classes/Controllers/Database/SqlFormatController.php
Expand Up @@ -11,14 +11,10 @@
*/
class SqlFormatController extends AbstractController
{
/**
* @param array $params Request parameters
*
* @return array
*/
public function index(array $params): array
public function index(): void
{
$params = ['sql' => $_POST['sql'] ?? null];
$query = strlen((string) $params['sql']) > 0 ? $params['sql'] : '';
return ['sql' => Formatter::format($query)];
$this->response->addJSON(['sql' => Formatter::format($query)]);
}
}

0 comments on commit d0283d7

Please sign in to comment.