1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the BEAR.Package package. |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
6
|
|
|
*/ |
7
|
|
|
namespace BEAR\Package\Provide\Router; |
8
|
|
|
|
9
|
|
|
use Aura\Cli\CliFactory; |
10
|
|
|
use Aura\Cli\Context\OptionFactory; |
11
|
|
|
use Aura\Cli\Status; |
12
|
|
|
use Aura\Cli\Stdio; |
13
|
|
|
use BEAR\Package\Annotation\StdIn; |
14
|
|
|
use BEAR\Sunday\Extension\Router\RouterInterface; |
15
|
|
|
use Ray\Di\Di\Inject; |
16
|
|
|
use Ray\Di\Di\Named; |
17
|
|
|
|
18
|
|
|
class CliRouter implements RouterInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var RouterInterface |
22
|
|
|
*/ |
23
|
|
|
private $router; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Stdio |
27
|
|
|
*/ |
28
|
|
|
private $stdIo; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $stdIn; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \Exception|null |
37
|
|
|
*/ |
38
|
|
|
private $terminateException; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @Named("original") |
42
|
|
|
*/ |
43
|
14 |
|
public function __construct(RouterInterface $router, Stdio $stdIo = null) |
44
|
|
|
{ |
45
|
14 |
|
$this->router = $router; |
46
|
14 |
|
$this->stdIo = $stdIo ?: (new CliFactory)->newStdio(); |
47
|
14 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
6 |
|
public function __destruct() |
53
|
|
|
{ |
54
|
6 |
|
file_exists($this->stdIn) && unlink($this->stdIn); |
55
|
6 |
|
} |
56
|
|
|
|
57
|
1 |
|
public function setTerminateException(\Exception $e) |
58
|
|
|
{ |
59
|
1 |
|
$this->terminateException = $e; |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @Inject |
64
|
|
|
* @StdIn |
65
|
|
|
*/ |
66
|
14 |
|
public function setStdIn(string $stdIn) |
67
|
|
|
{ |
68
|
14 |
|
$this->stdIn = $stdIn; |
69
|
14 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
7 |
|
public function match(array $globals, array $server) |
75
|
|
|
{ |
76
|
7 |
|
$this->validateArgs($globals); |
77
|
6 |
|
list($method, $query, $server) = $this->parseGlobals($globals); |
78
|
6 |
|
$this->setQuery($method, $query, $globals, $server); |
79
|
|
|
|
80
|
6 |
|
return $this->router->match($globals, $server); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
1 |
|
public function generate($name, $data) |
87
|
|
|
{ |
88
|
1 |
|
return $this->router->generate($name, $data); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set user input query to $globals or &$server |
93
|
|
|
*/ |
94
|
6 |
|
private function setQuery(string $method, array $query, array &$globals, array &$server) |
95
|
|
|
{ |
96
|
6 |
|
if ($method === 'get') { |
97
|
1 |
|
$globals['_GET'] = $query; |
98
|
|
|
|
99
|
1 |
|
return; |
100
|
|
|
} |
101
|
5 |
|
if ($method === 'post') { |
102
|
1 |
|
$globals['_POST'] = $query; |
103
|
|
|
|
104
|
1 |
|
return; |
105
|
|
|
} |
106
|
4 |
|
$server = $this->getStdIn($method, $query, $server); |
107
|
4 |
|
} |
108
|
|
|
|
109
|
1 |
|
private function error(string $command) |
110
|
|
|
{ |
111
|
1 |
|
$help = new CliRouterHelp(new OptionFactory); |
112
|
1 |
|
$this->stdIo->outln($help->getHelp($command)); |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @SuppressWarnings(PHPMD) |
117
|
|
|
*/ |
118
|
1 |
|
private function terminate(int $status) |
119
|
|
|
{ |
120
|
1 |
|
if ($this->terminateException instanceof \Exception) { |
121
|
1 |
|
throw $this->terminateException; |
122
|
|
|
} |
123
|
|
|
// @codeCoverageIgnoreStart |
124
|
|
|
exit($status); |
125
|
|
|
// @codeCoverageIgnoreEnd |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Return StdIn in PUT, PATCH or DELETE |
130
|
|
|
*/ |
131
|
4 |
|
private function getStdIn(string $method, array $query, array &$server) : array |
132
|
|
|
{ |
133
|
4 |
|
if ($method === 'put' || $method === 'patch' || $method === 'delete') { |
134
|
3 |
|
$server[HttpMethodParams::CONTENT_TYPE] = HttpMethodParams::FORM_URL_ENCODE; |
135
|
3 |
|
file_put_contents($this->stdIn, http_build_query($query)); |
136
|
|
|
|
137
|
3 |
|
return $server; |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
return $server; |
141
|
|
|
} |
142
|
|
|
|
143
|
7 |
|
private function validateArgs(array $globals) |
144
|
|
|
{ |
145
|
7 |
|
if ($globals['argc'] !== 3) { |
146
|
1 |
|
$this->error(basename($globals['argv'][0])); |
147
|
1 |
|
$this->terminate(Status::USAGE); |
148
|
|
|
} |
149
|
6 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return $method, $query, $server from $globals |
153
|
|
|
*/ |
154
|
6 |
|
private function parseGlobals(array $globals) : array |
155
|
|
|
{ |
156
|
6 |
|
list(, $method, $uri) = $globals['argv']; |
157
|
6 |
|
$parsedUrl = parse_url($uri); |
158
|
6 |
|
$query = []; |
159
|
6 |
|
if (isset($parsedUrl['query'])) { |
160
|
5 |
|
parse_str($parsedUrl['query'], $query); |
161
|
|
|
} |
162
|
|
|
$server = [ |
163
|
6 |
|
'REQUEST_METHOD' => strtoupper($method), |
164
|
6 |
|
'REQUEST_URI' => $parsedUrl['path'] |
165
|
|
|
]; |
166
|
|
|
|
167
|
6 |
|
return [$method, $query, $server]; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|