Use early exit for Routing::callControllerForRoute
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
parent
f233d2a210
commit
e23f933d97
@ -1,7 +1,4 @@
|
||||
<?php
|
||||
/**
|
||||
* Main loader script
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
@ -13,9 +10,9 @@ if (! defined('ROOT_PATH')) {
|
||||
// phpcs:enable
|
||||
}
|
||||
|
||||
global $route;
|
||||
global $route, $containerBuilder;
|
||||
|
||||
require_once ROOT_PATH . 'libraries/common.inc.php';
|
||||
|
||||
$dispatcher = Routing::getDispatcher();
|
||||
Routing::callControllerForRoute($route, $dispatcher);
|
||||
Routing::callControllerForRoute($route, $dispatcher, $containerBuilder);
|
||||
|
||||
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace PhpMyAdmin;
|
||||
|
||||
use FastRoute\Dispatcher;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use function FastRoute\cachedDispatcher;
|
||||
use function htmlspecialchars;
|
||||
use function mb_strlen;
|
||||
@ -50,34 +51,44 @@ class Routing
|
||||
|
||||
/**
|
||||
* 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;
|
||||
public static function callControllerForRoute(
|
||||
string $route,
|
||||
Dispatcher $dispatcher,
|
||||
ContainerInterface $container
|
||||
): void {
|
||||
$routeInfo = $dispatcher->dispatch(
|
||||
$_SERVER['REQUEST_METHOD'],
|
||||
rawurldecode($route)
|
||||
);
|
||||
|
||||
if ($routeInfo[0] === Dispatcher::NOT_FOUND) {
|
||||
/** @var Response $response */
|
||||
$response = $containerBuilder->get(Response::class);
|
||||
$response = $container->get(Response::class);
|
||||
$response->setHttpResponseCode(404);
|
||||
Message::error(sprintf(
|
||||
__('Error 404! The page %s was not found.'),
|
||||
'<code>' . htmlspecialchars($route) . '</code>'
|
||||
))->display();
|
||||
} elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
|
||||
/** @var Response $response */
|
||||
$response = $containerBuilder->get(Response::class);
|
||||
$response = $container->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]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($routeInfo[0] !== Dispatcher::FOUND) {
|
||||
return;
|
||||
}
|
||||
|
||||
[$controllerName, $action] = $routeInfo[1];
|
||||
$controller = $container->get($controllerName);
|
||||
$controller->$action($routeInfo[2]);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user