Skip to content

Commit

Permalink
Use early exit for Routing::callControllerForRoute
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <[email protected]>
  • Loading branch information
MauricioFauth committed Jul 8, 2020
1 parent f233d2a commit e23f933
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
7 changes: 2 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<?php
/**
* Main loader script
*/

declare(strict_types=1);

Expand All @@ -13,9 +10,9 @@
// 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);
37 changes: 24 additions & 13 deletions libraries/classes/Routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpMyAdmin;

use FastRoute\Dispatcher;
use Psr\Container\ContainerInterface;
use function FastRoute\cachedDispatcher;
use function htmlspecialchars;
use function mb_strlen;
Expand Down Expand Up @@ -50,34 +51,44 @@ public static function getCurrentRoute(): string

/**
* 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]);
}
}

0 comments on commit e23f933

Please sign in to comment.