RequestPanel.php 4.44 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\debug\Panel;
Qiang Xue committed
13
use yii\helpers\Html;
Qiang Xue committed
14 15

/**
16 17
 * Debugger panel that collects and displays request data.
 *
Qiang Xue committed
18 19 20 21 22 23 24 25 26 27 28 29
 * @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
30 31 32
		$memory = sprintf('%.1f MB', $this->data['memory'] / 1048576);
		$time = number_format($this->data['time'] * 1000) . ' ms';
		$url = $this->getUrl();
Qiang Xue committed
33 34 35

		return <<<EOD
<div class="yii-debug-toolbar-block">
Qiang Xue committed
36
	<a href="$url">Memory: <span class="label">$memory</span></a>
Qiang Xue committed
37 38 39
</div>

<div class="yii-debug-toolbar-block">
Qiang Xue committed
40
	<a href="$url">Time: <span class="label">$time</span></a>
Qiang Xue committed
41
</div>
42 43

<div class="yii-debug-toolbar-block">
Qiang Xue committed
44
	<a href="$url">Action: <span class="label">{$this->data['action']}</span></a>
45
</div>
Qiang Xue committed
46 47 48 49 50
EOD;
	}

	public function getDetail()
	{
Qiang Xue committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
		$data = array(
			'Route' => $this->data['route'],
			'Action' => $this->data['action'],
			'Parameters' => $this->data['actionParams'],
		);
		return "<h1>Request Information</h1>\n"
			. $this->renderData('Routing', $data) . "\n"
			. $this->renderData('Flashes', $this->data['flashes']) . "\n"
			. $this->renderData('$_GET', $this->data['GET']) . "\n"
			. $this->renderData('$_POST', $this->data['POST']) . "\n"
			. $this->renderData('$_COOKIE', $this->data['COOKIE']) . "\n"
			. $this->renderData('$_FILES', $this->data['FILES']) . "\n"
			. $this->renderData('$_SESSION', $this->data['SESSION']) . "\n"
			. $this->renderData('$_SERVER', $this->data['SERVER']) . "\n"
			. $this->renderData('Request Headers', $this->data['requestHeaders']) . "\n"
			. $this->renderData('Response Headers', $this->data['responseHeaders']);
Qiang Xue committed
67 68 69 70
	}

	public function save()
	{
Qiang Xue committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
		if (function_exists('apache_request_headers')) {
			$requestHeaders = apache_request_headers();
		} elseif (function_exists('http_get_request_headers')) {
			$requestHeaders = http_get_request_headers();
		} else {
			$requestHeaders = array();
		}
		$responseHeaders = array();
		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])) {
						$responseHeaders[$name] = array($responseHeaders[$name], $value);
					} 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);
Qiang Xue committed
107 108 109
		return array(
			'memory' => memory_get_peak_usage(),
			'time' => microtime(true) - YII_BEGIN_TIME,
Qiang Xue committed
110 111 112
			'flashes' => $session ? $session->getAllFlashes() : array(),
			'requestHeaders' => $requestHeaders,
			'responseHeaders' => $responseHeaders,
Qiang Xue committed
113
			'route' => Yii::$app->requestedAction ? Yii::$app->requestedAction->getUniqueId() : Yii::$app->requestedRoute,
Qiang Xue committed
114 115 116 117 118 119
			'action' => $action,
			'actionParams' => Yii::$app->requestedParams,
			'SERVER' => empty($_SERVER) ? array() : $_SERVER,
			'GET' => empty($_GET) ? array() : $_GET,
			'POST' => empty($_POST) ? array() : $_POST,
			'COOKIE' => empty($_COOKIE) ? array() : $_COOKIE,
Qiang Xue committed
120 121 122 123
			'FILES' => empty($_FILES) ? array() : $_FILES,
			'SESSION' => empty($_SESSION) ? array() : $_SESSION,
		);
	}
Qiang Xue committed
124

Qiang Xue committed
125
	protected function renderData($caption, $values)
Qiang Xue committed
126
	{
Qiang Xue committed
127 128 129
		if (empty($values)) {
			return "<h3>$caption</h3>\n<p>Empty.</p>";
		}
Qiang Xue committed
130 131
		$rows = array();
		foreach ($values as $name => $value) {
Qiang Xue committed
132
			$rows[] = '<tr><th style="width: 200px;">' . Html::encode($name) . '</th><td><div style="overflow:auto">' . Html::encode(var_export($value, true)) . '</div></td></tr>';
Qiang Xue committed
133
		}
Qiang Xue committed
134 135 136
		$rows = implode("\n", $rows);
		return <<<EOD
<h3>$caption</h3>
Qiang Xue committed
137 138 139 140 141 142 143
<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
144
	}
Qiang Xue committed
145
}