1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Guzzle\GuzzleHttp; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\MessageFormatter as BaseMessageFormatter; |
6
|
|
|
use Psr\Http\Message\MessageInterface; |
7
|
|
|
use Psr\Http\Message\RequestInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Sludio\HelperBundle\Script\Utils\Helper; |
10
|
|
|
use GuzzleHttp\Psr7; |
11
|
|
|
|
12
|
|
|
class MessageFormatter extends BaseMessageFormatter |
13
|
|
|
{ |
14
|
|
|
public $template; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var RequestInterface |
18
|
|
|
*/ |
19
|
|
|
protected $request; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ResponseInterface |
23
|
|
|
*/ |
24
|
|
|
protected $response; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Exception |
28
|
|
|
*/ |
29
|
|
|
protected $error; |
30
|
|
|
|
31
|
|
|
protected function sludioRequest() |
32
|
|
|
{ |
33
|
|
|
return Psr7\str($this->request); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function sludioResponse() |
37
|
|
|
{ |
38
|
|
|
return $this->response ? Psr7\str($this->response) : ''; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function sludioReqHeaders() |
42
|
|
|
{ |
43
|
|
|
return trim($this->request->getMethod().' '.(string)$this->request).' HTTP/'.$this->request->getProtocolVersion()."\r\n".$this->headers($this->request); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function sludioResHeaders() |
47
|
|
|
{ |
48
|
|
|
return $this->response ? sprintf('HTTP/%s %d %s', $this->response->getProtocolVersion(), $this->response->getStatusCode(), $this->response->getReasonPhrase())."\r\n".$this->headers($this->response) : 'NULL'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function sludioReqBody() |
52
|
|
|
{ |
53
|
|
|
return $this->request->getBody(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function sludioResBody() |
57
|
|
|
{ |
58
|
|
|
return $this->response ? $this->response->getBody() : 'NULL'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function sludioTs() |
62
|
|
|
{ |
63
|
|
|
return gmdate('c'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function sludioDateIso8601() |
67
|
|
|
{ |
68
|
|
|
return $this->sludioTs(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function sludioDateCommonLog() |
72
|
|
|
{ |
73
|
|
|
return date('d/M/Y:H:i:s O'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function sludioMethod() |
77
|
|
|
{ |
78
|
|
|
return $this->request->getMethod(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function sludioVersion() |
82
|
|
|
{ |
83
|
|
|
return $this->request->getProtocolVersion(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function sludioUri() |
87
|
|
|
{ |
88
|
|
|
return $this->request->getUri(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function sludioUrl() |
92
|
|
|
{ |
93
|
|
|
return $this->sludioUri(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function sludioTarget() |
97
|
|
|
{ |
98
|
|
|
return $this->request->getRequestTarget(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function sludioReqVersion() |
102
|
|
|
{ |
103
|
|
|
return $this->request->getProtocolVersion(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function sludioResVersion() |
107
|
|
|
{ |
108
|
|
|
return $this->response ? $this->response->getProtocolVersion() : 'NULL'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function sludioHost() |
112
|
|
|
{ |
113
|
|
|
return $this->request->getHeaderLine('Host'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function sludioHostname() |
117
|
|
|
{ |
118
|
|
|
return gethostname(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function sludioCode() |
122
|
|
|
{ |
123
|
|
|
return $this->response ? $this->response->getStatusCode() : 'NULL'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function sludioPhrase() |
127
|
|
|
{ |
128
|
|
|
return $this->response ? $this->response->getReasonPhrase() : 'NULL'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function sludioError() |
132
|
|
|
{ |
133
|
|
|
if ($this->error) { |
134
|
|
|
if ($this->error instanceof \Exception) { |
135
|
|
|
return $this->error->getMessage(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return (string)$this->error; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return 'NULL'; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function format(RequestInterface $request, ResponseInterface $response = null, \Exception $error = null) |
148
|
|
|
{ |
149
|
|
|
$cache = []; |
150
|
|
|
|
151
|
|
|
return preg_replace_callback('/{\s*([A-Za-z_\-\.0-9]+)\s*}/', function(array $matches) use ($request, $response, $error, &$cache) { |
152
|
|
|
if (isset($cache[$matches[1]])) { |
153
|
|
|
return $cache[$matches[1]]; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$cache[$matches[1]] = ''; |
157
|
|
|
|
158
|
|
|
$method = Helper::toCamelCase('sludio_'.$matches[1]); |
159
|
|
|
if (method_exists($this, $method)) { |
160
|
|
|
$this->request = $request; |
161
|
|
|
$this->response = $response; |
162
|
|
|
$this->error = $error; |
163
|
|
|
|
164
|
|
|
$cache[$matches[1]] = $this->{$method}(); |
165
|
|
|
} else { |
166
|
|
|
if (strpos($matches[1], 'req_header_') === 0) { |
167
|
|
|
$cache[$matches[1]] = $request->getHeaderLine(substr($matches[1], 11)); |
168
|
|
|
} elseif (strpos($matches[1], 'res_header_') === 0) { |
169
|
|
|
$cache[$matches[1]] = $response ? $response->getHeaderLine(substr($matches[1], 11)) : 'NULL'; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $cache[$matches[1]]; |
174
|
|
|
}, $this->template); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
private function headers(MessageInterface $message) |
178
|
|
|
{ |
179
|
|
|
$result = ''; |
180
|
|
|
foreach ($message->getHeaders() as $name => $values) { |
181
|
|
|
$result .= $name.': '.implode(', ', $values)."\r\n"; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return trim($result); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|