|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace WShafer\SwooleExpressive\Bridge; |
|
5
|
|
|
|
|
6
|
|
|
use Swoole\Http\Request as SwooleRequest; |
|
7
|
|
|
use Zend\Diactoros\ServerRequest; |
|
8
|
|
|
|
|
9
|
|
|
class Psr7RequestBuilder |
|
10
|
|
|
{ |
|
11
|
3 |
|
public function build(SwooleRequest $swooleRequest) |
|
12
|
|
|
{ |
|
13
|
3 |
|
$body = (string) $swooleRequest->rawcontent(); |
|
14
|
|
|
|
|
15
|
3 |
|
if (empty($body)) { |
|
16
|
3 |
|
$body = 'php://input'; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
3 |
|
return new ServerRequest( |
|
20
|
3 |
|
$this->buildServerParams($swooleRequest), |
|
21
|
3 |
|
$swooleRequest->files ?? [], |
|
22
|
3 |
|
$swooleRequest->server['request_uri'] ?? null, |
|
23
|
3 |
|
$swooleRequest->server['request_method'] ?? null, |
|
24
|
3 |
|
$body, |
|
25
|
3 |
|
$swooleRequest->header ?? [], |
|
26
|
3 |
|
$swooleRequest->cookie ?? [], |
|
27
|
3 |
|
$swooleRequest->get ?? [], |
|
28
|
3 |
|
$swooleRequest->post ?? null, |
|
29
|
3 |
|
$swooleRequest->server['server_protocol'] ?? '1.1' |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function buildServerParams(SwooleRequest $swooleRequest) |
|
34
|
|
|
{ |
|
35
|
|
|
$server = $swooleRequest->server ?? []; |
|
36
|
|
|
$header = $swooleRequest->header ?? []; |
|
37
|
|
|
|
|
38
|
|
|
$return['USER'] = get_current_user(); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
if (function_exists('posix_getpwuid')) { |
|
41
|
|
|
$return['USER'] = posix_getpwuid(posix_geteuid())['name']; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$return['HTTP_CACHE_CONTROL'] = $header['cache-control'] ?? ''; |
|
45
|
|
|
$return['HTTP_UPGRADE_INSECURE_REQUESTS'] = $header['upgrade-insecure-requests-control'] ?? ''; |
|
46
|
|
|
$return['HTTP_CONNECTION'] = $header['connection'] ?? ''; |
|
47
|
|
|
$return['HTTP_DNT'] = $header['dnt'] ?? ''; |
|
48
|
|
|
$return['HTTP_ACCEPT_ENCODING'] = $header['accept-encoding'] ?? ''; |
|
49
|
|
|
$return['HTTP_ACCEPT_LANGUAGE'] = $header['accept-accept-language'] ?? ''; |
|
50
|
|
|
$return['HTTP_ACCEPT'] = $header['accept'] ?? ''; |
|
51
|
|
|
$return['HTTP_USER_AGENT'] = $header['user-agent'] ?? ''; |
|
52
|
|
|
$return['HTTP_HOST'] = $header['user-host'] ?? ''; |
|
53
|
|
|
$return['SERVER_NAME'] = '_'; |
|
54
|
|
|
$return['SERVER_PORT'] = $server['server_port'] ?? null; |
|
55
|
|
|
$return['SERVER_ADDR'] = $server['server_addr'] ?? ''; |
|
56
|
|
|
$return['REMOTE_PORT'] = $server['remote_port'] ?? null; |
|
57
|
|
|
$return['REMOTE_ADDR'] = $server['remote_addr'] ?? ''; |
|
58
|
|
|
$return['SERVER_SOFTWARE'] = $server['server_software'] ?? ''; |
|
59
|
|
|
$return['GATEWAY_INTERFACE'] = $server['server_software'] ?? ''; |
|
60
|
|
|
$return['REQUEST_SCHEME'] = 'http'; |
|
61
|
|
|
$return['SERVER_PROTOCOL'] = $server['server_protocol'] ?? null; |
|
62
|
|
|
$return['DOCUMENT_ROOT'] = realpath(__DIR__.'/../../bin'); |
|
63
|
|
|
$return['DOCUMENT_URI'] = '/'; |
|
64
|
|
|
$return['REQUEST_URI'] = $server['request_uri'] ?? ''; |
|
65
|
|
|
$return['SCRIPT_NAME'] = '/swoole-expressive'; |
|
66
|
|
|
$return['CONTENT_LENGTH'] = $header['content-length'] ?? null; |
|
67
|
|
|
$return['CONTENT_TYPE'] = $header['content-type'] ?? null; |
|
68
|
|
|
$return['REQUEST_METHOD'] = $server['request_method'] ?? 'GET'; |
|
69
|
|
|
$return['QUERY_STRING'] = $server['query_string'] ?? ''; |
|
70
|
|
|
$return['SCRIPT_FILENAME'] = rtrim($return['DOCUMENT_ROOT'],'/').'/'.ltrim($return['SCRIPT_NAME']); |
|
71
|
|
|
$return['PATH_INFO'] = $server['path_info'] ?? ''; |
|
72
|
|
|
$return['FCGI_ROLE'] = 'RESPONDER'; |
|
73
|
|
|
$return['PHP_SELF'] = $return['PATH_INFO']; |
|
74
|
|
|
$return['REQUEST_TIME_FLOAT'] = $server['request_time_float'] ?? ''; |
|
75
|
|
|
$return['REQUEST_TIME'] = $server['request_time'] ?? ''; |
|
76
|
|
|
|
|
77
|
|
|
return $return; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.