|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Package\Provide\Router; |
|
6
|
|
|
|
|
7
|
|
|
use BEAR\Sunday\Annotation\DefaultSchemeHost; |
|
8
|
|
|
use BEAR\Sunday\Extension\Router\RouterInterface; |
|
9
|
|
|
use BEAR\Sunday\Extension\Router\RouterMatch; |
|
10
|
|
|
|
|
11
|
|
|
use function assert; |
|
12
|
|
|
use function parse_url; |
|
13
|
|
|
|
|
14
|
|
|
class WebRouter implements RouterInterface, WebRouterInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
private $schemeHost; |
|
18
|
|
|
|
|
19
|
|
|
/** @var HttpMethodParamsInterface */ |
|
20
|
|
|
private $httpMethodParams; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @DefaultSchemeHost("schemeHost") |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(string $schemeHost, HttpMethodParamsInterface $httpMethodParams) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->schemeHost = $schemeHost; |
|
28
|
|
|
$this->httpMethodParams = $httpMethodParams; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array{HTTP_X_HTTP_METHOD_OVERRIDE?: string, REQUEST_METHOD: string, REQUEST_URI: string } $server |
|
33
|
|
|
* @param array{_GET: array<string|array>, _POST: array<string|array>} $globals |
|
34
|
|
|
*/ |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function match(array $globals, array $server) |
|
40
|
|
|
{ |
|
41
|
|
|
assert(isset($server['REQUEST_URI'])); |
|
42
|
|
|
assert(isset($server['REQUEST_METHOD'])); |
|
43
|
|
|
$requestUri = $server['REQUEST_URI']; |
|
44
|
|
|
$request = new RouterMatch(); |
|
45
|
|
|
/** @var array{HTTP_X_HTTP_METHOD_OVERRIDE?: string, REQUEST_METHOD: string} $server */ |
|
46
|
|
|
[$request->method, $request->query] = $this->httpMethodParams->get($server, $globals['_GET'], $globals['_POST']); |
|
47
|
|
|
$request->path = $this->schemeHost . parse_url($requestUri, 5); // 5 = PHP_URL_PATH |
|
48
|
|
|
|
|
49
|
|
|
return $request; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function generate($name, $data) |
|
56
|
|
|
{ |
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|