1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\Sunday\Provide\Router; |
6
|
|
|
|
7
|
|
|
use BEAR\Sunday\Annotation\DefaultSchemeHost; |
8
|
|
|
use BEAR\Sunday\Exception\BadRequestJsonException; |
9
|
|
|
use BEAR\Sunday\Extension\Router\RouterInterface; |
10
|
|
|
use BEAR\Sunday\Extension\Router\RouterMatch; |
11
|
|
|
|
12
|
|
|
use function file_get_contents; |
13
|
|
|
use function json_decode; |
14
|
|
|
use function json_last_error; |
15
|
|
|
use function json_last_error_msg; |
16
|
|
|
use function parse_str; |
17
|
|
|
use function parse_url; |
18
|
|
|
use function rtrim; |
19
|
|
|
use function strpos; |
20
|
|
|
use function strtolower; |
21
|
5 |
|
|
22
|
|
|
use const JSON_ERROR_NONE; |
23
|
5 |
|
use const PHP_URL_PATH; |
24
|
5 |
|
|
25
|
|
|
/** |
26
|
|
|
* @psalm-import-type Globals from RouterInterface |
27
|
|
|
* @psalm-import-type Server from RouterInterface |
28
|
|
|
*/ |
29
|
2 |
|
final class WebRouter implements RouterInterface |
30
|
|
|
{ |
31
|
2 |
|
/** |
32
|
2 |
|
* @readonly |
33
|
2 |
|
* @var string |
34
|
2 |
|
*/ |
35
|
2 |
|
private $schemeHost; |
36
|
2 |
|
|
37
|
|
|
/** |
38
|
|
|
* @DefaultSchemeHost |
39
|
2 |
|
*/ |
40
|
|
|
#[DefaultSchemeHost] |
41
|
|
|
public function __construct(string $schemeHost) |
42
|
|
|
{ |
43
|
|
|
$this->schemeHost = $schemeHost; |
44
|
|
|
} |
45
|
1 |
|
|
46
|
|
|
/** |
47
|
1 |
|
* {@inheritdoc} |
48
|
|
|
* |
49
|
|
|
* @param Globals $globals |
|
|
|
|
50
|
|
|
* @param Server $server |
|
|
|
|
51
|
|
|
*/ |
52
|
|
|
public function match(array $globals, array $server) |
53
|
|
|
{ |
54
|
|
|
$method = strtolower($server['REQUEST_METHOD']); |
55
|
|
|
|
56
|
|
|
return new RouterMatch( |
57
|
|
|
$method, |
58
|
|
|
$this->schemeHost . parse_url($server['REQUEST_URI'], PHP_URL_PATH), |
59
|
|
|
$this->getQuery($method, $globals, $server) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function generate($name, $data) |
67
|
|
|
{ |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return request query by media-type |
73
|
|
|
* |
74
|
|
|
* @param Server $server |
75
|
|
|
* @param Globals $globals |
76
|
|
|
* |
77
|
|
|
* @return array<string, mixed> |
78
|
|
|
*/ |
79
|
|
|
private function getQuery(string $method, array $globals, array $server): array |
80
|
|
|
{ |
81
|
|
|
if ($method === 'get') { |
82
|
|
|
return $globals['_GET']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($method === 'post') { |
86
|
|
|
return $globals['_POST']; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$contentType = $server['CONTENT_TYPE'] ?? $server['HTTP_CONTENT_TYPE'] ?? ''; |
90
|
|
|
$isFormUrlEncoded = strpos($contentType, 'application/x-www-form-urlencoded') !== false; |
91
|
|
|
$rawBody = $server['HTTP_RAW_POST_DATA'] ?? rtrim((string) file_get_contents('php://input')); |
92
|
|
|
if ($isFormUrlEncoded) { |
93
|
|
|
parse_str(rtrim($rawBody), $put); |
94
|
|
|
|
95
|
|
|
/** @var array<string, mixed> $put */ |
96
|
|
|
return $put; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$isApplicationJson = strpos($contentType, 'application/json') !== false; |
100
|
|
|
if (! $isApplicationJson) { |
101
|
|
|
return []; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** @var array<string, mixed> $content */ |
105
|
|
|
$content = json_decode($rawBody, true); |
106
|
|
|
$error = json_last_error(); |
107
|
|
|
if ($error !== JSON_ERROR_NONE) { |
108
|
|
|
throw new BadRequestJsonException(json_last_error_msg()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $content; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths