xref: /webtrees/app/Http/Middleware/Router.php (revision e252129f69fbec0af9c81ab88481e006f8a0c031)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Http\Middleware;
20
21use Aura\Router\RouterContainer;
22use Fisharebest\Webtrees\Services\ModuleService;
23use Fisharebest\Webtrees\Tree;
24use Fisharebest\Webtrees\View;
25use Middleland\Dispatcher;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28use Psr\Http\Server\MiddlewareInterface;
29use Psr\Http\Server\RequestHandlerInterface;
30use function app;
31use function array_map;
32use function http_build_query;
33use const PHP_QUERY_RFC3986;
34
35/**
36 * Simple class to help migrate to a third-party routing library.
37 */
38class Router implements MiddlewareInterface
39{
40    /** @var ModuleService */
41    private $module_service;
42
43    /** @var RouterContainer */
44    private $router_container;
45
46    /**
47     * Router constructor.
48     *
49     * @param ModuleService   $module_service
50     * @param RouterContainer $router_container
51     */
52    public function __construct(ModuleService $module_service, RouterContainer $router_container)
53    {
54        $this->module_service   = $module_service;
55        $this->router_container = $router_container;
56    }
57
58    /**
59     * @param ServerRequestInterface  $request
60     * @param RequestHandlerInterface $handler
61     *
62     * @return ResponseInterface
63     */
64    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
65    {
66        if ((bool) $request->getAttribute('rewrite_urls') === false) {
67            // Turn the ugly URL into a pretty one, so the router can parse it.
68            // Note that the 'route' parameter contains the full path.
69            $uri       = $request->getUri();
70            $params    = $request->getQueryParams();
71            $url_route = $params['route'] ?? '/';
72            $uri       = $uri->withPath($url_route);
73            unset($params['route']);
74            $uri     = $uri->withQuery(http_build_query($params, '', '&', PHP_QUERY_RFC3986));
75            $temp_request = $request->withUri($uri)->withQueryParams($params);
76        } else {
77            $temp_request = $request;
78        }
79
80        // Match the request to a route.
81        $route = $this->router_container->getMatcher()->match($temp_request);
82
83        // No route matched?
84        if ($route === false) {
85            // Bind the request into the container and the layout
86            app()->instance(ServerRequestInterface::class, $request);
87            View::share('request', $request);
88
89            return $handler->handle($request);
90        }
91
92        // Add the route as attribute of the request
93        $request = $request->withAttribute('route', $route->name);
94
95        // Firstly, apply the route middleware
96        $route_middleware = $route->extras['middleware'] ?? [];
97        $route_middleware = array_map('app', $route_middleware);
98
99        // Secondly, apply any module middleware
100        $module_middleware = $this->module_service->findByInterface(MiddlewareInterface::class)->all();
101
102        // Finally, run the handler using middleware
103        $handler_middleware = [new WrapHandler($route->handler)];
104
105        $middleware = array_merge($route_middleware, $module_middleware, $handler_middleware);
106
107        // Add the matched attributes to the request.
108        foreach ($route->attributes as $key => $value) {
109            if ($key === 'tree') {
110                $value = Tree::findByName($value);
111                // @TODO - this is still required for some legacy code.
112                app()->instance(Tree::class, $value);
113            }
114            $request = $request->withAttribute($key, $value);
115        }
116
117        // Bind the request into the container and the layout
118        app()->instance(ServerRequestInterface::class, $request);
119        View::share('request', $request);
120
121        $dispatcher = new Dispatcher($middleware, app());
122
123        return $dispatcher->dispatch($request);
124    }
125}
126