RequestPanel.php 4.86 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
use yii\bootstrap\Tabs;
Qiang Xue committed
13
use yii\debug\Panel;
Qiang Xue committed
14
use yii\helpers\Html;
15
use yii\web\Response;
Qiang Xue committed
16 17

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

	public function getSummary()
	{
Qiang Xue committed
32
		$url = $this->getUrl();
33 34 35 36 37 38 39 40 41 42 43 44
		$statusCode = $this->data['statusCode'];
		if ($statusCode === null) {
			$statusCode = 200;
		}
		if ($statusCode >= 200 && $statusCode < 300) {
			$class = 'label-success';
		} elseif ($statusCode >= 100 && $statusCode < 200) {
			$class = 'label-info';
		} else {
			$class = 'label-important';
		}
		$statusText = Html::encode(isset(Response::$httpStatuses[$statusCode]) ? Response::$httpStatuses[$statusCode] : '');
Qiang Xue committed
45 46

		return <<<EOD
47
<div class="yii-debug-toolbar-block">
48
	<a href="$url" title="Status code: $statusCode $statusText">Status <span class="label $class">$statusCode</span></a>
49
</div>
50
<div class="yii-debug-toolbar-block">
51
	<a href="$url">Action <span class="label">{$this->data['action']}</span></a>
52
</div>
Qiang Xue committed
53 54 55 56 57
EOD;
	}

	public function getDetail()
	{
Alexander Makarov committed
58
		$data = [
Qiang Xue committed
59 60 61
			'Route' => $this->data['route'],
			'Action' => $this->data['action'],
			'Parameters' => $this->data['actionParams'],
Alexander Makarov committed
62 63 64 65
		];
		return Tabs::widget([
			'items' => [
				[
Qiang Xue committed
66 67 68 69
					'label' => 'Parameters',
					'content' => $this->renderData('Routing', $data)
						. $this->renderData('$_GET', $this->data['GET'])
						. $this->renderData('$_POST', $this->data['POST'])
Roman Zhuravlev committed
70
						. $this->renderData('$_FILES', $this->data['FILES'])
Qiang Xue committed
71 72
						. $this->renderData('$_COOKIE', $this->data['COOKIE']),
					'active' => true,
Alexander Makarov committed
73 74
				],
				[
Qiang Xue committed
75 76 77
					'label' => 'Headers',
					'content' => $this->renderData('Request Headers', $this->data['requestHeaders'])
						. $this->renderData('Response Headers', $this->data['responseHeaders']),
Alexander Makarov committed
78 79
				],
				[
Qiang Xue committed
80 81 82
					'label' => 'Session',
					'content' => $this->renderData('$_SESSION', $this->data['SESSION'])
						. $this->renderData('Flashes', $this->data['flashes']),
Alexander Makarov committed
83 84
				],
				[
Qiang Xue committed
85 86
					'label' => '$_SERVER',
					'content' => $this->renderData('$_SERVER', $this->data['SERVER']),
Alexander Makarov committed
87 88 89
				],
			],
		]);
Qiang Xue committed
90 91 92 93
	}

	public function save()
	{
Qiang Xue committed
94 95 96 97 98
		if (function_exists('apache_request_headers')) {
			$requestHeaders = apache_request_headers();
		} elseif (function_exists('http_get_request_headers')) {
			$requestHeaders = http_get_request_headers();
		} else {
Alexander Makarov committed
99
			$requestHeaders = [];
Qiang Xue committed
100
		}
Alexander Makarov committed
101
		$responseHeaders = [];
Qiang Xue committed
102 103 104 105 106 107
		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
108
						$responseHeaders[$name] = [$responseHeaders[$name], $value];
Qiang Xue committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
					} 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
130 131
		return [
			'flashes' => $session ? $session->getAllFlashes() : [],
132
			'statusCode' => Yii::$app->getResponse()->getStatusCode(),
Qiang Xue committed
133 134
			'requestHeaders' => $requestHeaders,
			'responseHeaders' => $responseHeaders,
Qiang Xue committed
135
			'route' => Yii::$app->requestedAction ? Yii::$app->requestedAction->getUniqueId() : Yii::$app->requestedRoute,
Qiang Xue committed
136 137
			'action' => $action,
			'actionParams' => Yii::$app->requestedParams,
Alexander Makarov committed
138 139 140 141 142 143 144
			'SERVER' => empty($_SERVER) ? [] : $_SERVER,
			'GET' => empty($_GET) ? [] : $_GET,
			'POST' => empty($_POST) ? [] : $_POST,
			'COOKIE' => empty($_COOKIE) ? [] : $_COOKIE,
			'FILES' => empty($_FILES) ? [] : $_FILES,
			'SESSION' => empty($_SESSION) ? [] : $_SESSION,
		];
Qiang Xue committed
145
	}
Qiang Xue committed
146

Qiang Xue committed
147
	protected function renderData($caption, $values)
Qiang Xue committed
148
	{
Qiang Xue committed
149 150 151
		if (empty($values)) {
			return "<h3>$caption</h3>\n<p>Empty.</p>";
		}
Alexander Makarov committed
152
		$rows = [];
Qiang Xue committed
153
		foreach ($values as $name => $value) {
154
			$rows[] = '<tr><th style="width: 200px;">' . Html::encode($name) . '</th><td>' . htmlspecialchars(var_export($value, true), ENT_QUOTES|ENT_SUBSTITUTE, \Yii::$app->charset, TRUE) . '</td></tr>';
Qiang Xue committed
155
		}
Qiang Xue committed
156 157 158
		$rows = implode("\n", $rows);
		return <<<EOD
<h3>$caption</h3>
Qiang Xue committed
159 160 161 162 163 164 165
<table class="table table-condensed table-bordered table-striped table-hover" style="table-layout: fixed;">
<thead><tr><th style="width: 200px;">Name</th><th>Value</th></tr></thead>
<tbody>
$rows
</tbody>
</table>
EOD;
Qiang Xue committed
166
	}
Qiang Xue committed
167
}