xref: /webtrees/app/Http/Middleware/Router.php (revision 808caab3cae7e4a32422558c1e28d7c68ef8a695)
19e5d8e6fSGreg Roach<?php
29e5d8e6fSGreg Roach
39e5d8e6fSGreg Roach/**
49e5d8e6fSGreg Roach * webtrees: online genealogy
59e5d8e6fSGreg Roach * Copyright (C) 2019 webtrees development team
69e5d8e6fSGreg Roach * This program is free software: you can redistribute it and/or modify
79e5d8e6fSGreg Roach * it under the terms of the GNU General Public License as published by
89e5d8e6fSGreg Roach * the Free Software Foundation, either version 3 of the License, or
99e5d8e6fSGreg Roach * (at your option) any later version.
109e5d8e6fSGreg Roach * This program is distributed in the hope that it will be useful,
119e5d8e6fSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
129e5d8e6fSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
139e5d8e6fSGreg Roach * GNU General Public License for more details.
149e5d8e6fSGreg Roach * You should have received a copy of the GNU General Public License
159e5d8e6fSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
169e5d8e6fSGreg Roach */
179e5d8e6fSGreg Roachdeclare(strict_types=1);
189e5d8e6fSGreg Roach
199e5d8e6fSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
209e5d8e6fSGreg Roach
21ee4364daSGreg Roachuse Aura\Router\RouterContainer;
2282e92bfaSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
23ee4364daSGreg Roachuse Fisharebest\Webtrees\Tree;
24a992e8c1SGreg Roachuse Fisharebest\Webtrees\View;
2582e92bfaSGreg Roachuse Middleland\Dispatcher;
269e5d8e6fSGreg Roachuse Psr\Http\Message\ResponseInterface;
279e5d8e6fSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
289e5d8e6fSGreg Roachuse Psr\Http\Server\MiddlewareInterface;
299e5d8e6fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
309e5d8e6fSGreg Roachuse function app;
3182e92bfaSGreg Roachuse function array_map;
324874f72dSGreg Roachuse function http_build_query;
334874f72dSGreg Roachuse const PHP_QUERY_RFC3986;
349e5d8e6fSGreg Roach
359e5d8e6fSGreg Roach/**
369e5d8e6fSGreg Roach * Simple class to help migrate to a third-party routing library.
379e5d8e6fSGreg Roach */
3871378461SGreg Roachclass Router implements MiddlewareInterface
399e5d8e6fSGreg Roach{
4082e92bfaSGreg Roach    /** @var ModuleService */
4182e92bfaSGreg Roach    private $module_service;
4282e92bfaSGreg Roach
433ecf8b4eSGreg Roach    /** @var RouterContainer */
443ecf8b4eSGreg Roach    private $router_container;
453ecf8b4eSGreg Roach
4682e92bfaSGreg Roach    /**
4782e92bfaSGreg Roach     * Router constructor.
4882e92bfaSGreg Roach     *
4982e92bfaSGreg Roach     * @param ModuleService   $module_service
504874f72dSGreg Roach     * @param RouterContainer $router_container
5182e92bfaSGreg Roach     */
523ecf8b4eSGreg Roach    public function __construct(ModuleService $module_service, RouterContainer $router_container)
5382e92bfaSGreg Roach    {
5482e92bfaSGreg Roach        $this->module_service   = $module_service;
553ecf8b4eSGreg Roach        $this->router_container = $router_container;
569e5d8e6fSGreg Roach    }
579e5d8e6fSGreg Roach
589e5d8e6fSGreg Roach    /**
599e5d8e6fSGreg Roach     * @param ServerRequestInterface  $request
609e5d8e6fSGreg Roach     * @param RequestHandlerInterface $handler
619e5d8e6fSGreg Roach     *
629e5d8e6fSGreg Roach     * @return ResponseInterface
639e5d8e6fSGreg Roach     */
649e5d8e6fSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
659e5d8e6fSGreg Roach    {
664874f72dSGreg Roach        if ((bool) $request->getAttribute('rewrite_urls') === false) {
674874f72dSGreg Roach            // Turn the ugly URL into a pretty one, so the router can parse it.
684874f72dSGreg Roach            // Note that the 'route' parameter contains the full path.
694874f72dSGreg Roach            $uri       = $request->getUri();
7044aaa5c3SGreg Roach            $params    = $request->getQueryParams();
714874f72dSGreg Roach            $url_route = $params['route'] ?? '/';
724874f72dSGreg Roach            $uri       = $uri->withPath($url_route);
7344aaa5c3SGreg Roach            unset($params['route']);
744874f72dSGreg Roach            $uri     = $uri->withQuery(http_build_query($params, '', '&', PHP_QUERY_RFC3986));
75*808caab3SGreg Roach            $temp_request = $request->withUri($uri)->withQueryParams($params);
76*808caab3SGreg Roach        } else {
77*808caab3SGreg Roach            $temp_request = $request;
78ee4364daSGreg Roach        }
799e5d8e6fSGreg Roach
803ecf8b4eSGreg Roach        // Match the request to a route.
81*808caab3SGreg Roach        $route = $this->router_container->getMatcher()->match($temp_request);
823ecf8b4eSGreg Roach
833ecf8b4eSGreg Roach        // No route matched?
843ecf8b4eSGreg Roach        if ($route === false) {
85a992e8c1SGreg Roach            // Bind the request into the container and the layout
869e5d8e6fSGreg Roach            app()->instance(ServerRequestInterface::class, $request);
87a992e8c1SGreg Roach            View::share('request', $request);
889e5d8e6fSGreg Roach
899e5d8e6fSGreg Roach            return $handler->handle($request);
909e5d8e6fSGreg Roach        }
919e5d8e6fSGreg Roach
924874f72dSGreg Roach        // Add the route as attribute of the request
934874f72dSGreg Roach        $request = $request->withAttribute('route', $route->name);
944874f72dSGreg Roach
9582e92bfaSGreg Roach        // Firstly, apply the route middleware
96ee4364daSGreg Roach        $route_middleware = $route->extras['middleware'] ?? [];
9782e92bfaSGreg Roach        $route_middleware = array_map('app', $route_middleware);
989e5d8e6fSGreg Roach
9982e92bfaSGreg Roach        // Secondly, apply any module middleware
10082e92bfaSGreg Roach        $module_middleware = $this->module_service->findByInterface(MiddlewareInterface::class)->all();
1019e5d8e6fSGreg Roach
10282e92bfaSGreg Roach        // Finally, run the handler using middleware
103ee4364daSGreg Roach        $handler_middleware = [new WrapHandler($route->handler)];
10482e92bfaSGreg Roach
10582e92bfaSGreg Roach        $middleware = array_merge($route_middleware, $module_middleware, $handler_middleware);
10682e92bfaSGreg Roach
107ee4364daSGreg Roach        // Add the matched attributes to the request.
108ee4364daSGreg Roach        foreach ($route->attributes as $key => $value) {
109ee4364daSGreg Roach            if ($key === 'tree') {
110ee4364daSGreg Roach                $value = Tree::findByName($value);
1114874f72dSGreg Roach                // @TODO - this is still required for some legacy code.
1123cfcc809SGreg Roach                app()->instance(Tree::class, $value);
113ee4364daSGreg Roach            }
114ee4364daSGreg Roach            $request = $request->withAttribute($key, $value);
115ee4364daSGreg Roach        }
116ee4364daSGreg Roach
1173ecf8b4eSGreg Roach        // Bind the request into the container and the layout
1183ecf8b4eSGreg Roach        app()->instance(ServerRequestInterface::class, $request);
1193ecf8b4eSGreg Roach        View::share('request', $request);
1203ecf8b4eSGreg Roach
12182e92bfaSGreg Roach        $dispatcher = new Dispatcher($middleware, app());
12282e92bfaSGreg Roach
12382e92bfaSGreg Roach        return $dispatcher->dispatch($request);
1249e5d8e6fSGreg Roach    }
1259e5d8e6fSGreg Roach}
126