-
Notifications
You must be signed in to change notification settings - Fork 4
/
Command.php
420 lines (362 loc) · 9.57 KB
/
Command.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php declare(strict_types=1);
namespace Alexkart\CurlBuilder;
use Psr\Http\Message\RequestInterface;
final class Command
{
/**
* Template part which represents command name
*/
public const TEMPLATE_COMMAND_NAME = '{name}';
/**
* Template part which represents command line options
*/
public const TEMPLATE_OPTIONS = '{options}';
/**
* Template part which represents url
*/
public const TEMPLATE_URL = '{url}';
/**
* Single quote character
*/
public const QUOTE_SINGLE = "'";
/**
* Double quote character
*/
public const QUOTE_DOUBLE = '"';
/**
* No quote character
*/
public const QUOTE_NONE = '';
/**
* Command name
*/
private const COMMAND_NAME = 'curl';
/**
* Header names that can use %x2C (",") character, so multiple header fields can't be folded into a single header field
*/
private const HEADER_EXCEPTIONS = [
'set-cookie',
'www-authenticate',
'proxy-authenticate',
];
/**
* @var string built command
*/
private $command = '';
/**
* @var string url
*/
private $url = '';
/**
* @var string command template
*/
private $template;
/**
* @var array<mixed> command line options
*/
private $options = [];
/**
* Character used to quote arguments
* @var string
*/
private $quoteCharacter;
/**
* @var RequestInterface|null
*/
private $request;
public function __construct()
{
$this->initTemplate();
$this->initQuoteCharacter();
}
/**
* Generates command
* @return string
*/
public function build(): string
{
$this->setCommand($this->getTemplate());
$this->buildName();
$this->buildOptions();
$this->buildUrl();
return $this->getCommand();
}
/**
* @param string $url
* @return Command
*/
public function setUrl(string $url): Command
{
$this->url = $url;
return $this;
}
/**
* @return string
*/
public function getUrl(): string
{
return $this->url;
}
/**
* @param string $template
* @return Command
*/
public function setTemplate(string $template): Command
{
$this->template = $template;
return $this;
}
/**
* @return string
*/
public function getTemplate(): string
{
return $this->template;
}
/**
* @param array<mixed> $options
* @return Command
*/
public function setOptions(array $options): Command
{
$this->options = $this->toInternalFormat($options);
return $this;
}
/**
* @return array<mixed>
*/
public function getOptions(): array
{
return $this->options;
}
/**
* @param string $option
* @param string|null $argument
* @return Command
*/
public function addOption(string $option, $argument = null): Command
{
$this->options[$option][] = $argument;
return $this;
}
/**
* @param array<mixed> $options
*/
public function addOptions(array $options): void
{
$options = $this->toInternalFormat($options);
foreach ($options as $option => $arguments) {
foreach ($arguments as $argument) {
$this->addOption((string)$option, $argument);
}
}
}
/**
* @return string
*/
public function getCommand(): string
{
return $this->command;
}
/**
* @param string $command
*/
public function setCommand(string $command): void
{
$this->command = $command;
}
/**
* Inits default command template
*/
private function initTemplate(): void
{
$this->setTemplate(self::TEMPLATE_COMMAND_NAME . self::TEMPLATE_OPTIONS . self::TEMPLATE_URL);
}
/**
* Inits default quote character
*/
private function initQuoteCharacter(): void
{
$this->setQuoteCharacter(self::QUOTE_SINGLE);
}
/**
* Builds command name
*/
private function buildName(): void
{
$this->buildTemplatePart(self::TEMPLATE_COMMAND_NAME, self::COMMAND_NAME);
}
/**
* Builds command line options
*/
private function buildOptions(): void
{
$optionsString = '';
$options = $this->getOptions();
if (!empty($options)) {
foreach ($options as $option => $arguments) {
foreach ($arguments as $argument) {
$optionsString .= ' ' . $option;
if ($argument !== null) {
$optionsString .= ' ' . $this->quote($argument);
}
}
}
}
$optionsString = trim($optionsString);
$this->buildTemplatePart(self::TEMPLATE_OPTIONS, $optionsString);
}
/**
* Builds URL
*/
private function buildUrl(): void
{
$this->buildTemplatePart(self::TEMPLATE_URL, $this->getUrl());
}
/**
* Builds command part
* @param string $search
* @param string $replace
*/
private function buildTemplatePart(string $search, string $replace): void
{
if ($replace === '') {
// remove extra space
$this->setCommand((string)preg_replace($this->getTemplatePartPattern($search), $search, $this->getCommand()));
} else {
// add space
$replace = ' ' . $replace;
}
$this->setCommand(trim(str_replace($search, $replace, $this->getCommand())));
}
/**
* Generates regular expression in order to remove extra spaces from the command template if the part is empty
* @param string $search
* @return string
*/
private function getTemplatePartPattern(string $search): string
{
return '/ ?' . preg_quote($search, '/') . ' ?/';
}
/**
* @param string $quoteCharacter
* @return Command
*/
public function setQuoteCharacter(string $quoteCharacter): Command
{
$this->quoteCharacter = $quoteCharacter;
return $this;
}
/**
* @return string
*/
public function getQuoteCharacter(): string
{
return $this->quoteCharacter;
}
/**
* Quotes argument
* @param string $argument
* @return string
*/
private function quote(string $argument): string
{
$quoteCharacter = $this->getQuoteCharacter();
if ($quoteCharacter === '') {
return $argument;
}
if (strpos($argument, $quoteCharacter) !== false) {
if ($quoteCharacter === self::QUOTE_SINGLE) {
return '$' . $quoteCharacter . $this->escape($argument) . $quoteCharacter;
}
return $quoteCharacter . $this->escape($argument) . $quoteCharacter;
}
return $quoteCharacter . $argument . $quoteCharacter;
}
/**
* Escapes quotes in the argument
* @param string $argument
* @return string
*/
private function escape(string $argument): string
{
return str_replace($this->getQuoteCharacter(), '\\' . $this->getQuoteCharacter(), $argument);
}
/**
* Converts option from user-friendly format ot internal format
* @param array<mixed> $options
* @return array<mixed>
*/
private function toInternalFormat(array $options): array
{
$formattedOptions = [];
foreach ($options as $option => $arguments) {
$option = trim((string)$option);
if (strpos($option, '-') !== 0) {
// ['-L', '-v']
$formattedOptions[$arguments] = [null];
} elseif (!is_array($arguments)) {
// ['-L' => null, '-v' => null]
$formattedOptions[$option] = [$arguments];
} else {
// internal format
$formattedOptions[$option] = $arguments;
}
}
return $formattedOptions;
}
/**
* Sets request. If $parse = true gets data from request
* @param RequestInterface|null $request
* @param bool $parse
* @return Command
*/
public function setRequest(?RequestInterface $request, bool $parse = true): Command
{
$this->request = $request;
if ($parse) {
$this->parseRequest();
}
return $this;
}
/**
* @return RequestInterface|null
*/
public function getRequest(): ?RequestInterface
{
return $this->request;
}
/**
* Gets data from request
* @return bool
*/
public function parseRequest(): bool
{
$request = $this->getRequest();
if ($request === null) {
return false;
}
// URL
$this->setUrl((string)$request->getUri());
// headers
foreach (array_keys($request->getHeaders()) as $name) {
$name = (string)$name;
if (strtolower($name) === 'host') {
continue;
}
if (in_array(strtolower($name), self::HEADER_EXCEPTIONS, true)) {
foreach ($request->getHeader($name) as $value) {
$this->addOption('-H', $name . ': ' . $value);
}
} else {
$this->addOption('-H', $name . ': ' . $request->getHeaderLine($name));
}
}
// data
$data = (string)$request->getBody();
if (!empty($data)) {
$this->addOption('-d', (string)$request->getBody());
}
return true;
}
}