1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Symfony package. |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Symfony\Component\HttpKernel\DataCollector; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
18
|
|
|
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* RequestDataCollector. |
23
|
|
|
* |
24
|
|
|
* @author Fabien Potencier <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class RequestDataCollector extends DataCollector implements EventSubscriberInterface |
27
|
|
|
{ |
28
|
|
|
protected $controllers; |
29
|
|
|
|
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
$this->controllers = new \SplObjectStorage(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
39
|
|
|
{ |
40
|
|
|
$responseHeaders = $response->headers->all(); |
41
|
|
|
foreach ($response->headers->getCookies() as $cookie) { |
42
|
|
|
$responseHeaders['set-cookie'][] = (string) $cookie; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// attributes are serialized and as they can be anything, they need to be converted to strings. |
46
|
|
|
$attributes = array(); |
47
|
|
|
foreach ($request->attributes->all() as $key => $value) { |
48
|
|
|
if ('_route' === $key && is_object($value)) { |
49
|
|
|
$attributes[$key] = $this->varToString($value->getPath()); |
50
|
|
|
} elseif ('_route_params' === $key) { |
51
|
|
|
// we need to keep route params as an array (see getRouteParams()) |
52
|
|
|
foreach ($value as $k => $v) { |
53
|
|
|
$value[$k] = $this->varToString($v); |
54
|
|
|
} |
55
|
|
|
$attributes[$key] = $value; |
56
|
|
|
} else { |
57
|
|
|
$attributes[$key] = $this->varToString($value); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$content = null; |
|
|
|
|
62
|
|
|
try { |
63
|
|
|
$content = $request->getContent(); |
64
|
|
|
} catch (\LogicException $e) { |
65
|
|
|
// the user already got the request content as a resource |
66
|
|
|
$content = false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$sessionMetadata = array(); |
70
|
|
|
$sessionAttributes = array(); |
71
|
|
|
$flashes = array(); |
72
|
|
|
if ($request->hasSession()) { |
73
|
|
|
$session = $request->getSession(); |
74
|
|
|
if ($session->isStarted()) { |
75
|
|
|
$sessionMetadata['Created'] = date(DATE_RFC822, $session->getMetadataBag()->getCreated()); |
76
|
|
|
$sessionMetadata['Last used'] = date(DATE_RFC822, $session->getMetadataBag()->getLastUsed()); |
77
|
|
|
$sessionMetadata['Lifetime'] = $session->getMetadataBag()->getLifetime(); |
78
|
|
|
$sessionAttributes = $session->all(); |
79
|
|
|
$flashes = $session->getFlashBag()->peekAll(); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$statusCode = $response->getStatusCode(); |
84
|
|
|
|
85
|
|
|
$this->data = array( |
86
|
|
|
'format' => $request->getRequestFormat(), |
87
|
|
|
'content' => $content, |
88
|
|
|
'content_type' => $response->headers->get('Content-Type', 'text/html'), |
89
|
|
|
'status_text' => isset(Response::$statusTexts[$statusCode]) ? Response::$statusTexts[$statusCode] : '', |
90
|
|
|
'status_code' => $statusCode, |
91
|
|
|
'request_query' => $request->query->all(), |
92
|
|
|
'request_request' => $request->request->all(), |
93
|
|
|
'request_headers' => $request->headers->all(), |
94
|
|
|
'request_server' => $request->server->all(), |
95
|
|
|
'request_cookies' => $request->cookies->all(), |
96
|
|
|
'request_attributes' => $attributes, |
97
|
|
|
'response_headers' => $responseHeaders, |
98
|
|
|
'session_metadata' => $sessionMetadata, |
99
|
|
|
'session_attributes' => $sessionAttributes, |
100
|
|
|
'flashes' => $flashes, |
101
|
|
|
'path_info' => $request->getPathInfo(), |
102
|
|
|
'controller' => 'n/a', |
103
|
|
|
'locale' => $request->getLocale(), |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
if (isset($this->data['request_headers']['php-auth-pw'])) { |
107
|
|
|
$this->data['request_headers']['php-auth-pw'] = '******'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (isset($this->data['request_server']['PHP_AUTH_PW'])) { |
111
|
|
|
$this->data['request_server']['PHP_AUTH_PW'] = '******'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (isset($this->data['request_request']['_password'])) { |
115
|
|
|
$this->data['request_request']['_password'] = '******'; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
foreach ($this->data as $key => $value) { |
119
|
|
|
if (!is_array($value)) { |
120
|
|
|
continue; |
121
|
|
|
} |
122
|
|
|
if ('request_headers' === $key || 'response_headers' === $key) { |
123
|
|
|
$value = array_map(function ($v) { return isset($v[0]) && !isset($v[1]) ? $v[0] : $v; }, $value); |
124
|
|
|
} |
125
|
|
|
if ('request_server' !== $key && 'request_cookies' !== $key) { |
126
|
|
|
$this->data[$key] = $value; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (isset($this->controllers[$request])) { |
131
|
|
|
$controller = $this->controllers[$request]; |
132
|
|
|
if (is_array($controller)) { |
133
|
|
|
try { |
134
|
|
|
$r = new \ReflectionMethod($controller[0], $controller[1]); |
135
|
|
|
$this->data['controller'] = array( |
136
|
|
|
'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0], |
137
|
|
|
'method' => $controller[1], |
138
|
|
|
'file' => $r->getFileName(), |
139
|
|
|
'line' => $r->getStartLine(), |
140
|
|
|
); |
141
|
|
|
} catch (\ReflectionException $e) { |
142
|
|
|
if (is_callable($controller)) { |
143
|
|
|
// using __call or __callStatic |
144
|
|
|
$this->data['controller'] = array( |
145
|
|
|
'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0], |
146
|
|
|
'method' => $controller[1], |
147
|
|
|
'file' => 'n/a', |
148
|
|
|
'line' => 'n/a', |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} elseif ($controller instanceof \Closure) { |
153
|
|
|
$r = new \ReflectionFunction($controller); |
154
|
|
|
$this->data['controller'] = array( |
155
|
|
|
'class' => $r->getName(), |
156
|
|
|
'method' => null, |
157
|
|
|
'file' => $r->getFileName(), |
158
|
|
|
'line' => $r->getStartLine(), |
159
|
|
|
); |
160
|
|
|
} elseif (is_object($controller)) { |
161
|
|
|
$r = new \ReflectionClass($controller); |
162
|
|
|
$this->data['controller'] = array( |
163
|
|
|
'class' => $r->getName(), |
164
|
|
|
'method' => null, |
165
|
|
|
'file' => $r->getFileName(), |
166
|
|
|
'line' => $r->getStartLine(), |
167
|
|
|
); |
168
|
|
|
} else { |
169
|
|
|
$this->data['controller'] = (string) $controller ?: 'n/a'; |
170
|
|
|
} |
171
|
|
|
unset($this->controllers[$request]); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function getPathInfo() |
176
|
|
|
{ |
177
|
|
|
return $this->data['path_info']; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function getRequestRequest() |
181
|
|
|
{ |
182
|
|
|
return new ParameterBag($this->data['request_request']); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function getRequestQuery() |
186
|
|
|
{ |
187
|
|
|
return new ParameterBag($this->data['request_query']); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function getRequestHeaders() |
191
|
|
|
{ |
192
|
|
|
return new ParameterBag($this->data['request_headers']); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function getRequestServer() |
196
|
|
|
{ |
197
|
|
|
return new ParameterBag($this->data['request_server']); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function getRequestCookies() |
201
|
|
|
{ |
202
|
|
|
return new ParameterBag($this->data['request_cookies']); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getRequestAttributes() |
206
|
|
|
{ |
207
|
|
|
return new ParameterBag($this->data['request_attributes']); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function getResponseHeaders() |
211
|
|
|
{ |
212
|
|
|
return new ParameterBag($this->data['response_headers']); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function getSessionMetadata() |
216
|
|
|
{ |
217
|
|
|
return $this->data['session_metadata']; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function getSessionAttributes() |
221
|
|
|
{ |
222
|
|
|
return $this->data['session_attributes']; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function getFlashes() |
226
|
|
|
{ |
227
|
|
|
return $this->data['flashes']; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function getContent() |
231
|
|
|
{ |
232
|
|
|
return $this->data['content']; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function getContentType() |
236
|
|
|
{ |
237
|
|
|
return $this->data['content_type']; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function getStatusText() |
241
|
|
|
{ |
242
|
|
|
return $this->data['status_text']; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function getStatusCode() |
246
|
|
|
{ |
247
|
|
|
return $this->data['status_code']; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function getFormat() |
251
|
|
|
{ |
252
|
|
|
return $this->data['format']; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function getLocale() |
256
|
|
|
{ |
257
|
|
|
return $this->data['locale']; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Gets the route name. |
262
|
|
|
* |
263
|
|
|
* The _route request attributes is automatically set by the Router Matcher. |
264
|
|
|
* |
265
|
|
|
* @return string The route |
266
|
|
|
*/ |
267
|
|
|
public function getRoute() |
268
|
|
|
{ |
269
|
|
|
return isset($this->data['request_attributes']['_route']) ? $this->data['request_attributes']['_route'] : ''; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Gets the route parameters. |
274
|
|
|
* |
275
|
|
|
* The _route_params request attributes is automatically set by the RouterListener. |
276
|
|
|
* |
277
|
|
|
* @return array The parameters |
278
|
|
|
*/ |
279
|
|
|
public function getRouteParams() |
280
|
|
|
{ |
281
|
|
|
return isset($this->data['request_attributes']['_route_params']) ? $this->data['request_attributes']['_route_params'] : array(); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Gets the controller. |
286
|
|
|
* |
287
|
|
|
* @return string The controller as a string |
288
|
|
|
*/ |
289
|
|
|
public function getController() |
290
|
|
|
{ |
291
|
|
|
return $this->data['controller']; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
public function onKernelController(FilterControllerEvent $event) |
295
|
|
|
{ |
296
|
|
|
$this->controllers[$event->getRequest()] = $event->getController(); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public static function getSubscribedEvents() |
300
|
|
|
{ |
301
|
|
|
return array(KernelEvents::CONTROLLER => 'onKernelController'); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* {@inheritdoc} |
306
|
|
|
*/ |
307
|
|
|
public function getName() |
308
|
|
|
{ |
309
|
|
|
return 'request'; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|