You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.1KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\debug\panels;
  8. use Yii;
  9. use yii\base\InlineAction;
  10. use yii\debug\Panel;
  11. /**
  12. * Debugger panel that collects and displays request data.
  13. *
  14. * @author Qiang Xue <qiang.xue@gmail.com>
  15. * @since 2.0
  16. */
  17. class RequestPanel extends Panel
  18. {
  19. /**
  20. * @inheritdoc
  21. */
  22. public function getName()
  23. {
  24. return 'Request';
  25. }
  26. /**
  27. * @inheritdoc
  28. */
  29. public function getSummary()
  30. {
  31. return Yii::$app->view->render('panels/request/summary', ['panel' => $this]);
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function getDetail()
  37. {
  38. return Yii::$app->view->render('panels/request/detail', ['panel' => $this]);
  39. }
  40. /**
  41. * @inheritdoc
  42. */
  43. public function save()
  44. {
  45. $headers = Yii::$app->getRequest()->getHeaders();
  46. $requestHeaders = [];
  47. foreach ($headers as $name => $value) {
  48. if (is_array($value) && count($value) == 1) {
  49. $requestHeaders[$name] = current($value);
  50. } else {
  51. $requestHeaders[$name] = $value;
  52. }
  53. }
  54. $responseHeaders = [];
  55. foreach (headers_list() as $header) {
  56. if (($pos = strpos($header, ':')) !== false) {
  57. $name = substr($header, 0, $pos);
  58. $value = trim(substr($header, $pos + 1));
  59. if (isset($responseHeaders[$name])) {
  60. if (!is_array($responseHeaders[$name])) {
  61. $responseHeaders[$name] = [$responseHeaders[$name], $value];
  62. } else {
  63. $responseHeaders[$name][] = $value;
  64. }
  65. } else {
  66. $responseHeaders[$name] = $value;
  67. }
  68. } else {
  69. $responseHeaders[] = $header;
  70. }
  71. }
  72. if (Yii::$app->requestedAction) {
  73. if (Yii::$app->requestedAction instanceof InlineAction) {
  74. $action = get_class(Yii::$app->requestedAction->controller) . '::' . Yii::$app->requestedAction->actionMethod . '()';
  75. } else {
  76. $action = get_class(Yii::$app->requestedAction) . '::run()';
  77. }
  78. } else {
  79. $action = null;
  80. }
  81. return [
  82. 'flashes' => $this->getFlashes(),
  83. 'statusCode' => Yii::$app->getResponse()->getStatusCode(),
  84. 'requestHeaders' => $requestHeaders,
  85. 'responseHeaders' => $responseHeaders,
  86. 'route' => Yii::$app->requestedAction ? Yii::$app->requestedAction->getUniqueId() : Yii::$app->requestedRoute,
  87. 'action' => $action,
  88. 'actionParams' => Yii::$app->requestedParams,
  89. 'requestBody' => Yii::$app->getRequest()->getRawBody() == '' ? [] : [
  90. 'Content Type' => Yii::$app->getRequest()->getContentType(),
  91. 'Raw' => Yii::$app->getRequest()->getRawBody(),
  92. 'Decoded to Params' => Yii::$app->getRequest()->getBodyParams(),
  93. ],
  94. 'SERVER' => empty($_SERVER) ? [] : $_SERVER,
  95. 'GET' => empty($_GET) ? [] : $_GET,
  96. 'POST' => empty($_POST) ? [] : $_POST,
  97. 'COOKIE' => empty($_COOKIE) ? [] : $_COOKIE,
  98. 'FILES' => empty($_FILES) ? [] : $_FILES,
  99. 'SESSION' => empty($_SESSION) ? [] : $_SESSION,
  100. ];
  101. }
  102. /**
  103. * Getting flash messages without deleting them or touching deletion counters
  104. *
  105. * @return array flash messages (key => message).
  106. */
  107. protected function getFlashes()
  108. {
  109. /* @var $session \yii\web\Session */
  110. $session = Yii::$app->has('session', true) ? Yii::$app->get('session') : null;
  111. if ($session === null || !$session->getIsActive()) {
  112. return [];
  113. }
  114. $counters = $session->get($session->flashParam, []);
  115. $flashes = [];
  116. foreach (array_keys($counters) as $key) {
  117. if (array_key_exists($key, $_SESSION)) {
  118. $flashes[$key] = $_SESSION[$key];
  119. }
  120. }
  121. return $flashes;
  122. }
  123. }