Skip to content

Commit

Permalink
Refactor the Routing::callControllerForRoute() method
Browse files Browse the repository at this point in the history
Changes the method to return a Response object when possible.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Jul 27, 2023
1 parent ecf1c78 commit fb39be0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
31 changes: 16 additions & 15 deletions libraries/classes/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,12 @@ private function handle(ServerRequest $request): Response|null
return null;
}

Routing::callControllerForRoute($request, Routing::getDispatcher(), $container);

return null;
return Routing::callControllerForRoute(
$request,
Routing::getDispatcher(),
$container,
$this->responseFactory,
);
}

/**
Expand Down Expand Up @@ -267,30 +270,30 @@ private function handle(ServerRequest $request): Response|null

// TODO: Set SQL modes too.
} else { // end server connecting
$response = ResponseRenderer::getInstance();
$response->setAjax($request->isAjax());
$response->getHeader()->disableMenuAndConsole();
$response->setMinimalFooter();
$responseRenderer = ResponseRenderer::getInstance();
$responseRenderer->setAjax($request->isAjax());
$responseRenderer->getHeader()->disableMenuAndConsole();
$responseRenderer->setMinimalFooter();
}

$response = ResponseRenderer::getInstance();
$response->setAjax($request->isAjax());
$responseRenderer = ResponseRenderer::getInstance();
$responseRenderer->setAjax($request->isAjax());

/**
* There is no point in even attempting to process
* an ajax request if there is a token mismatch
*/
if ($request->isAjax() && $request->isPost() && $GLOBALS['token_mismatch']) {
$response->setRequestStatus(false);
$response->addJSON(
$responseRenderer->setRequestStatus(false);
$responseRenderer->addJSON(
'message',
Message::error(__('Error: Token mismatch')),
);

return null;
}

Profiling::check($GLOBALS['dbi'], $response);
Profiling::check($GLOBALS['dbi'], $responseRenderer);

$container->set('response', ResponseRenderer::getInstance());

Expand All @@ -306,9 +309,7 @@ private function handle(ServerRequest $request): Response|null
$GLOBALS['dbi']->postConnectControl($relation);
}

Routing::callControllerForRoute($request, Routing::getDispatcher(), $container);

return null;
return Routing::callControllerForRoute($request, Routing::getDispatcher(), $container, $this->responseFactory);
}

/**
Expand Down
32 changes: 16 additions & 16 deletions libraries/classes/Routing/Routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
use FastRoute\Dispatcher\GroupCountBased as DispatcherGroupCountBased;
use FastRoute\RouteCollector;
use FastRoute\RouteParser\Std as RouteParserStd;
use Fig\Http\Message\StatusCodeInterface;
use PhpMyAdmin\Controllers\HomeController;
use PhpMyAdmin\Controllers\Setup\MainController;
use PhpMyAdmin\Controllers\Setup\ShowConfigController;
use PhpMyAdmin\Controllers\Setup\ValidateController;
use PhpMyAdmin\Core;
use PhpMyAdmin\Http\Factory\ResponseFactory;
use PhpMyAdmin\Http\Response;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Sanitize;
use PhpMyAdmin\Template;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -127,43 +129,41 @@ public static function callControllerForRoute(
ServerRequest $request,
Dispatcher $dispatcher,
ContainerInterface $container,
): void {
ResponseFactory $responseFactory,
): Response|null {
$route = $request->getRoute();
$routeInfo = $dispatcher->dispatch($request->getMethod(), rawurldecode($route));

if ($routeInfo[0] === Dispatcher::NOT_FOUND) {
/** @var ResponseRenderer $response */
$response = $container->get(ResponseRenderer::class);
$response->setHttpResponseCode(404);
echo Message::error(sprintf(
$response = $responseFactory->createResponse(StatusCodeInterface::STATUS_NOT_FOUND);
$response->getBody()->write(Message::error(sprintf(
__('Error 404! The page %s was not found.'),
'<code>' . htmlspecialchars($route) . '</code>',
))->getDisplay();
))->getDisplay());

return;
return $response;
}

if ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
/** @var ResponseRenderer $response */
$response = $container->get(ResponseRenderer::class);
$response->setHttpResponseCode(405);
echo Message::error(__('Error 405! Request method not allowed.'))->getDisplay();
$response = $responseFactory->createResponse(StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED);
$response->getBody()->write(Message::error(__('Error 405! Request method not allowed.'))->getDisplay());

return;
return $response;
}

if ($routeInfo[0] !== Dispatcher::FOUND) {
return;
return $responseFactory->createResponse(StatusCodeInterface::STATUS_BAD_REQUEST);
}

/** @psalm-var class-string $controllerName */
$controllerName = $routeInfo[1];
/** @var array<string, string> $vars */
$vars = $routeInfo[2];

/** @psalm-var callable(ServerRequest=, array<string, string>=):void $controller */
/** @psalm-var callable(ServerRequest=, array<string, string>=): (Response|null) $controller */
$controller = $container->get($controllerName);
$controller($request, $vars);

return $controller($request, $vars);
}

/** @psalm-assert-if-true array[] $dispatchData */
Expand Down

0 comments on commit fb39be0

Please sign in to comment.