RequestDataCollector.php 9.7 KB

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