xref: /webtrees/app/Http/Middleware/Router.php (revision 0c0910bf0f275a14f35d2ccdf698f91f79e269d4)
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;
23*0c0910bfSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
24ee4364daSGreg Roachuse Fisharebest\Webtrees\Tree;
25a992e8c1SGreg Roachuse Fisharebest\Webtrees\View;
2682e92bfaSGreg Roachuse Middleland\Dispatcher;
279e5d8e6fSGreg Roachuse Psr\Http\Message\ResponseInterface;
289e5d8e6fSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
299e5d8e6fSGreg Roachuse Psr\Http\Server\MiddlewareInterface;
309e5d8e6fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
31*0c0910bfSGreg Roach
329e5d8e6fSGreg Roachuse function app;
3382e92bfaSGreg Roachuse function array_map;
344874f72dSGreg Roachuse function http_build_query;
35*0c0910bfSGreg Roach
364874f72dSGreg Roachuse const PHP_QUERY_RFC3986;
379e5d8e6fSGreg Roach
389e5d8e6fSGreg Roach/**
399e5d8e6fSGreg Roach * Simple class to help migrate to a third-party routing library.
409e5d8e6fSGreg Roach */
4171378461SGreg Roachclass Router implements MiddlewareInterface
429e5d8e6fSGreg Roach{
4382e92bfaSGreg Roach    /** @var ModuleService */
4482e92bfaSGreg Roach    private $module_service;
4582e92bfaSGreg Roach
463ecf8b4eSGreg Roach    /** @var RouterContainer */
473ecf8b4eSGreg Roach    private $router_container;
483ecf8b4eSGreg Roach
49*0c0910bfSGreg Roach    /** @var TreeService */
50*0c0910bfSGreg Roach    private $tree_service;
51*0c0910bfSGreg Roach
5282e92bfaSGreg Roach    /**
5382e92bfaSGreg Roach     * Router constructor.
5482e92bfaSGreg Roach     *
5582e92bfaSGreg Roach     * @param ModuleService   $module_service
564874f72dSGreg Roach     * @param RouterContainer $router_container
57*0c0910bfSGreg Roach     * @param TreeService     $tree_service
5882e92bfaSGreg Roach     */
59*0c0910bfSGreg Roach    public function __construct(ModuleService $module_service, RouterContainer $router_container, TreeService $tree_service)
6082e92bfaSGreg Roach    {
6182e92bfaSGreg Roach        $this->module_service   = $module_service;
623ecf8b4eSGreg Roach        $this->router_container = $router_container;
63*0c0910bfSGreg Roach        $this->tree_service     = $tree_service;
649e5d8e6fSGreg Roach    }
659e5d8e6fSGreg Roach
669e5d8e6fSGreg Roach    /**
679e5d8e6fSGreg Roach     * @param ServerRequestInterface  $request
689e5d8e6fSGreg Roach     * @param RequestHandlerInterface $handler
699e5d8e6fSGreg Roach     *
709e5d8e6fSGreg Roach     * @return ResponseInterface
719e5d8e6fSGreg Roach     */
729e5d8e6fSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
739e5d8e6fSGreg Roach    {
744874f72dSGreg Roach        if ((bool) $request->getAttribute('rewrite_urls') === false) {
754874f72dSGreg Roach            // Turn the ugly URL into a pretty one, so the router can parse it.
764874f72dSGreg Roach            // Note that the 'route' parameter contains the full path.
774874f72dSGreg Roach            $uri       = $request->getUri();
7844aaa5c3SGreg Roach            $params    = $request->getQueryParams();
794874f72dSGreg Roach            $url_route = $params['route'] ?? '/';
804874f72dSGreg Roach            $uri       = $uri->withPath($url_route);
8144aaa5c3SGreg Roach            unset($params['route']);
824874f72dSGreg Roach            $uri     = $uri->withQuery(http_build_query($params, '', '&', PHP_QUERY_RFC3986));
83808caab3SGreg Roach            $temp_request = $request->withUri($uri)->withQueryParams($params);
84808caab3SGreg Roach        } else {
85808caab3SGreg Roach            $temp_request = $request;
86ee4364daSGreg Roach        }
879e5d8e6fSGreg Roach
883ecf8b4eSGreg Roach        // Match the request to a route.
89808caab3SGreg Roach        $route = $this->router_container->getMatcher()->match($temp_request);
903ecf8b4eSGreg Roach
913ecf8b4eSGreg Roach        // No route matched?
923ecf8b4eSGreg Roach        if ($route === false) {
93a992e8c1SGreg Roach            // Bind the request into the container and the layout
949e5d8e6fSGreg Roach            app()->instance(ServerRequestInterface::class, $request);
95a992e8c1SGreg Roach            View::share('request', $request);
969e5d8e6fSGreg Roach
979e5d8e6fSGreg Roach            return $handler->handle($request);
989e5d8e6fSGreg Roach        }
999e5d8e6fSGreg Roach
1004874f72dSGreg Roach        // Add the route as attribute of the request
1014874f72dSGreg Roach        $request = $request->withAttribute('route', $route->name);
1024874f72dSGreg Roach
10382e92bfaSGreg Roach        // Firstly, apply the route middleware
104ee4364daSGreg Roach        $route_middleware = $route->extras['middleware'] ?? [];
10582e92bfaSGreg Roach        $route_middleware = array_map('app', $route_middleware);
1069e5d8e6fSGreg Roach
10782e92bfaSGreg Roach        // Secondly, apply any module middleware
10882e92bfaSGreg Roach        $module_middleware = $this->module_service->findByInterface(MiddlewareInterface::class)->all();
1099e5d8e6fSGreg Roach
11082e92bfaSGreg Roach        // Finally, run the handler using middleware
111ee4364daSGreg Roach        $handler_middleware = [new WrapHandler($route->handler)];
11282e92bfaSGreg Roach
11382e92bfaSGreg Roach        $middleware = array_merge($route_middleware, $module_middleware, $handler_middleware);
11482e92bfaSGreg Roach
115ee4364daSGreg Roach        // Add the matched attributes to the request.
116ee4364daSGreg Roach        foreach ($route->attributes as $key => $value) {
117ee4364daSGreg Roach            if ($key === 'tree') {
118*0c0910bfSGreg Roach                $value = $this->tree_service->findByName($value);
119*0c0910bfSGreg Roach                // @TODO - this is still required by the date formatter.
1203cfcc809SGreg Roach                app()->instance(Tree::class, $value);
121*0c0910bfSGreg Roach                // @TODO - this is still required by various view templates.
122*0c0910bfSGreg Roach                View::share('tree', $value);
123ee4364daSGreg Roach            }
124ee4364daSGreg Roach            $request = $request->withAttribute($key, $value);
125ee4364daSGreg Roach        }
126ee4364daSGreg Roach
1273ecf8b4eSGreg Roach        // Bind the request into the container and the layout
1283ecf8b4eSGreg Roach        app()->instance(ServerRequestInterface::class, $request);
1293ecf8b4eSGreg Roach        View::share('request', $request);
1303ecf8b4eSGreg Roach
13182e92bfaSGreg Roach        $dispatcher = new Dispatcher($middleware, app());
13282e92bfaSGreg Roach
13382e92bfaSGreg Roach        return $dispatcher->dispatch($request);
1349e5d8e6fSGreg Roach    }
1359e5d8e6fSGreg Roach}
136