RequestDataCollector.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\Cookie;
  12. use Symfony\Component\HttpFoundation\ParameterBag;
  13. use Symfony\Component\HttpFoundation\HeaderBag;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  17. /**
  18. * RequestDataCollector.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class RequestDataCollector extends DataCollector
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function collect(Request $request, Response $response, \Exception $exception = null)
  28. {
  29. $responseHeaders = $response->headers->all();
  30. $cookies = array();
  31. foreach ($response->headers->getCookies() as $cookie) {
  32. $cookies[] = $this->getCookieHeader($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
  33. }
  34. if (count($cookies) > 0) {
  35. $responseHeaders['Set-Cookie'] = $cookies;
  36. }
  37. $attributes = array();
  38. foreach ($request->attributes->all() as $key => $value) {
  39. $attributes[$key] = is_object($value) ? sprintf('Object(%s)', get_class($value)) : $value;
  40. }
  41. $this->data = array(
  42. 'format' => $request->getRequestFormat(),
  43. 'content_type' => $response->headers->get('Content-Type') ? $response->headers->get('Content-Type') : 'text/html',
  44. 'status_code' => $response->getStatusCode(),
  45. 'request_query' => $request->query->all(),
  46. 'request_request' => $request->request->all(),
  47. 'request_headers' => $request->headers->all(),
  48. 'request_server' => $request->server->all(),
  49. 'request_cookies' => $request->cookies->all(),
  50. 'request_attributes' => $attributes,
  51. 'response_headers' => $responseHeaders,
  52. 'session_attributes' => $request->hasSession() ? $request->getSession()->getAttributes() : array(),
  53. );
  54. }
  55. public function getRequestRequest()
  56. {
  57. return new ParameterBag($this->data['request_request']);
  58. }
  59. public function getRequestQuery()
  60. {
  61. return new ParameterBag($this->data['request_query']);
  62. }
  63. public function getRequestHeaders()
  64. {
  65. return new HeaderBag($this->data['request_headers']);
  66. }
  67. public function getRequestServer()
  68. {
  69. return new ParameterBag($this->data['request_server']);
  70. }
  71. public function getRequestCookies()
  72. {
  73. return new ParameterBag($this->data['request_cookies']);
  74. }
  75. public function getRequestAttributes()
  76. {
  77. return new ParameterBag($this->data['request_attributes']);
  78. }
  79. public function getResponseHeaders()
  80. {
  81. return new ResponseHeaderBag($this->data['response_headers']);
  82. }
  83. public function getSessionAttributes()
  84. {
  85. return $this->data['session_attributes'];
  86. }
  87. public function getContentType()
  88. {
  89. return $this->data['content_type'];
  90. }
  91. public function getStatusCode()
  92. {
  93. return $this->data['status_code'];
  94. }
  95. public function getFormat()
  96. {
  97. return $this->data['format'];
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function getName()
  103. {
  104. return 'request';
  105. }
  106. private function getCookieHeader($name, $value, $expires, $path, $domain, $secure, $httponly)
  107. {
  108. $cookie = sprintf('%s=%s', $name, urlencode($value));
  109. if (0 !== $expires) {
  110. if (is_numeric($expires)) {
  111. $expires = (int) $expires;
  112. } elseif ($expires instanceof \DateTime) {
  113. $expires = $expires->getTimestamp();
  114. } else {
  115. $expires = strtotime($expires);
  116. if (false === $expires || -1 == $expires) {
  117. throw new \InvalidArgumentException(sprintf('The "expires" cookie parameter is not valid.', $expires));
  118. }
  119. }
  120. $cookie .= '; expires='.substr(\DateTime::createFromFormat('U', $expires, new \DateTimeZone('UTC'))->format('D, d-M-Y H:i:s T'), 0, -5);
  121. }
  122. if ($domain) {
  123. $cookie .= '; domain='.$domain;
  124. }
  125. $cookie .= '; path='.$path;
  126. if ($secure) {
  127. $cookie .= '; secure';
  128. }
  129. if ($httponly) {
  130. $cookie .= '; httponly';
  131. }
  132. return $cookie;
  133. }
  134. }