1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HMLB\UserBundle\Message\Trace; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Context of a message trace. |
7
|
|
|
* |
8
|
|
|
* Debug information about a trace: was it initiated with CLI or Web request, cli arguments, web URI ... |
9
|
|
|
* |
10
|
|
|
* @author Hugues Maignol <[email protected]> |
11
|
|
|
* |
12
|
|
|
* TODO: separate CLI and HTTP contexts in separate classes. |
13
|
|
|
*/ |
14
|
|
|
class Context |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $phpInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
private $cli; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $arguments; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $requestMethod; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $path; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $host; |
45
|
|
|
|
46
|
|
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
$this->phpInterface = php_sapi_name(); |
49
|
|
|
$this->cli = $this->inCliContext(); |
50
|
|
|
if ($this->cli) { |
51
|
|
|
$this->getCliInfo(); |
52
|
|
|
} else { |
53
|
|
|
$this->getHttpInfo(); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Context in CLI. |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function isCli(): bool |
63
|
|
|
{ |
64
|
|
|
return true === $this->cli; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Getter de phpInterface. |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function getPhpInterface() |
73
|
|
|
{ |
74
|
|
|
return $this->phpInterface; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Arguments. |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function getArguments() |
83
|
|
|
{ |
84
|
|
|
return $this->argumentsStringToArray($this->arguments); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* RequestMethod. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getRequestMethod() |
93
|
|
|
{ |
94
|
|
|
return $this->requestMethod; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Path. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getPath() |
103
|
|
|
{ |
104
|
|
|
return $this->path; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Host. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getHost() |
113
|
|
|
{ |
114
|
|
|
return $this->host; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function inCliContext(): bool |
118
|
|
|
{ |
119
|
|
|
return 'cli' === $this->phpInterface || defined('STDIN'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function getCliInfo() |
123
|
|
|
{ |
124
|
|
|
$this->arguments = $this->argumentsToString($_SERVER['argv']); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function argumentsToString(array $args): string |
128
|
|
|
{ |
129
|
|
|
$tokens = array_map( |
130
|
|
|
function ($token) { |
131
|
|
|
if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) { |
132
|
|
|
return $match[1].$this->escapeToken($match[2]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($token && $token[0] !== '-') { |
136
|
|
|
return $this->escapeToken($token); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $token; |
140
|
|
|
}, |
141
|
|
|
$args |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
return implode(' ', $tokens); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
private function escapeToken(string $token): string |
148
|
|
|
{ |
149
|
|
|
return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private function argumentsStringToArray(string $argsString): array |
153
|
|
|
{ |
154
|
|
|
return explode(' ', $argsString); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private function getHttpInfo() |
158
|
|
|
{ |
159
|
|
|
$this->requestMethod = $_SERVER['REQUEST_METHOD']; |
160
|
|
|
$this->host = $_SERVER['HTTP_HOST']; |
161
|
|
|
$this->path = $_SERVER['PATH_INFO']; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|