|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Package\Provide\Router; |
|
6
|
|
|
|
|
7
|
|
|
use Aura\Cli\CliFactory; |
|
8
|
|
|
use Aura\Cli\Context\OptionFactory; |
|
9
|
|
|
use Aura\Cli\Status; |
|
10
|
|
|
use Aura\Cli\Stdio; |
|
11
|
|
|
use BEAR\Package\Annotation\StdIn; |
|
12
|
|
|
use BEAR\Sunday\Extension\Router\RouterInterface; |
|
13
|
|
|
use Exception; |
|
14
|
|
|
use Ray\Di\Di\Named; |
|
15
|
|
|
use Throwable; |
|
16
|
|
|
|
|
17
|
|
|
use function basename; |
|
18
|
|
|
use function file_exists; |
|
19
|
|
|
use function file_put_contents; |
|
20
|
|
|
use function http_build_query; |
|
21
|
|
|
use function parse_str; |
|
22
|
|
|
use function parse_url; |
|
23
|
|
|
use function strtoupper; |
|
24
|
|
|
use function unlink; |
|
25
|
|
|
|
|
26
|
|
|
use const PHP_URL_PATH; |
|
27
|
|
|
use const PHP_URL_QUERY; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @psalm-import-type Globals from RouterInterface |
|
31
|
|
|
* @psalm-import-type Server from RouterInterface |
|
32
|
|
|
* @psalm-type CliServer = array{ |
|
33
|
|
|
* argc: int, |
|
34
|
|
|
* argv: array<int, string>, |
|
35
|
|
|
* REQUEST_URI: string, |
|
36
|
|
|
* REQUEST_METHOD: string, |
|
37
|
|
|
* CONTENT_TYPE?: string, |
|
38
|
|
|
* HTTP_CONTENT_TYPE?: string, |
|
39
|
|
|
* HTTP_RAW_POST_DATA?: string |
|
40
|
|
|
* } |
|
41
|
|
|
*/ |
|
42
|
|
|
class CliRouter implements RouterInterface |
|
43
|
|
|
{ |
|
44
|
|
|
private Stdio $stdIo; |
|
45
|
|
|
private Throwable|null $terminateException = null; |
|
46
|
|
|
|
|
47
|
|
|
public function __construct( |
|
48
|
|
|
#[Named('original')] |
|
49
|
|
|
private RouterInterface $router, |
|
50
|
|
|
Stdio|null $stdIo = null, |
|
51
|
|
|
#[StdIn] |
|
52
|
|
|
private string $stdIn = '', |
|
53
|
|
|
) { |
|
54
|
|
|
$this->stdIo = $stdIo ?: (new CliFactory())->newStdio(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function __destruct() |
|
58
|
|
|
{ |
|
59
|
|
|
file_exists($this->stdIn) && unlink($this->stdIn); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function __wakeup(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->stdIo = (new CliFactory())->newStdio(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function setTerminateException(Throwable $e): void |
|
68
|
|
|
{ |
|
69
|
|
|
$this->terminateException = $e; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @psalm-api |
|
74
|
|
|
* @deprecated Use constructor injection |
|
75
|
|
|
* @codeCoverageIgnore |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setStdIn( |
|
78
|
|
|
string $stdIn, |
|
79
|
|
|
): void { |
|
80
|
|
|
$this->stdIn = $stdIn; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritDoc} |
|
85
|
|
|
* |
|
86
|
|
|
* @param Globals $globals |
|
|
|
|
|
|
87
|
|
|
* @param Server $server |
|
|
|
|
|
|
88
|
|
|
*/ |
|
89
|
|
|
public function match(array $globals, array $server) |
|
90
|
|
|
{ |
|
91
|
|
|
/** @var CliServer $server */ |
|
92
|
|
|
$this->validateArgs($server['argc'], $server['argv']); |
|
93
|
|
|
// covert console $_SERVER to web $_SERVER $GLOBALS |
|
94
|
|
|
/** @psalm-suppress InvalidArgument */ |
|
95
|
|
|
[$method, $query, $server] = $this->parseServer($server); |
|
|
|
|
|
|
96
|
|
|
/** @psalm-suppress MixedArgumentTypeCoercion */ |
|
97
|
|
|
[$webGlobals, $webServer] = $this->addQuery($method, $query, $globals, $server); // @phpstan-ignore-line |
|
98
|
|
|
|
|
99
|
|
|
return $this->router->match($webGlobals, $webServer); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritDoc} |
|
104
|
|
|
*/ |
|
105
|
|
|
public function generate($name, $data) |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->router->generate($name, $data); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Set user input query to $globals or &$server |
|
112
|
|
|
* |
|
113
|
|
|
* @param array<string, array<string, mixed>> $query |
|
114
|
|
|
* @param Globals $globals |
|
115
|
|
|
* @param Server $server |
|
116
|
|
|
* |
|
117
|
|
|
* @return array{0:Globals, 1:Server} |
|
118
|
|
|
*/ |
|
119
|
|
|
private function addQuery(string $method, array $query, array $globals, array $server): array |
|
120
|
|
|
{ |
|
121
|
|
|
if ($method === 'get') { |
|
122
|
|
|
$globals['_GET'] = $query; |
|
123
|
|
|
|
|
124
|
|
|
return [$globals, $server]; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if ($method === 'post') { |
|
128
|
|
|
$globals['_POST'] = $query; |
|
129
|
|
|
|
|
130
|
|
|
return [$globals, $server]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$server = $this->getStdIn($method, $query, $server); |
|
134
|
|
|
|
|
135
|
|
|
return [$globals, $server]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
private function error(string $command): void |
|
139
|
|
|
{ |
|
140
|
|
|
$help = new CliRouterHelp(new OptionFactory()); |
|
141
|
|
|
$this->stdIo->outln($help->getHelp($command)); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** @SuppressWarnings(PHPMD) */ |
|
145
|
|
|
private function terminate(int $status): void |
|
146
|
|
|
{ |
|
147
|
|
|
if ($this->terminateException instanceof Exception) { |
|
148
|
|
|
throw $this->terminateException; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// @codeCoverageIgnoreStart |
|
152
|
|
|
exit($status); |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
// @codeCoverageIgnoreEnd |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Return StdIn in PUT, PATCH or DELETE |
|
159
|
|
|
* |
|
160
|
|
|
* @param array<string, array<string, mixed>|string> $query |
|
161
|
|
|
* @param Server $server |
|
162
|
|
|
* |
|
163
|
|
|
* @return Server |
|
164
|
|
|
*/ |
|
165
|
|
|
private function getStdIn(string $method, array $query, array $server): array |
|
166
|
|
|
{ |
|
167
|
|
|
if ($method === 'put' || $method === 'patch' || $method === 'delete') { |
|
168
|
|
|
$server[HttpMethodParams::CONTENT_TYPE] = HttpMethodParams::FORM_URL_ENCODE; |
|
169
|
|
|
file_put_contents($this->stdIn, http_build_query($query)); |
|
170
|
|
|
|
|
171
|
|
|
return $server; |
|
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $server; |
|
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** @param array<int, string> $argv */ |
|
178
|
|
|
private function validateArgs(int $argc, array $argv): void |
|
179
|
|
|
{ |
|
180
|
|
|
if ($argc >= 3) { |
|
181
|
|
|
return; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$this->error(basename($argv[0])); |
|
185
|
|
|
$this->terminate(Status::USAGE); |
|
186
|
|
|
// @codeCoverageIgnoreStart |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
// @codeCoverageIgnoreEnd |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Return $method, $query, $server from $server |
|
193
|
|
|
* |
|
194
|
|
|
* @param Server $server |
|
195
|
|
|
* |
|
196
|
|
|
* @return array{string, array<string, mixed>, Server} |
|
|
|
|
|
|
197
|
|
|
*/ |
|
198
|
|
|
private function parseServer(array $server): array |
|
199
|
|
|
{ |
|
200
|
|
|
/** @var array{argv: array<string>} $server */ |
|
201
|
|
|
[, $method, $uri] = $server['argv']; |
|
202
|
|
|
$urlQuery = (string) parse_url($uri, PHP_URL_QUERY); |
|
203
|
|
|
$urlPath = (string) parse_url($uri, PHP_URL_PATH); |
|
204
|
|
|
$query = []; |
|
205
|
|
|
if ($urlQuery !== '') { |
|
206
|
|
|
parse_str($urlQuery, $query); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$server = [ |
|
210
|
|
|
'REQUEST_METHOD' => strtoupper($method), |
|
211
|
|
|
'REQUEST_URI' => $urlPath, |
|
212
|
|
|
]; |
|
213
|
|
|
|
|
214
|
|
|
/** @var array<string, array<mixed>|string> $query */ |
|
215
|
|
|
return [$method, $query, $server]; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
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