Move all index.php code to PhpMyAdmin\Routing and change cache name
Signed-off-by: William Desportes <williamdes@wdes.fr> Fix some phpdoc comments Signed-off-by: William Desportes <williamdes@wdes.fr>
This commit is contained in:
parent
898a526dfa
commit
bde3e5ae65
53
index.php
53
index.php
@ -4,10 +4,7 @@
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
use FastRoute\Dispatcher;
|
||||
use PhpMyAdmin\Message;
|
||||
use PhpMyAdmin\Response;
|
||||
use function FastRoute\cachedDispatcher;
|
||||
use PhpMyAdmin\Routing;
|
||||
|
||||
if (! defined('ROOT_PATH')) {
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
@ -15,22 +12,9 @@ if (! defined('ROOT_PATH')) {
|
||||
// phpcs:enable
|
||||
}
|
||||
|
||||
global $containerBuilder, $route, $cfg;
|
||||
global $route;
|
||||
|
||||
/** @var string $route */
|
||||
$route = $_GET['route'] ?? $_POST['route'] ?? '/';
|
||||
|
||||
/**
|
||||
* See FAQ 1.34.
|
||||
*
|
||||
* @see https://docs.phpmyadmin.net/en/latest/faq.html#faq1-34
|
||||
*/
|
||||
if (($route === '/' || $route === '') && isset($_GET['db']) && mb_strlen($_GET['db']) !== 0) {
|
||||
$route = '/database/structure';
|
||||
if (isset($_GET['table']) && mb_strlen($_GET['table']) !== 0) {
|
||||
$route = '/sql';
|
||||
}
|
||||
}
|
||||
$route = Routing::getCurrentRoute();
|
||||
|
||||
if ($route === '/import-status') {
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
@ -40,32 +24,5 @@ if ($route === '/import-status') {
|
||||
|
||||
require_once ROOT_PATH . 'libraries/common.inc.php';
|
||||
|
||||
$routes = require ROOT_PATH . 'libraries/routes.php';
|
||||
/** @var \PhpMyAdmin\Config|null $config */
|
||||
$config = $GLOBALS['PMA_Config'];
|
||||
$dispatcher = cachedDispatcher($routes, [
|
||||
'cacheFile' => $config !== null ? $config->getTempDir('routing') . '/routes.cache' : null,
|
||||
'cacheDisabled' => ($cfg['environment'] ?? '') === 'development',
|
||||
]);
|
||||
$routeInfo = $dispatcher->dispatch(
|
||||
$_SERVER['REQUEST_METHOD'],
|
||||
rawurldecode($route)
|
||||
);
|
||||
if ($routeInfo[0] === Dispatcher::NOT_FOUND) {
|
||||
/** @var Response $response */
|
||||
$response = $containerBuilder->get(Response::class);
|
||||
$response->setHttpResponseCode(404);
|
||||
Message::error(sprintf(
|
||||
__('Error 404! The page %s was not found.'),
|
||||
'<code>' . $route . '</code>'
|
||||
))->display();
|
||||
} elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
|
||||
/** @var Response $response */
|
||||
$response = $containerBuilder->get(Response::class);
|
||||
$response->setHttpResponseCode(405);
|
||||
Message::error(__('Error 405! Request method not allowed.'))->display();
|
||||
} elseif ($routeInfo[0] === Dispatcher::FOUND) {
|
||||
[$controllerName, $action] = $routeInfo[1];
|
||||
$controller = $containerBuilder->get($controllerName);
|
||||
$controller->$action($routeInfo[2]);
|
||||
}
|
||||
$dispatcher = Routing::getDispatcher();
|
||||
Routing::callControllerForRoute($route, $dispatcher);
|
||||
|
||||
81
libraries/classes/Routing.php
Normal file
81
libraries/classes/Routing.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin;
|
||||
|
||||
use PhpMyAdmin\Message;
|
||||
use PhpMyAdmin\Response;
|
||||
use FastRoute\Dispatcher;
|
||||
use function FastRoute\cachedDispatcher;
|
||||
use function mb_strlen;
|
||||
use function rawurldecode;
|
||||
|
||||
/**
|
||||
* Class used to warm up the routing cache and manage routing.
|
||||
*/
|
||||
class Routing
|
||||
{
|
||||
|
||||
public static function getDispatcher(): Dispatcher
|
||||
{
|
||||
global $cfg;
|
||||
|
||||
$routes = require ROOT_PATH . 'libraries/routes.php';
|
||||
return cachedDispatcher($routes, [
|
||||
'cacheFile' => ROOT_PATH . 'libraries/cache/routes.cache',
|
||||
'cacheDisabled' => ($cfg['environment'] ?? '') === 'development',
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getCurrentRoute(): string
|
||||
{
|
||||
/** @var string $route */
|
||||
$route = $_GET['route'] ?? $_POST['route'] ?? '/';
|
||||
|
||||
/**
|
||||
* See FAQ 1.34.
|
||||
*
|
||||
* @see https://docs.phpmyadmin.net/en/latest/faq.html#faq1-34
|
||||
*/
|
||||
if (($route === '/' || $route === '') && isset($_GET['db']) && mb_strlen($_GET['db']) !== 0) {
|
||||
$route = '/database/structure';
|
||||
if (isset($_GET['table']) && mb_strlen($_GET['table']) !== 0) {
|
||||
$route = '/sql';
|
||||
}
|
||||
}
|
||||
return $route;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call associated controller for a route using the dispatcher
|
||||
* @param string $route The current route
|
||||
* @param Dispatcher $dispatcher The dispatcher
|
||||
*/
|
||||
public static function callControllerForRoute(string $route, Dispatcher $dispatcher): void
|
||||
{
|
||||
global $containerBuilder;
|
||||
$routeInfo = $dispatcher->dispatch(
|
||||
$_SERVER['REQUEST_METHOD'],
|
||||
rawurldecode($route)
|
||||
);
|
||||
if ($routeInfo[0] === Dispatcher::NOT_FOUND) {
|
||||
/** @var Response $response */
|
||||
$response = $containerBuilder->get(Response::class);
|
||||
$response->setHttpResponseCode(404);
|
||||
Message::error(sprintf(
|
||||
__('Error 404! The page %s was not found.'),
|
||||
'<code>' . $route . '</code>'
|
||||
))->display();
|
||||
} elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
|
||||
/** @var Response $response */
|
||||
$response = $containerBuilder->get(Response::class);
|
||||
$response->setHttpResponseCode(405);
|
||||
Message::error(__('Error 405! Request method not allowed.'))->display();
|
||||
} elseif ($routeInfo[0] === Dispatcher::FOUND) {
|
||||
[$controllerName, $action] = $routeInfo[1];
|
||||
$controller = $containerBuilder->get($controllerName);
|
||||
$controller->$action($routeInfo[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user