forked from phpmyadmin/phpmyadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move all index.php code to PhpMyAdmin\Routing and change cache name
Signed-off-by: William Desportes <[email protected]> Fix some phpdoc comments Signed-off-by: William Desportes <[email protected]>
- Loading branch information
1 parent
898a526
commit bde3e5a
Showing
2 changed files
with
86 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
} | ||
} | ||
} |