RequestDataCollector.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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' => $request->getContent(),
  44. 'content_type' => $response->headers->get('Content-Type') ? $response->headers->get('Content-Type') : 'text/html',
  45. 'status_code' => $response->getStatusCode(),
  46. 'request_query' => $request->query->all(),
  47. 'request_request' => $request->request->all(),
  48. 'request_headers' => $request->headers->all(),
  49. 'request_server' => $request->server->all(),
  50. 'request_cookies' => $request->cookies->all(),
  51. 'request_attributes' => $attributes,
  52. 'response_headers' => $responseHeaders,
  53. 'session_attributes' => $request->hasSession() ? $request->getSession()->getAttributes() : array(),
  54. );
  55. }
  56. public function getRequestRequest()
  57. {
  58. return new ParameterBag($this->data['request_request']);
  59. }
  60. public function getRequestQuery()
  61. {
  62. return new ParameterBag($this->data['request_query']);
  63. }
  64. public function getRequestHeaders()
  65. {
  66. return new HeaderBag($this->data['request_headers']);
  67. }
  68. public function getRequestServer()
  69. {
  70. return new ParameterBag($this->data['request_server']);
  71. }
  72. public function getRequestCookies()
  73. {
  74. return new ParameterBag($this->data['request_cookies']);
  75. }
  76. public function getRequestAttributes()
  77. {
  78. return new ParameterBag($this->data['request_attributes']);
  79. }
  80. public function getResponseHeaders()
  81. {
  82. return new ResponseHeaderBag($this->data['response_headers']);
  83. }
  84. public function getSessionAttributes()
  85. {
  86. return $this->data['session_attributes'];
  87. }
  88. public function getContent()
  89. {
  90. return $this->data['content'];
  91. }
  92. public function getContentType()
  93. {
  94. return $this->data['content_type'];
  95. }
  96. public function getStatusCode()
  97. {
  98. return $this->data['status_code'];
  99. }
  100. public function getFormat()
  101. {
  102. return $this->data['format'];
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function getName()
  108. {
  109. return 'request';
  110. }
  111. private function getCookieHeader($name, $value, $expires, $path, $domain, $secure, $httponly)
  112. {
  113. $cookie = sprintf('%s=%s', $name, urlencode($value));
  114. if (0 !== $expires) {
  115. if (is_numeric($expires)) {
  116. $expires = (int) $expires;
  117. } elseif ($expires instanceof \DateTime) {
  118. $expires = $expires->getTimestamp();
  119. } else {
  120. $expires = strtotime($expires);
  121. if (false === $expires || -1 == $expires) {
  122. throw new \InvalidArgumentException(sprintf('The "expires" cookie parameter is not valid.', $expires));
  123. }
  124. }
  125. $cookie .= '; expires='.substr(\DateTime::createFromFormat('U', $expires, new \DateTimeZone('UTC'))->format('D, d-M-Y H:i:s T'), 0, -5);
  126. }
  127. if ($domain) {
  128. $cookie .= '; domain='.$domain;
  129. }
  130. $cookie .= '; path='.$path;
  131. if ($secure) {
  132. $cookie .= '; secure';
  133. }
  134. if ($httponly) {
  135. $cookie .= '; httponly';
  136. }
  137. return $cookie;
  138. }
  139. }