<?php

declare(strict_types=1);

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

namespace TYPO3\CMS\Backend\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Backend\Routing\Exception\ResourceNotFoundException;
use TYPO3\CMS\Backend\Routing\Router;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Http\Error\MethodNotAllowedException;
use TYPO3\CMS\Core\Http\RedirectResponse;
use TYPO3\CMS\Core\Routing\RequestContextFactory;

/**
 * Injects the Router and tries to match the current request with a
 * configured backend route. The available backend routes were added
 * in the corresponding dependency injection factories, which load
 * and process the module and route configuration files
 *
 * - Configuration/Backend/{,Ajax}Routes.php
 * - Configuration/Backend/Modules.php
 *
 * from each extension.
 *
 * After this middleware, a "Route" object is available as attribute in the
 * Request object.
 *
 * @internal
 */
readonly class BackendRouteInitialization implements MiddlewareInterface
{
    public function __construct(
        protected Router $router,
        protected UriBuilder $uriBuilder,
        protected RequestContextFactory $requestContextFactory,
    ) {}

    /**
     * Resolve the route based on the URL path part, and also resolves a Route object
     */
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $this->uriBuilder->setRequestContext($this->requestContextFactory->fromBackendRequest($request));

        try {
            $routeResult = $this->router->matchResult($request);
            $request = $request->withAttribute('routing', $routeResult);
            $request = $request->withAttribute('route', $routeResult->getRoute());
        } catch (MethodNotAllowedException $e) {
            return $e->createResponse();
        } catch (ResourceNotFoundException $e) {
            // Route not found in system
            $uri = $this->uriBuilder->buildUriFromRoute('login');
            return new RedirectResponse($uri);
        }

        return $handler->handle($request);
    }
}
