1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace jschreuder\Middle\Router; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
6
|
|
|
use Symfony\Component\Routing\Exception\ExceptionInterface as SymfonyRoutingException; |
7
|
|
|
use Symfony\Component\Routing\Generator\UrlGenerator; |
8
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher; |
9
|
|
|
use Symfony\Component\Routing\RequestContext; |
10
|
|
|
use Symfony\Component\Routing\Route; |
11
|
|
|
use Symfony\Component\Routing\RouteCollection; |
12
|
|
|
use Closure; |
13
|
|
|
|
14
|
|
|
final class SymfonyRouter implements RouterInterface |
15
|
|
|
{ |
16
|
|
|
private readonly RouteCollection $router; |
17
|
|
|
private readonly SymfonyUrlGenerator $generator; |
18
|
|
|
|
19
|
19 |
|
public function __construct( |
20
|
|
|
private readonly string $baseUrl, |
21
|
|
|
?RouteCollection $router = null, |
22
|
|
|
?SymfonyUrlGenerator $generator = null |
23
|
|
|
) |
24
|
|
|
{ |
25
|
19 |
|
$this->router = $router ?? new RouteCollection(); |
26
|
19 |
|
$this->generator = $generator ?? new SymfonyUrlGenerator( |
27
|
19 |
|
new UrlGenerator( |
28
|
19 |
|
$this->router, |
29
|
19 |
|
new RequestContext($this->baseUrl, 'GET', parse_url($this->baseUrl, PHP_URL_HOST) ?: 'localhost') |
30
|
19 |
|
) |
31
|
19 |
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
15 |
|
public function parseRequest(ServerRequestInterface $request): RouteMatchInterface |
35
|
|
|
{ |
36
|
|
|
try { |
37
|
15 |
|
$matcher = new UrlMatcher($this->router, $this->getRequestContext($request)); |
38
|
15 |
|
$routeMatch = $matcher->match($request->getUri()->getPath()); |
39
|
|
|
|
40
|
13 |
|
return new RouteMatch( |
41
|
13 |
|
($routeMatch['controller'])(), |
42
|
13 |
|
array_diff_key($routeMatch, array_flip(['controller', '_route'])) |
43
|
13 |
|
); |
44
|
2 |
|
} catch (SymfonyRoutingException $exception) { |
45
|
2 |
|
return new NoRouteMatch(); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
15 |
|
private function getRequestContext(ServerRequestInterface $request): RequestContext |
50
|
|
|
{ |
51
|
15 |
|
return new RequestContext( |
52
|
15 |
|
$this->baseUrl, |
53
|
15 |
|
$request->getMethod(), |
54
|
15 |
|
$request->getUri()->getHost(), |
55
|
15 |
|
$request->getUri()->getScheme(), |
56
|
15 |
|
80, |
57
|
15 |
|
443, |
58
|
15 |
|
$request->getUri()->getPath(), |
59
|
15 |
|
$request->getUri()->getQuery() |
60
|
15 |
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
public function getGenerator(): UrlGeneratorInterface |
64
|
|
|
{ |
65
|
1 |
|
return $this->generator; |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
public function registerRoutes(RoutingProviderInterface $routingProvider): void |
69
|
|
|
{ |
70
|
3 |
|
$routingProvider->registerRoutes($this); |
71
|
|
|
} |
72
|
|
|
|
73
|
13 |
|
public function match( |
74
|
|
|
string $name, |
75
|
|
|
string $methods, |
76
|
|
|
string $path, |
77
|
|
|
callable $controllerFactory, |
78
|
|
|
array $defaults = [], |
79
|
|
|
array $requirements = [], |
80
|
|
|
?Closure $configCallback = null |
81
|
|
|
): void |
82
|
|
|
{ |
83
|
13 |
|
$symfonyRoute = new Route($path, $defaults, $requirements); |
84
|
13 |
|
$symfonyRoute->setMethods(explode('|', $methods)) |
85
|
13 |
|
->setDefault('controller', $controllerFactory); |
86
|
13 |
|
if ($configCallback) { |
87
|
1 |
|
$configCallback($symfonyRoute); |
88
|
|
|
} |
89
|
13 |
|
$this->router->add($name, $symfonyRoute); |
90
|
|
|
} |
91
|
|
|
|
92
|
5 |
|
public function get( |
93
|
|
|
string $name, |
94
|
|
|
string $path, |
95
|
|
|
callable $controllerFactory, |
96
|
|
|
array $defaults = [], |
97
|
|
|
array $requirements = [], |
98
|
|
|
?Closure $configCallback = null |
99
|
|
|
): void |
100
|
|
|
{ |
101
|
5 |
|
$this->match($name, 'GET', $path, $controllerFactory, $defaults, $requirements, $configCallback); |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
public function post( |
105
|
|
|
string $name, |
106
|
|
|
string $path, |
107
|
|
|
callable $controllerFactory, |
108
|
|
|
array $defaults = [], |
109
|
|
|
array $requirements = [], |
110
|
|
|
?Closure $configCallback = null |
111
|
|
|
): void |
112
|
|
|
{ |
113
|
3 |
|
$this->match($name, 'POST', $path, $controllerFactory, $defaults, $requirements, $configCallback); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
public function put( |
117
|
|
|
string $name, |
118
|
|
|
string $path, |
119
|
|
|
callable $controllerFactory, |
120
|
|
|
array $defaults = [], |
121
|
|
|
array $requirements = [], |
122
|
|
|
?Closure $configCallback = null |
123
|
|
|
): void |
124
|
|
|
{ |
125
|
1 |
|
$this->match($name, 'PUT', $path, $controllerFactory, $defaults, $requirements, $configCallback); |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
public function patch( |
129
|
|
|
string $name, |
130
|
|
|
string $path, |
131
|
|
|
callable $controllerFactory, |
132
|
|
|
array $defaults = [], |
133
|
|
|
array $requirements = [], |
134
|
|
|
?Closure $configCallback = null |
135
|
|
|
): void |
136
|
|
|
{ |
137
|
1 |
|
$this->match($name, 'PATCH', $path, $controllerFactory, $defaults, $requirements, $configCallback); |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
public function delete( |
141
|
|
|
string $name, |
142
|
|
|
string $path, |
143
|
|
|
callable $controllerFactory, |
144
|
|
|
array $defaults = [], |
145
|
|
|
array $requirements = [], |
146
|
|
|
?Closure $configCallback = null |
147
|
|
|
): void |
148
|
|
|
{ |
149
|
1 |
|
$this->match($name, 'DELETE', $path, $controllerFactory, $defaults, $requirements, $configCallback); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|