RequestPanel.php 4.08 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 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class RequestPanel extends Panel
{
	public function getName()
	{
		return 'Request';
	}

	public function getSummary()
	{
		$memory = sprintf('%.2fMB', $this->data['memory'] / 1048576);
		$time = sprintf('%.3fs', $this->data['time']);

		return <<<EOD
<div class="yii-debug-toolbar-block">
Peak memory: $memory
</div>

<div class="yii-debug-toolbar-block">
	Time spent: $time
</div>
EOD;
	}

	public function getDetail()
	{
Qiang Xue committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
		$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
60 61 62 63
	}

	public function save()
	{
Qiang Xue committed
64 65 66 67 68 69 70 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
		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
100 101 102
		return array(
			'memory' => memory_get_peak_usage(),
			'time' => microtime(true) - YII_BEGIN_TIME,
Qiang Xue committed
103 104 105 106 107 108 109 110 111 112
			'flashes' => $session ? $session->getAllFlashes() : array(),
			'requestHeaders' => $requestHeaders,
			'responseHeaders' => $responseHeaders,
			'route' => Yii::$app->requestedAction->getUniqueId(),
			'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
113 114 115 116
			'FILES' => empty($_FILES) ? array() : $_FILES,
			'SESSION' => empty($_SESSION) ? array() : $_SESSION,
		);
	}
Qiang Xue committed
117

Qiang Xue committed
118
	protected function renderData($caption, $values)
Qiang Xue committed
119
	{
Qiang Xue committed
120 121 122
		if (empty($values)) {
			return "<h3>$caption</h3>\n<p>Empty.</p>";
		}
Qiang Xue committed
123 124
		$rows = array();
		foreach ($values as $name => $value) {
Qiang Xue committed
125
			$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
126
		}
Qiang Xue committed
127 128 129
		$rows = implode("\n", $rows);
		return <<<EOD
<h3>$caption</h3>
Qiang Xue committed
130 131 132 133 134 135 136
<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
137
	}
Qiang Xue committed
138
}