phpmyadmin/libraries/classes/Middleware/ConfigLoading.php
Maurício Meneghini Fauth a36c5ae3b7
Create the ConfigLoading middleware
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
2023-08-16 20:51:18 -03:00

47 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpMyAdmin\Middleware;
use Fig\Http\Message\StatusCodeInterface;
use PhpMyAdmin\Config;
use PhpMyAdmin\Exceptions\ConfigException;
use PhpMyAdmin\Http\Factory\ResponseFactory;
use PhpMyAdmin\Template;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use const CONFIG_FILE;
final class ConfigLoading implements MiddlewareInterface
{
public function __construct(
private readonly Config $config,
private readonly Template $template,
private readonly ResponseFactory $responseFactory,
) {
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$this->config->loadAndCheck(CONFIG_FILE);
} catch (ConfigException $exception) {
// Disables template caching because the cache directory is not known yet.
$this->template->disableCache();
$response = $this->responseFactory->createResponse(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR);
return $response->write($this->template->render('error/generic', [
'lang' => 'en',
'dir' => 'ltr',
'error_message' => $exception->getMessage(),
]));
}
return $handler->handle($request);
}
}