PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/http-kernel/DataCollector/RequestDataCollector.php

https://gitlab.com/judielsm/Handora
PHP | 342 lines | 254 code | 46 blank | 42 comment | 22 complexity | e054af5cb8a2cd9a5bc741e500f8d04c MD5 | raw file
  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\HeaderBag;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. /**
  20. * RequestDataCollector.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. */
  24. class RequestDataCollector extends DataCollector implements EventSubscriberInterface
  25. {
  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. $responseHeaders = $response->headers->all();
  37. $cookies = array();
  38. foreach ($response->headers->getCookies() as $cookie) {
  39. $cookies[] = $this->getCookieHeader($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
  40. }
  41. if (count($cookies) > 0) {
  42. $responseHeaders['Set-Cookie'] = $cookies;
  43. }
  44. // attributes are serialized and as they can be anything, they need to be converted to strings.
  45. $attributes = array();
  46. foreach ($request->attributes->all() as $key => $value) {
  47. if ('_route' === $key && is_object($value)) {
  48. $attributes[$key] = $this->varToString($value->getPath());
  49. } elseif ('_route_params' === $key) {
  50. // we need to keep route params as an array (see getRouteParams())
  51. foreach ($value as $k => $v) {
  52. $value[$k] = $this->varToString($v);
  53. }
  54. $attributes[$key] = $value;
  55. } else {
  56. $attributes[$key] = $this->varToString($value);
  57. }
  58. }
  59. $content = null;
  60. try {
  61. $content = $request->getContent();
  62. } catch (\LogicException $e) {
  63. // the user already got the request content as a resource
  64. $content = false;
  65. }
  66. $sessionMetadata = array();
  67. $sessionAttributes = array();
  68. $flashes = array();
  69. if ($request->hasSession()) {
  70. $session = $request->getSession();
  71. if ($session->isStarted()) {
  72. $sessionMetadata['Created'] = date(DATE_RFC822, $session->getMetadataBag()->getCreated());
  73. $sessionMetadata['Last used'] = date(DATE_RFC822, $session->getMetadataBag()->getLastUsed());
  74. $sessionMetadata['Lifetime'] = $session->getMetadataBag()->getLifetime();
  75. $sessionAttributes = $session->all();
  76. $flashes = $session->getFlashBag()->peekAll();
  77. }
  78. }
  79. $statusCode = $response->getStatusCode();
  80. $this->data = array(
  81. 'format' => $request->getRequestFormat(),
  82. 'content' => $content,
  83. 'content_type' => $response->headers->get('Content-Type', 'text/html'),
  84. 'status_text' => isset(Response::$statusTexts[$statusCode]) ? Response::$statusTexts[$statusCode] : '',
  85. 'status_code' => $statusCode,
  86. 'request_query' => $request->query->all(),
  87. 'request_request' => $request->request->all(),
  88. 'request_headers' => $request->headers->all(),
  89. 'request_server' => $request->server->all(),
  90. 'request_cookies' => $request->cookies->all(),
  91. 'request_attributes' => $attributes,
  92. 'response_headers' => $responseHeaders,
  93. 'session_metadata' => $sessionMetadata,
  94. 'session_attributes' => $sessionAttributes,
  95. 'flashes' => $flashes,
  96. 'path_info' => $request->getPathInfo(),
  97. 'controller' => 'n/a',
  98. 'locale' => $request->getLocale(),
  99. );
  100. if (isset($this->data['request_headers']['php-auth-pw'])) {
  101. $this->data['request_headers']['php-auth-pw'] = '******';
  102. }
  103. if (isset($this->data['request_server']['PHP_AUTH_PW'])) {
  104. $this->data['request_server']['PHP_AUTH_PW'] = '******';
  105. }
  106. if (isset($this->data['request_request']['_password'])) {
  107. $this->data['request_request']['_password'] = '******';
  108. }
  109. if (isset($this->controllers[$request])) {
  110. $controller = $this->controllers[$request];
  111. if (is_array($controller)) {
  112. try {
  113. $r = new \ReflectionMethod($controller[0], $controller[1]);
  114. $this->data['controller'] = array(
  115. 'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0],
  116. 'method' => $controller[1],
  117. 'file' => $r->getFileName(),
  118. 'line' => $r->getStartLine(),
  119. );
  120. } catch (\ReflectionException $e) {
  121. if (is_callable($controller)) {
  122. // using __call or __callStatic
  123. $this->data['controller'] = array(
  124. 'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0],
  125. 'method' => $controller[1],
  126. 'file' => 'n/a',
  127. 'line' => 'n/a',
  128. );
  129. }
  130. }
  131. } elseif ($controller instanceof \Closure) {
  132. $r = new \ReflectionFunction($controller);
  133. $this->data['controller'] = array(
  134. 'class' => $r->getName(),
  135. 'method' => null,
  136. 'file' => $r->getFileName(),
  137. 'line' => $r->getStartLine(),
  138. );
  139. } elseif (is_object($controller)) {
  140. $r = new \ReflectionClass($controller);
  141. $this->data['controller'] = array(
  142. 'class' => $r->getName(),
  143. 'method' => null,
  144. 'file' => $r->getFileName(),
  145. 'line' => $r->getStartLine(),
  146. );
  147. } else {
  148. $this->data['controller'] = (string) $controller ?: 'n/a';
  149. }
  150. unset($this->controllers[$request]);
  151. }
  152. }
  153. public function getPathInfo()
  154. {
  155. return $this->data['path_info'];
  156. }
  157. public function getRequestRequest()
  158. {
  159. return new ParameterBag($this->data['request_request']);
  160. }
  161. public function getRequestQuery()
  162. {
  163. return new ParameterBag($this->data['request_query']);
  164. }
  165. public function getRequestHeaders()
  166. {
  167. return new HeaderBag($this->data['request_headers']);
  168. }
  169. public function getRequestServer()
  170. {
  171. return new ParameterBag($this->data['request_server']);
  172. }
  173. public function getRequestCookies()
  174. {
  175. return new ParameterBag($this->data['request_cookies']);
  176. }
  177. public function getRequestAttributes()
  178. {
  179. return new ParameterBag($this->data['request_attributes']);
  180. }
  181. public function getResponseHeaders()
  182. {
  183. return new ResponseHeaderBag($this->data['response_headers']);
  184. }
  185. public function getSessionMetadata()
  186. {
  187. return $this->data['session_metadata'];
  188. }
  189. public function getSessionAttributes()
  190. {
  191. return $this->data['session_attributes'];
  192. }
  193. public function getFlashes()
  194. {
  195. return $this->data['flashes'];
  196. }
  197. public function getContent()
  198. {
  199. return $this->data['content'];
  200. }
  201. public function getContentType()
  202. {
  203. return $this->data['content_type'];
  204. }
  205. public function getStatusText()
  206. {
  207. return $this->data['status_text'];
  208. }
  209. public function getStatusCode()
  210. {
  211. return $this->data['status_code'];
  212. }
  213. public function getFormat()
  214. {
  215. return $this->data['format'];
  216. }
  217. public function getLocale()
  218. {
  219. return $this->data['locale'];
  220. }
  221. /**
  222. * Gets the route name.
  223. *
  224. * The _route request attributes is automatically set by the Router Matcher.
  225. *
  226. * @return string The route
  227. */
  228. public function getRoute()
  229. {
  230. return isset($this->data['request_attributes']['_route']) ? $this->data['request_attributes']['_route'] : '';
  231. }
  232. /**
  233. * Gets the route parameters.
  234. *
  235. * The _route_params request attributes is automatically set by the RouterListener.
  236. *
  237. * @return array The parameters
  238. */
  239. public function getRouteParams()
  240. {
  241. return isset($this->data['request_attributes']['_route_params']) ? $this->data['request_attributes']['_route_params'] : array();
  242. }
  243. /**
  244. * Gets the controller.
  245. *
  246. * @return string The controller as a string
  247. */
  248. public function getController()
  249. {
  250. return $this->data['controller'];
  251. }
  252. public function onKernelController(FilterControllerEvent $event)
  253. {
  254. $this->controllers[$event->getRequest()] = $event->getController();
  255. }
  256. public static function getSubscribedEvents()
  257. {
  258. return array(KernelEvents::CONTROLLER => 'onKernelController');
  259. }
  260. /**
  261. * {@inheritdoc}
  262. */
  263. public function getName()
  264. {
  265. return 'request';
  266. }
  267. private function getCookieHeader($name, $value, $expires, $path, $domain, $secure, $httponly)
  268. {
  269. $cookie = sprintf('%s=%s', $name, urlencode($value));
  270. if (0 !== $expires) {
  271. if (is_numeric($expires)) {
  272. $expires = (int) $expires;
  273. } elseif ($expires instanceof \DateTime) {
  274. $expires = $expires->getTimestamp();
  275. } else {
  276. $tmp = strtotime($expires);
  277. if (false === $tmp || -1 == $tmp) {
  278. throw new \InvalidArgumentException(sprintf('The "expires" cookie parameter is not valid (%s).', $expires));
  279. }
  280. $expires = $tmp;
  281. }
  282. $cookie .= '; expires='.str_replace('+0000', '', \DateTime::createFromFormat('U', $expires, new \DateTimeZone('GMT'))->format('D, d-M-Y H:i:s T'));
  283. }
  284. if ($domain) {
  285. $cookie .= '; domain='.$domain;
  286. }
  287. $cookie .= '; path='.$path;
  288. if ($secure) {
  289. $cookie .= '; secure';
  290. }
  291. if ($httponly) {
  292. $cookie .= '; httponly';
  293. }
  294. return $cookie;
  295. }
  296. }