xref: /webtrees/app/Http/Middleware/Router.php (revision d72b284a0846ca045e548a1c77ad11813bcbab92)
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;
32
33/**
34 * Simple class to help migrate to a third-party routing library.
35 */
36class Router implements MiddlewareInterface
37{
38    /** @var ModuleService */
39    private $module_service;
40
41    /** @var RouterContainer */
42    private $router_container;
43
44    /**
45     * Router constructor.
46     *
47     * @param ModuleService $module_service
48     */
49    public function __construct(ModuleService $module_service, RouterContainer $router_container)
50    {
51        $this->module_service   = $module_service;
52        $this->router_container = $router_container;
53    }
54
55    /**
56     * @param ServerRequestInterface  $request
57     * @param RequestHandlerInterface $handler
58     *
59     * @return ResponseInterface
60     */
61    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
62    {
63        if ($request->getAttribute('rewrite_urls') !== '1') {
64            // Turn the ugly URL into a pretty one.
65            $params     = $request->getQueryParams();
66            $route_name = $params['route'] ?? '';
67            unset($params['route']);
68            $uri     = $request->getUri()->withPath($route_name);
69            $request = $request->withUri($uri)->withQueryParams($params);
70        }
71
72        // Match the request to a route.
73        $route = $this->router_container->getMatcher()->match($request);
74
75        // No route matched?
76        if ($route === false) {
77            // Bind the request into the container and the layout
78            app()->instance(ServerRequestInterface::class, $request);
79            View::share('request', $request);
80
81            return $handler->handle($request);
82        }
83
84        // Firstly, apply the route middleware
85        $route_middleware = $route->extras['middleware'] ?? [];
86        $route_middleware = array_map('app', $route_middleware);
87
88        // Secondly, apply any module middleware
89        $module_middleware = $this->module_service->findByInterface(MiddlewareInterface::class)->all();
90
91        // Add the route as attribute of the request
92        $request = $request->withAttribute('route', $route->name);
93
94        // Finally, run the handler using middleware
95        $handler_middleware = [new WrapHandler($route->handler)];
96
97        $middleware = array_merge($route_middleware, $module_middleware, $handler_middleware);
98
99        // Add the matched attributes to the request.
100        foreach ($route->attributes as $key => $value) {
101            if ($key === 'tree') {
102                $value = Tree::findByName($value);
103                // @TODO - this is required for some legacy code.
104                app()->instance(Tree::class, $value);
105            }
106            $request = $request->withAttribute($key, $value);
107        }
108
109        // Bind the request into the container and the layout
110        app()->instance(ServerRequestInterface::class, $request);
111        View::share('request', $request);
112
113        $dispatcher = new Dispatcher($middleware, app());
114
115        return $dispatcher->dispatch($request);
116    }
117}
118