1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hiapi\Core\Console; |
4
|
|
|
|
5
|
|
|
use Laminas\Diactoros\ServerRequest; |
6
|
|
|
use Laminas\Diactoros\Uri; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class for marshaling a request object sent with console. |
10
|
|
|
*/ |
11
|
|
|
class ServerRequestFactory |
12
|
|
|
{ |
13
|
|
|
public const PROTOCOL_VERSION = '1.1'; |
14
|
|
|
|
15
|
|
|
public const ATTRIBUTE_NAME = 'from-console'; |
16
|
|
|
|
17
|
|
|
private $command; |
18
|
|
|
|
19
|
|
|
private $ops = [ |
20
|
|
|
self::ATTRIBUTE_NAME => true, |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
private $query = []; |
24
|
|
|
|
25
|
|
|
private $post = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create a request from superglobal values: argv. |
29
|
|
|
* @return ServerRequest |
30
|
|
|
*/ |
31
|
|
|
public static function fromGlobals(): ServerRequest |
32
|
|
|
{ |
33
|
|
|
return (new static())->createFromGlobals(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function createFromGlobals(): ServerRequest |
37
|
|
|
{ |
38
|
|
|
$this->parseGlobals(); |
39
|
|
|
|
40
|
|
|
return $this->addAttributes(new ServerRequest( |
41
|
|
|
$_SERVER, |
42
|
|
|
$_FILES, |
43
|
|
|
$this->prepareUri($this->command), |
44
|
|
|
'POST', |
45
|
|
|
'php://input', |
46
|
|
|
$this->prepareHeaders($this->ops), |
47
|
|
|
$this->prepareCookies(), |
48
|
|
|
$this->query, |
49
|
|
|
$this->post, |
50
|
|
|
static::PROTOCOL_VERSION |
51
|
|
|
), $this->ops); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function parseGlobals() |
55
|
|
|
{ |
56
|
|
|
$argv = $GLOBALS['argv']; |
57
|
|
|
$this->program = array_shift($argv); |
|
|
|
|
58
|
|
|
while ($argv[0][0]=='-') { |
59
|
|
|
$vs = explode('=', substr(array_shift($argv), 1), 2); |
60
|
|
|
$this->ops[$vs[0]] = $vs[1] ?? true; |
61
|
|
|
} |
62
|
|
|
$this->command = array_shift($argv); |
63
|
|
|
|
64
|
|
|
foreach ($argv as $n => $arg) { |
65
|
|
|
if ($arg[0] == '-') { |
66
|
|
|
$vs = explode('=', substr($arg, 1), 2); |
67
|
|
|
$this->query[$vs[0]] = is_null($vs[1]) ? true : $vs[1]; |
68
|
|
|
} else $this->query[$n] = $arg; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!empty($this->ops['stdin'])) { |
72
|
|
|
$this->post = json_decode(file_get_contents('php://stdin'), true); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function addAttributes(ServerRequest $request, array $ops): ServerRequest |
77
|
|
|
{ |
78
|
|
|
foreach ($ops as $key => $value) { |
79
|
|
|
$request = $request->withAttribute($key, $value); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $request; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function prepareUri(string $command): Uri |
86
|
|
|
{ |
87
|
|
|
$uri = new Uri(''); |
88
|
|
|
|
89
|
|
|
return $uri->withPath("/$command"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function prepareHeaders(array $ops): array |
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
|
|
'Accept' => $this->detectContentType($ops), |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function detectContentType(array $ops): string |
100
|
|
|
{ |
101
|
|
|
if (!empty($ops['text'])) { |
102
|
|
|
return 'text/plain'; |
103
|
|
|
} |
104
|
|
|
if (!empty($ops['show']) || !empty($ops['dump'])) { |
105
|
|
|
return 'text/php'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return 'application/json'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function prepareCookies(): array |
112
|
|
|
{ |
113
|
|
|
return $_COOKIE; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: