/src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php

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