Test Failed
Pull Request — master (#74)
by Anatoly
05:22 queued 02:58
created

Router::match()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

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

230
        return /** @scrutinizer ignore-call */ path_build($route->getPath(), $attributes, $strict);
Loading history...
231
    }
232
233
    /**
234
     * Looks for a route that matches the given request
235
     *
236
     * @param ServerRequestInterface $request
237
     *
238
     * @return RouteInterface
239
     *
240
     * @throws MethodNotAllowedException
241
     * @throws PageNotFoundException
242 15
     */
243
    public function match(ServerRequestInterface $request) : RouteInterface
244 15
    {
245 15
        $requestHost = $request->getUri()->getHost();
246 15
        $requestPath = $request->getUri()->getPath();
247 15
        $requestMethod = $request->getMethod();
248
        $allowedMethods = [];
249 15
250 15
        foreach ($this->routes as $route) {
251 1
            if (!$this->compareHosts($route->getHost(), $requestHost)) {
252
                continue;
253
            }
254
255
            // https://github.com/sunrise-php/http-router/issues/50
256 15
            // https://tools.ietf.org/html/rfc7231#section-6.5.5
257 12
            if (!path_match($route->getPath(), $requestPath, $attributes)) {
0 ignored issues
show
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

257
            if (!/** @scrutinizer ignore-call */ path_match($route->getPath(), $requestPath, $attributes)) {
Loading history...
Comprehensibility Best Practice introduced by
The variable $attributes seems to be never defined.
Loading history...
258
                continue;
259
            }
260 11
261 11
            $routeMethods = array_flip($route->getMethods());
262
            $allowedMethods += $routeMethods;
263 11
264 4
            if (!isset($routeMethods[$requestMethod])) {
265
                continue;
266
            }
267 7
268
            return $route->withAddedAttributes($attributes);
269
        }
270 9
271 4
        if (!empty($allowedMethods)) {
272 4
            throw new MethodNotAllowedException('Method Not Allowed', [
273
                'method' => $requestMethod,
274
                'allowed' => array_keys($allowedMethods),
275
            ]);
276 5
        }
277
278
        throw new PageNotFoundException('Page Not Found');
279
    }
280
281
    /**
282
     * Runs the router
283
     *
284
     * @param ServerRequestInterface $request
285
     *
286
     * @return ResponseInterface
287
     *
288 3
     * @since 2.8.0
289
     */
290
    public function run(ServerRequestInterface $request) : ResponseInterface
291 3
    {
292 3
        // lazy resolving of the given request...
293 3
        $routing = new CallableRequestHandler(function (ServerRequestInterface $request) : ResponseInterface {
294
            return $this->match($request)->handle($request);
295 3
        });
296 3
297
        $middlewares = $this->getMiddlewares();
298 3
        if (empty($middlewares)) {
299
            return $routing->handle($request);
300
        }
301
302
        $handler = new QueueableRequestHandler($routing);
303
        $handler->add(...$middlewares);
304 8
305
        return $handler->handle($request);
306 8
    }
307
308 4
    /**
309 4
     * {@inheritdoc}
310
     */
311 4
    public function handle(ServerRequestInterface $request) : ResponseInterface
312
    {
313
        $route = $this->match($request);
314
315
        $middlewares = $this->getMiddlewares();
316
        if (empty($middlewares)) {
317 3
            return $route->handle($request);
318
        }
319
320 3
        $handler = new QueueableRequestHandler($route);
321 2
        $handler->add(...$middlewares);
322 2
323
        return $handler->handle($request);
324 2
    }
325
326
    /**
327
     * {@inheritdoc}
328
     */
329
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
330
    {
331
        try {
332
            return $this->handle($request);
333
        } catch (MethodNotAllowedException|PageNotFoundException $e) {
334
            $request = $request->withAttribute(self::ATTR_NAME_FOR_ROUTING_ERROR, $e);
335 2
336
            return $handler->handle($request);
337 2
        }
338 2
    }
339
340 2
    /**
341
     * Loads routes through the given loaders
342
     *
343
     * @param LoaderInterface ...$loaders
344
     *
345
     * @return void
346
     */
347
    public function load(LoaderInterface ...$loaders) : void
348
    {
349
        foreach ($loaders as $loader) {
350
            $this->addRoute(...$loader->load()->all());
351
        }
352
    }
353
354 15
    /**
355
     * Compares the given route host and the given request host
356 15
     *
357 15
     * Returns `true` if the route host is `null`
358
     * or if the route host is equal to the request host,
359
     * otherwise returns `false`.
360
     *
361 1
     * @param null|string $routeHost
362 1
     * @param string $requestHost
363
     *
364
     * @return bool
365 1
     */
366 1
    private function compareHosts(?string $routeHost, string $requestHost) : bool
367
    {
368
        if (null === $routeHost) {
369 1
            return true;
370
        }
371
372
        if ($requestHost === $routeHost) {
373
            return true;
374
        }
375
376
        if (!empty($this->hosts[$routeHost])) {
377
            foreach ($this->hosts[$routeHost] as $hostname) {
378
                if ($hostname === $requestHost) {
379
                    return true;
380
                }
381
            }
382
        }
383
384
        return false;
385
    }
386
}
387