Passed
Pull Request — master (#63)
by Anatoly
02:15
created

Router::match()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7

Importance

Changes 9
Bugs 0 Features 0
Metric Value
cc 7
eloc 22
c 9
b 0
f 0
nc 14
nop 1
dl 0
loc 39
ccs 22
cts 22
cp 1
crap 7
rs 8.6346
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
namespace Sunrise\Http\Router;
13
14
/**
15
 * Import classes
16
 */
17
use Fig\Http\Message\RequestMethodInterface;
18
use Psr\Http\Message\ResponseInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
use Psr\Http\Server\MiddlewareInterface;
21
use Psr\Http\Server\RequestHandlerInterface;
22
use Sunrise\Http\Router\Exception\MethodNotAllowedException;
23
use Sunrise\Http\Router\Exception\MiddlewareAlreadyExistsException;
24
use Sunrise\Http\Router\Exception\RouteAlreadyExistsException;
25
use Sunrise\Http\Router\Exception\RouteNotFoundException;
26
use Sunrise\Http\Router\Loader\LoaderInterface;
27
use Sunrise\Http\Router\RequestHandler\QueueableRequestHandler;
28
29
/**
30
 * Import functions
31
 */
32
use function array_flip;
33
use function array_keys;
34
use function array_values;
35
use function spl_object_hash;
36
use function sprintf;
37
38
/**
39
 * Router
40
 */
41
class Router implements MiddlewareInterface, RequestHandlerInterface, RequestMethodInterface
42
{
43
44
    /**
45
     * Server Request attribute name for routing error instance
46
     *
47
     * @var string
48
     */
49
    public const ATTR_NAME_FOR_ROUTING_ERROR = '@routing-error';
50
51
    /**
52
     * The router host table
53
     *
54
     * @var array
55
     */
56
    private $hosts = [];
57
58
    /**
59
     * The router routes
60
     *
61
     * @var RouteInterface[]
62
     */
63
    private $routes = [];
64
65
    /**
66
     * The router middlewares
67
     *
68
     * @var MiddlewareInterface[]
69
     */
70
    private $middlewares = [];
71
72
    /**
73
     * Gets the router host table
74
     *
75
     * @return array
76
     *
77
     * @since 2.6.0
78
     */
79 1
    public function getHosts() : array
80
    {
81 1
        return $this->hosts;
82
    }
83
84
    /**
85
     * Gets the router routes
86
     *
87
     * @return RouteInterface[]
88
     */
89 3
    public function getRoutes() : array
90
    {
91 3
        return array_values($this->routes);
92
    }
93
94
    /**
95
     * Gets the router middlewares
96
     *
97
     * @return MiddlewareInterface[]
98
     */
99 5
    public function getMiddlewares() : array
100
    {
101 5
        return array_values($this->middlewares);
102
    }
103
104
    /**
105
     * Adds the given host alias to the router host table
106
     *
107
     * @param string $alias
108
     * @param string $host
109
     *
110
     * @return void
111
     *
112
     * @since 2.6.0
113
     */
114 2
    public function addHost(string $alias, string $host) : void
115
    {
116 2
        $this->hosts[$alias] = $host;
117 2
    }
118
119
    /**
120
     * Adds the given route(s) to the router
121
     *
122
     * @param RouteInterface ...$routes
123
     *
124
     * @return void
125
     *
126
     * @throws RouteAlreadyExistsException
127
     */
128 20
    public function addRoute(RouteInterface ...$routes) : void
129
    {
130 20
        foreach ($routes as $route) {
131 20
            $name = $route->getName();
132
133 20
            if (isset($this->routes[$name])) {
134 1
                throw new RouteAlreadyExistsException(
135 1
                    sprintf('A route with the name "%s" already exists.', $name)
136
                );
137
            }
138
139 20
            $this->routes[$name] = $route;
140
        }
141 20
    }
142
143
    /**
144
     * Adds the given middleware(s) to the router
145
     *
146
     * @param MiddlewareInterface ...$middlewares
147
     *
148
     * @return void
149
     *
150
     * @throws MiddlewareAlreadyExistsException
151
     */
152 4
    public function addMiddleware(MiddlewareInterface ...$middlewares) : void
153
    {
154 4
        foreach ($middlewares as $middleware) {
155 4
            $hash = spl_object_hash($middleware);
156
157 4
            if (isset($this->middlewares[$hash])) {
158 1
                throw new MiddlewareAlreadyExistsException(
159 1
                    sprintf('A middleware with the hash "%s" already exists.', $hash)
160
                );
161
            }
162
163 4
            $this->middlewares[$hash] = $middleware;
164
        }
165 4
    }
166
167
    /**
168
     * Gets allowed methods
169
     *
170
     * @return string[]
171
     */
172 1
    public function getAllowedMethods() : array
173
    {
174 1
        $methods = [];
175 1
        foreach ($this->routes as $route) {
176 1
            foreach ($route->getMethods() as $method) {
177 1
                $methods[$method] = true;
178
            }
179
        }
180
181 1
        return array_keys($methods);
182
    }
183
184
    /**
185
     * Gets a route for the given name
186
     *
187
     * @param string $name
188
     *
189
     * @return RouteInterface
190
     *
191
     * @throws RouteNotFoundException
192
     */
193 3
    public function getRoute(string $name) : RouteInterface
194
    {
195 3
        if (!isset($this->routes[$name])) {
196 1
            throw new RouteNotFoundException(
197 1
                sprintf('No route found for the name "%s".', $name)
198
            );
199
        }
200
201 2
        return $this->routes[$name];
202
    }
203
204
    /**
205
     * Generates a URI for the given named route
206
     *
207
     * @param string $name
208
     * @param array $attributes
209
     * @param bool $strict
210
     *
211
     * @return string
212
     *
213
     * @throws RouteNotFoundException
214
     *         If the given named route wasn't found.
215
     *
216
     * @throws Exception\InvalidAttributeValueException
217
     *         It can be thrown in strict mode, if an attribute value is not valid.
218
     *
219
     * @throws Exception\MissingAttributeValueException
220
     *         If a required attribute value is not given.
221
     */
222 1
    public function generateUri(string $name, array $attributes = [], bool $strict = false) : string
223
    {
224 1
        $route = $this->getRoute($name);
225
226 1
        $attributes += $route->getAttributes();
227
228 1
        return path_build($route->getPath(), $attributes, $strict);
0 ignored issues
show
Bug introduced by
The function path_build was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

228
        return /** @scrutinizer ignore-call */ path_build($route->getPath(), $attributes, $strict);
Loading history...
229
    }
230
231
    /**
232
     * Looks for a route that matches the given request
233
     *
234
     * @param ServerRequestInterface $request
235
     *
236
     * @return RouteInterface
237
     *
238
     * @throws MethodNotAllowedException
239
     * @throws RouteNotFoundException
240
     */
241 12
    public function match(ServerRequestInterface $request) : RouteInterface
242
    {
243 12
        $requestHost = $request->getUri()->getHost();
244 12
        $requestPath = $request->getUri()->getPath();
245 12
        $requestMethod = $request->getMethod();
246 12
        $allowedMethods = [];
247
248 12
        foreach ($this->routes as $route) {
249 12
            $routeHost = $route->getHost();
250 12
            if (isset($routeHost)) {
251 1
                $routeHost = $this->hosts[$routeHost] ?? $routeHost;
252 1
                if ($routeHost <> $requestHost) {
253 1
                    continue;
254
                }
255
            }
256
257
            // https://github.com/sunrise-php/http-router/issues/50
258
            // https://tools.ietf.org/html/rfc7231#section-6.5.5
259 12
            if (!path_match($route->getPath(), $requestPath, $attributes)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $attributes seems to be never defined.
Loading history...
Bug introduced by
The function path_match was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

259
            if (!/** @scrutinizer ignore-call */ path_match($route->getPath(), $requestPath, $attributes)) {
Loading history...
260 9
                continue;
261
            }
262
263 9
            $routeMethods = array_flip($route->getMethods());
264 9
            $allowedMethods += $routeMethods;
265
266 9
            if (!isset($routeMethods[$requestMethod])) {
267 3
                continue;
268
            }
269
270 6
            return $route->withAddedAttributes($attributes);
271
        }
272
273 7
        if (!empty($allowedMethods)) {
274 3
            throw new MethodNotAllowedException('Method Not Allowed', [
275 3
                'allowed' => array_keys($allowedMethods),
276
            ]);
277
        }
278
279 4
        throw new RouteNotFoundException('Route Not Found');
280
    }
281
282
    /**
283
     * {@inheritDoc}
284
     */
285 8
    public function handle(ServerRequestInterface $request) : ResponseInterface
286
    {
287 8
        $route = $this->match($request);
288
289 4
        $handler = new QueueableRequestHandler($route);
290 4
        $handler->add(...$this->getMiddlewares());
291
292 4
        return $handler->handle($request);
293
    }
294
295
    /**
296
     * {@inheritDoc}
297
     */
298 3
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
299
    {
300
        try {
301 3
            return $this->handle($request);
302 2
        } catch (MethodNotAllowedException | RouteNotFoundException $e) {
303 2
            return $handler->handle(
304 2
                $request->withAttribute(self::ATTR_NAME_FOR_ROUTING_ERROR, $e)
305
            );
306
        }
307
    }
308
309
    /**
310
     * Loads routes through the given loaders
311
     *
312
     * @param LoaderInterface ...$loaders
313
     *
314
     * @return void
315
     */
316 2
    public function load(LoaderInterface ...$loaders) : void
317
    {
318 2
        foreach ($loaders as $loader) {
319 2
            $this->addRoute(...$loader->load()->all());
320
        }
321 2
    }
322
}
323