RequestPanel.php 2.93 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\debug\panels;

Qiang Xue committed
10 11
use Yii;
use yii\base\InlineAction;
Qiang Xue committed
12 13 14
use yii\debug\Panel;

/**
15 16
 * Debugger panel that collects and displays request data.
 *
Qiang Xue committed
17 18 19 20 21
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class RequestPanel extends Panel
{
22 23 24
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
25 26 27 28 29
	public function getName()
	{
		return 'Request';
	}

30 31 32
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
33 34
	public function getSummary()
	{
Qiang Xue committed
35
		return Yii::$app->view->render('panels/request/summary', ['panel' => $this]);
Qiang Xue committed
36 37
	}

38 39 40
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
41 42
	public function getDetail()
	{
Qiang Xue committed
43
		return Yii::$app->view->render('panels/request/detail', ['panel' => $this]);
Qiang Xue committed
44 45
	}

46 47 48
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
49 50
	public function save()
	{
51
		$headers = Yii::$app->getRequest()->getHeaders();
52
		$requestHeaders = [];
53 54 55 56 57 58
		foreach ($headers as $name => $value) {
			if (is_array($value) && count($value) == 1) {
				$requestHeaders[$name] = current($value);
			} else {
				$requestHeaders[$name] = $value;
			}
Qiang Xue committed
59
		}
60

Alexander Makarov committed
61
		$responseHeaders = [];
Qiang Xue committed
62 63 64 65 66 67
		foreach (headers_list() as $header) {
			if (($pos = strpos($header, ':')) !== false) {
				$name = substr($header, 0, $pos);
				$value = trim(substr($header, $pos + 1));
				if (isset($responseHeaders[$name])) {
					if (!is_array($responseHeaders[$name])) {
Alexander Makarov committed
68
						$responseHeaders[$name] = [$responseHeaders[$name], $value];
Qiang Xue committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
					} else {
						$responseHeaders[$name][] = $value;
					}
				} else {
					$responseHeaders[$name] = $value;
				}
			} else {
				$responseHeaders[] = $header;
			}
		}
		if (Yii::$app->requestedAction) {
			if (Yii::$app->requestedAction instanceof InlineAction) {
				$action = get_class(Yii::$app->requestedAction->controller) . '::' . Yii::$app->requestedAction->actionMethod . '()';
			} else {
				$action = get_class(Yii::$app->requestedAction) . '::run()';
			}
		} else {
			$action = null;
		}
		/** @var \yii\web\Session $session */
		$session = Yii::$app->getComponent('session', false);
Alexander Makarov committed
90 91
		return [
			'flashes' => $session ? $session->getAllFlashes() : [],
92
			'statusCode' => Yii::$app->getResponse()->getStatusCode(),
Qiang Xue committed
93 94
			'requestHeaders' => $requestHeaders,
			'responseHeaders' => $responseHeaders,
Qiang Xue committed
95
			'route' => Yii::$app->requestedAction ? Yii::$app->requestedAction->getUniqueId() : Yii::$app->requestedRoute,
Qiang Xue committed
96 97
			'action' => $action,
			'actionParams' => Yii::$app->requestedParams,
98 99 100 101 102
			'requestBody' => Yii::$app->getRequest()->getRawBody() == '' ? [] : [
				'Content Type' => Yii::$app->getRequest()->getContentType(),
				'Raw' => Yii::$app->getRequest()->getRawBody(),
				'Decoded to Params' => Yii::$app->getRequest()->getBodyParams(),
			],
Alexander Makarov committed
103
			'SERVER' => empty($_SERVER) ? [] : $_SERVER,
104 105 106
			'GET' => empty($_GET) ? [] : $_GET,
			'POST' => empty($_POST) ? [] : $_POST,
			'COOKIE' => empty($_COOKIE) ? [] : $_COOKIE,
Alexander Makarov committed
107
			'FILES' => empty($_FILES) ? [] : $_FILES,
108
			'SESSION' => empty($_SESSION) ? [] : $_SESSION,
Alexander Makarov committed
109
		];
Qiang Xue committed
110 111
	}
}