Passed
Push — master ( bb1b97...9e5d8e )
by Greg
05:12
created

Router::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 5
rs 10
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
declare(strict_types=1);
18
19
namespace Fisharebest\Webtrees\Http\Middleware;
20
21
use Fig\Http\Message\RequestMethodInterface;
22
use Illuminate\Support\Str;
23
use Psr\Http\Message\ResponseInterface;
24
use Psr\Http\Message\ServerRequestInterface;
25
use Psr\Http\Server\MiddlewareInterface;
26
use Psr\Http\Server\RequestHandlerInterface;
27
28
use function app;
29
use function explode;
30
31
/**
32
 * Simple class to help migrate to a third-party routing library.
33
 */
34
class Router implements MiddlewareInterface, RequestMethodInterface
35
{
36
    private const CONTROLLER_NAMESPACE = '\\Fisharebest\\Webtrees\\Http\\Controllers\\';
37
38
    // To parse Controller::action
39
    private const SCOPE_OPERATOR = '::';
40
41
    /** @var string[][] */
42
    private $routes = [
43
        self::METHOD_GET  => [],
44
        self::METHOD_POST => [],
45
    ];
46
47
    /**
48
     * @param string $path
49
     * @param string $handler
50
     *
51
     * @return Router
52
     */
53
    public function get(string $path, string $handler): Router
54
    {
55
        return $this->add(self::METHOD_GET, $path, $handler);
56
    }
57
58
    /**
59
     * @param string $method
60
     * @param string $path
61
     * @param string $handler
62
     *
63
     * @return Router
64
     */
65
    private function add(string $method, string $path, string $handler): Router
66
    {
67
        $this->routes[$method][$path] = $handler;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @param string $path
74
     * @param string $handler
75
     *
76
     * @return Router
77
     */
78
    public function post(string $path, string $handler): Router
79
    {
80
        return $this->add(self::METHOD_POST, $path, $handler);
81
    }
82
83
    /**
84
     * @param ServerRequestInterface  $request
85
     * @param RequestHandlerInterface $handler
86
     *
87
     * @return ResponseInterface
88
     */
89
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
90
    {
91
        app()->instance(self::class, $this);
92
        require 'routes/web.php';
93
94
        $method  = $request->getMethod();
95
        $route   = $request->getQueryParams()['route'] ?? '';
96
        $routing = $this->routes[$method][$route] ?? '';
97
98
        // Bind the request into the container
99
        app()->instance(ServerRequestInterface::class, $request);
100
101
        // No route matched?
102
        if ($routing === '') {
103
            return $handler->handle($request);
104
        }
105
106
        // Routes defined using controller::action
107
        if (Str::contains($routing, self::SCOPE_OPERATOR)) {
108
            [$class, $method] = explode(self::SCOPE_OPERATOR, $routing);
109
110
            return app(self::CONTROLLER_NAMESPACE . $class)->$method($request);
111
        }
112
113
        // Routes defined using a request handler
114
        return app($routing)->handle($request);
115
    }
116
}
117