|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Igni\Http; |
|
4
|
|
|
|
|
5
|
|
|
use FastRoute\DataGenerator; |
|
6
|
|
|
use FastRoute\Dispatcher; |
|
7
|
|
|
use FastRoute\Dispatcher\GroupCountBased; |
|
8
|
|
|
use FastRoute\RouteParser; |
|
9
|
|
|
use Igni\Http\Exception\MethodNotAllowedException; |
|
10
|
|
|
use Igni\Http\Exception\NotFoundException; |
|
11
|
|
|
use Igni\Http\Exception\RouterException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Utility class for simplifying api for nikic router. |
|
15
|
|
|
* |
|
16
|
|
|
* @package Igni\Http |
|
17
|
|
|
*/ |
|
18
|
|
|
class Router |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var RouteParser |
|
22
|
|
|
*/ |
|
23
|
|
|
private $routeParser; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var DataGenerator |
|
27
|
|
|
*/ |
|
28
|
|
|
private $dataGenerator; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Router constructor. |
|
32
|
|
|
* @param RouteParser $routeParser |
|
33
|
|
|
* @param DataGenerator $dataGenerator |
|
34
|
|
|
*/ |
|
35
|
17 |
|
public function __construct(RouteParser $routeParser, DataGenerator $dataGenerator) |
|
36
|
|
|
{ |
|
37
|
17 |
|
$this->routeParser = $routeParser; |
|
38
|
17 |
|
$this->dataGenerator = $dataGenerator; |
|
39
|
17 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Registers new route. |
|
43
|
|
|
* |
|
44
|
|
|
* @param Route $route |
|
45
|
|
|
*/ |
|
46
|
13 |
|
public function addRoute(Route $route): void |
|
47
|
|
|
{ |
|
48
|
13 |
|
if (!$route instanceof Route) { |
|
|
|
|
|
|
49
|
|
|
throw RouterException::invalidRoute($route); |
|
50
|
|
|
} |
|
51
|
13 |
|
$routeDatas = $this->routeParser->parse($route->getExpression()); |
|
52
|
13 |
|
foreach ((array) $route->getMethod() as $method) { |
|
53
|
13 |
|
foreach ($routeDatas as $routeData) { |
|
54
|
13 |
|
$this->dataGenerator->addRoute($method, $routeData, $route); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
13 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Finds route matching clients request. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $method request method. |
|
63
|
|
|
* @param string $uri request uri. |
|
64
|
|
|
* @return Route |
|
65
|
|
|
*/ |
|
66
|
14 |
|
public function findRoute(string $method, string $uri): Route |
|
67
|
|
|
{ |
|
68
|
14 |
|
$dispatcher = new GroupCountBased($this->getData()); |
|
69
|
14 |
|
$info = $dispatcher->dispatch($method, $uri); |
|
70
|
|
|
|
|
71
|
14 |
|
switch ($info[0]) { |
|
72
|
14 |
|
case Dispatcher::NOT_FOUND: |
|
73
|
3 |
|
throw NotFoundException::notFound($uri, $method); |
|
74
|
|
|
|
|
75
|
11 |
|
case Dispatcher::METHOD_NOT_ALLOWED: |
|
76
|
1 |
|
$allowedMethods = $info[1]; |
|
77
|
1 |
|
throw MethodNotAllowedException::methodNotAllowed($uri, $method, $allowedMethods); |
|
78
|
|
|
|
|
79
|
10 |
|
case Dispatcher::FOUND: |
|
80
|
10 |
|
return $info[1]->withAttributes($info[2]); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
14 |
|
protected function getData() |
|
85
|
|
|
{ |
|
86
|
14 |
|
return $this->dataGenerator->getData(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|