xref: /webtrees/app/Http/Middleware/Router.php (revision 2084f04f8a119431a5c319f5f72b46b3c6da2bdf)
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 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\Middleware;
21
22use Aura\Router\RouterContainer;
23use Fisharebest\Webtrees\Services\ModuleService;
24use Fisharebest\Webtrees\Services\TreeService;
25use Fisharebest\Webtrees\Tree;
26use Fisharebest\Webtrees\View;
27use Middleland\Dispatcher;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\MiddlewareInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function app;
34use function array_map;
35
36/**
37 * Simple class to help migrate to a third-party routing library.
38 */
39class Router implements MiddlewareInterface
40{
41    /** @var ModuleService */
42    private $module_service;
43
44    /** @var RouterContainer */
45    private $router_container;
46
47    /** @var TreeService */
48    private $tree_service;
49
50    /**
51     * Router constructor.
52     *
53     * @param ModuleService   $module_service
54     * @param RouterContainer $router_container
55     * @param TreeService     $tree_service
56     */
57    public function __construct(ModuleService $module_service, RouterContainer $router_container, TreeService $tree_service)
58    {
59        $this->module_service   = $module_service;
60        $this->router_container = $router_container;
61        $this->tree_service     = $tree_service;
62    }
63
64    /**
65     * @param ServerRequestInterface  $request
66     * @param RequestHandlerInterface $handler
67     *
68     * @return ResponseInterface
69     */
70    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
71    {
72        // Turn the ugly URL into a pretty one, so the router can parse it.
73        $pretty = $request;
74
75        if ($request->getAttribute('rewrite_urls') !== '1') {
76            // Ugly URLs store the path in a query parameter.
77            $url_route = $request->getQueryParams()['route'] ?? '';
78            $uri       = $request->getUri()->withPath($url_route);
79            $pretty    = $request->withUri($uri);
80        }
81
82        // Match the request to a route.
83        $route = $this->router_container->getMatcher()->match($pretty);
84
85        // No route matched?
86        if ($route === false) {
87            // Bind the request into the container and the layout
88            app()->instance(ServerRequestInterface::class, $request);
89            View::share('request', $request);
90
91            return $handler->handle($request);
92        }
93
94        // Add the route as attribute of the request
95        $request = $request->withAttribute('route', $route->name);
96
97        // Firstly, apply the route middleware
98        $route_middleware = $route->extras['middleware'] ?? [];
99        $route_middleware = array_map('app', $route_middleware);
100
101        // Secondly, apply any module middleware
102        $module_middleware = $this->module_service->findByInterface(MiddlewareInterface::class)->all();
103
104        // Finally, run the handler using middleware
105        $handler_middleware = [new WrapHandler($route->handler)];
106
107        $middleware = array_merge($route_middleware, $module_middleware, $handler_middleware);
108
109        // Add the matched attributes to the request.
110        foreach ($route->attributes as $key => $value) {
111            if ($key === 'tree') {
112                $value = $this->tree_service->all()->get($value);
113                // @TODO - this is still required by the date formatter.
114                app()->instance(Tree::class, $value);
115                // @TODO - this is still required by various view templates.
116                View::share('tree', $value);
117            }
118            $request = $request->withAttribute($key, $value);
119        }
120
121        // Bind the request into the container and the layout
122        app()->instance(ServerRequestInterface::class, $request);
123        View::share('request', $request);
124
125        $dispatcher = new Dispatcher($middleware, app());
126
127        return $dispatcher->dispatch($request);
128    }
129}
130