Mercurial > hg > cmmr2012-drupal-site
comparison vendor/symfony/http-kernel/DataCollector/RequestDataCollector.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of the Symfony package. | |
5 * | |
6 * (c) Fabien Potencier <fabien@symfony.com> | |
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\Cookie; | |
15 use Symfony\Component\HttpFoundation\ParameterBag; | |
16 use Symfony\Component\HttpFoundation\Request; | |
17 use Symfony\Component\HttpFoundation\Response; | |
18 use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | |
19 use Symfony\Component\HttpKernel\KernelEvents; | |
20 use Symfony\Component\HttpKernel\Event\FilterControllerEvent; | |
21 use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
22 | |
23 /** | |
24 * @author Fabien Potencier <fabien@symfony.com> | |
25 */ | |
26 class RequestDataCollector extends DataCollector implements EventSubscriberInterface, LateDataCollectorInterface | |
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 // attributes are serialized and as they can be anything, they need to be converted to strings. | |
41 $attributes = array(); | |
42 $route = ''; | |
43 foreach ($request->attributes->all() as $key => $value) { | |
44 if ('_route' === $key) { | |
45 $route = is_object($value) ? $value->getPath() : $value; | |
46 $attributes[$key] = $route; | |
47 } else { | |
48 $attributes[$key] = $value; | |
49 } | |
50 } | |
51 | |
52 $content = null; | |
53 try { | |
54 $content = $request->getContent(); | |
55 } catch (\LogicException $e) { | |
56 // the user already got the request content as a resource | |
57 $content = false; | |
58 } | |
59 | |
60 $sessionMetadata = array(); | |
61 $sessionAttributes = array(); | |
62 $session = null; | |
63 $flashes = array(); | |
64 if ($request->hasSession()) { | |
65 $session = $request->getSession(); | |
66 if ($session->isStarted()) { | |
67 $sessionMetadata['Created'] = date(DATE_RFC822, $session->getMetadataBag()->getCreated()); | |
68 $sessionMetadata['Last used'] = date(DATE_RFC822, $session->getMetadataBag()->getLastUsed()); | |
69 $sessionMetadata['Lifetime'] = $session->getMetadataBag()->getLifetime(); | |
70 $sessionAttributes = $session->all(); | |
71 $flashes = $session->getFlashBag()->peekAll(); | |
72 } | |
73 } | |
74 | |
75 $statusCode = $response->getStatusCode(); | |
76 | |
77 $responseCookies = array(); | |
78 foreach ($response->headers->getCookies() as $cookie) { | |
79 $responseCookies[$cookie->getName()] = $cookie; | |
80 } | |
81 | |
82 $this->data = array( | |
83 'method' => $request->getMethod(), | |
84 'format' => $request->getRequestFormat(), | |
85 'content' => $content, | |
86 'content_type' => $response->headers->get('Content-Type', 'text/html'), | |
87 'status_text' => isset(Response::$statusTexts[$statusCode]) ? Response::$statusTexts[$statusCode] : '', | |
88 'status_code' => $statusCode, | |
89 'request_query' => $request->query->all(), | |
90 'request_request' => $request->request->all(), | |
91 'request_headers' => $request->headers->all(), | |
92 'request_server' => $request->server->all(), | |
93 'request_cookies' => $request->cookies->all(), | |
94 'request_attributes' => $attributes, | |
95 'route' => $route, | |
96 'response_headers' => $response->headers->all(), | |
97 'response_cookies' => $responseCookies, | |
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 $this->data[$key] = array_map(function ($v) { return isset($v[0]) && !isset($v[1]) ? $v[0] : $v; }, $value); | |
124 } | |
125 } | |
126 | |
127 if (isset($this->controllers[$request])) { | |
128 $this->data['controller'] = $this->parseController($this->controllers[$request]); | |
129 unset($this->controllers[$request]); | |
130 } | |
131 | |
132 if ($request->attributes->has('_redirected') && $redirectCookie = $request->cookies->get('sf_redirect')) { | |
133 $this->data['redirect'] = json_decode($redirectCookie, true); | |
134 | |
135 $response->headers->clearCookie('sf_redirect'); | |
136 } | |
137 | |
138 if ($response->isRedirect()) { | |
139 $response->headers->setCookie(new Cookie( | |
140 'sf_redirect', | |
141 json_encode(array( | |
142 'token' => $response->headers->get('x-debug-token'), | |
143 'route' => $request->attributes->get('_route', 'n/a'), | |
144 'method' => $request->getMethod(), | |
145 'controller' => $this->parseController($request->attributes->get('_controller')), | |
146 'status_code' => $statusCode, | |
147 'status_text' => Response::$statusTexts[(int) $statusCode], | |
148 )) | |
149 )); | |
150 } | |
151 | |
152 $this->data['identifier'] = $this->data['route'] ?: (is_array($this->data['controller']) ? $this->data['controller']['class'].'::'.$this->data['controller']['method'].'()' : $this->data['controller']); | |
153 } | |
154 | |
155 public function lateCollect() | |
156 { | |
157 $this->data = $this->cloneVar($this->data); | |
158 } | |
159 | |
160 public function reset() | |
161 { | |
162 $this->data = array(); | |
163 $this->controllers = new \SplObjectStorage(); | |
164 } | |
165 | |
166 public function getMethod() | |
167 { | |
168 return $this->data['method']; | |
169 } | |
170 | |
171 public function getPathInfo() | |
172 { | |
173 return $this->data['path_info']; | |
174 } | |
175 | |
176 public function getRequestRequest() | |
177 { | |
178 return new ParameterBag($this->data['request_request']->getValue()); | |
179 } | |
180 | |
181 public function getRequestQuery() | |
182 { | |
183 return new ParameterBag($this->data['request_query']->getValue()); | |
184 } | |
185 | |
186 public function getRequestHeaders() | |
187 { | |
188 return new ParameterBag($this->data['request_headers']->getValue()); | |
189 } | |
190 | |
191 public function getRequestServer($raw = false) | |
192 { | |
193 return new ParameterBag($this->data['request_server']->getValue($raw)); | |
194 } | |
195 | |
196 public function getRequestCookies($raw = false) | |
197 { | |
198 return new ParameterBag($this->data['request_cookies']->getValue($raw)); | |
199 } | |
200 | |
201 public function getRequestAttributes() | |
202 { | |
203 return new ParameterBag($this->data['request_attributes']->getValue()); | |
204 } | |
205 | |
206 public function getResponseHeaders() | |
207 { | |
208 return new ParameterBag($this->data['response_headers']->getValue()); | |
209 } | |
210 | |
211 public function getResponseCookies() | |
212 { | |
213 return new ParameterBag($this->data['response_cookies']->getValue()); | |
214 } | |
215 | |
216 public function getSessionMetadata() | |
217 { | |
218 return $this->data['session_metadata']->getValue(); | |
219 } | |
220 | |
221 public function getSessionAttributes() | |
222 { | |
223 return $this->data['session_attributes']->getValue(); | |
224 } | |
225 | |
226 public function getFlashes() | |
227 { | |
228 return $this->data['flashes']->getValue(); | |
229 } | |
230 | |
231 public function getContent() | |
232 { | |
233 return $this->data['content']; | |
234 } | |
235 | |
236 public function getContentType() | |
237 { | |
238 return $this->data['content_type']; | |
239 } | |
240 | |
241 public function getStatusText() | |
242 { | |
243 return $this->data['status_text']; | |
244 } | |
245 | |
246 public function getStatusCode() | |
247 { | |
248 return $this->data['status_code']; | |
249 } | |
250 | |
251 public function getFormat() | |
252 { | |
253 return $this->data['format']; | |
254 } | |
255 | |
256 public function getLocale() | |
257 { | |
258 return $this->data['locale']; | |
259 } | |
260 | |
261 /** | |
262 * Gets the route name. | |
263 * | |
264 * The _route request attributes is automatically set by the Router Matcher. | |
265 * | |
266 * @return string The route | |
267 */ | |
268 public function getRoute() | |
269 { | |
270 return $this->data['route']; | |
271 } | |
272 | |
273 public function getIdentifier() | |
274 { | |
275 return $this->data['identifier']; | |
276 } | |
277 | |
278 /** | |
279 * Gets the route parameters. | |
280 * | |
281 * The _route_params request attributes is automatically set by the RouterListener. | |
282 * | |
283 * @return array The parameters | |
284 */ | |
285 public function getRouteParams() | |
286 { | |
287 return isset($this->data['request_attributes']['_route_params']) ? $this->data['request_attributes']['_route_params']->getValue() : array(); | |
288 } | |
289 | |
290 /** | |
291 * Gets the parsed controller. | |
292 * | |
293 * @return array|string The controller as a string or array of data | |
294 * with keys 'class', 'method', 'file' and 'line' | |
295 */ | |
296 public function getController() | |
297 { | |
298 return $this->data['controller']; | |
299 } | |
300 | |
301 /** | |
302 * Gets the previous request attributes. | |
303 * | |
304 * @return array|bool A legacy array of data from the previous redirection response | |
305 * or false otherwise | |
306 */ | |
307 public function getRedirect() | |
308 { | |
309 return isset($this->data['redirect']) ? $this->data['redirect'] : false; | |
310 } | |
311 | |
312 public function onKernelController(FilterControllerEvent $event) | |
313 { | |
314 $this->controllers[$event->getRequest()] = $event->getController(); | |
315 } | |
316 | |
317 public function onKernelResponse(FilterResponseEvent $event) | |
318 { | |
319 if (!$event->isMasterRequest()) { | |
320 return; | |
321 } | |
322 | |
323 if ($event->getRequest()->cookies->has('sf_redirect')) { | |
324 $event->getRequest()->attributes->set('_redirected', true); | |
325 } | |
326 } | |
327 | |
328 public static function getSubscribedEvents() | |
329 { | |
330 return array( | |
331 KernelEvents::CONTROLLER => 'onKernelController', | |
332 KernelEvents::RESPONSE => 'onKernelResponse', | |
333 ); | |
334 } | |
335 | |
336 /** | |
337 * {@inheritdoc} | |
338 */ | |
339 public function getName() | |
340 { | |
341 return 'request'; | |
342 } | |
343 | |
344 /** | |
345 * Parse a controller. | |
346 * | |
347 * @param mixed $controller The controller to parse | |
348 * | |
349 * @return array|string An array of controller data or a simple string | |
350 */ | |
351 protected function parseController($controller) | |
352 { | |
353 if (is_string($controller) && false !== strpos($controller, '::')) { | |
354 $controller = explode('::', $controller); | |
355 } | |
356 | |
357 if (is_array($controller)) { | |
358 try { | |
359 $r = new \ReflectionMethod($controller[0], $controller[1]); | |
360 | |
361 return array( | |
362 'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0], | |
363 'method' => $controller[1], | |
364 'file' => $r->getFileName(), | |
365 'line' => $r->getStartLine(), | |
366 ); | |
367 } catch (\ReflectionException $e) { | |
368 if (is_callable($controller)) { | |
369 // using __call or __callStatic | |
370 return array( | |
371 'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0], | |
372 'method' => $controller[1], | |
373 'file' => 'n/a', | |
374 'line' => 'n/a', | |
375 ); | |
376 } | |
377 } | |
378 } | |
379 | |
380 if ($controller instanceof \Closure) { | |
381 $r = new \ReflectionFunction($controller); | |
382 | |
383 return array( | |
384 'class' => $r->getName(), | |
385 'method' => null, | |
386 'file' => $r->getFileName(), | |
387 'line' => $r->getStartLine(), | |
388 ); | |
389 } | |
390 | |
391 if (is_object($controller)) { | |
392 $r = new \ReflectionClass($controller); | |
393 | |
394 return array( | |
395 'class' => $r->getName(), | |
396 'method' => null, | |
397 'file' => $r->getFileName(), | |
398 'line' => $r->getStartLine(), | |
399 ); | |
400 } | |
401 | |
402 return is_string($controller) ? $controller : 'n/a'; | |
403 } | |
404 } |