RequestDataCollector.php 12 KB

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