RequestPanel.php 4.9 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()
	{
Qiang Xue committed
58 59 60 61 62
		$data = array(
			'Route' => $this->data['route'],
			'Action' => $this->data['action'],
			'Parameters' => $this->data['actionParams'],
		);
Qiang Xue committed
63 64 65 66 67 68 69
		return Tabs::widget(array(
			'items' => array(
				array(
					'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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
						. $this->renderData('$_COOKIE', $this->data['COOKIE']),
					'active' => true,
				),
				array(
					'label' => 'Headers',
					'content' => $this->renderData('Request Headers', $this->data['requestHeaders'])
						. $this->renderData('Response Headers', $this->data['responseHeaders']),
				),
				array(
					'label' => 'Session',
					'content' => $this->renderData('$_SESSION', $this->data['SESSION'])
						. $this->renderData('Flashes', $this->data['flashes']),
				),
				array(
					'label' => '$_SERVER',
					'content' => $this->renderData('$_SERVER', $this->data['SERVER']),
				),
			),
		));
Qiang Xue committed
90 91 92 93
	}

	public function save()
	{
Qiang Xue committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
		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
130
		return array(
Qiang Xue committed
131
			'flashes' => $session ? $session->getAllFlashes() : array(),
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 138 139 140 141
			'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
142 143 144 145
			'FILES' => empty($_FILES) ? array() : $_FILES,
			'SESSION' => empty($_SESSION) ? array() : $_SESSION,
		);
	}
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>";
		}
Qiang Xue committed
152 153
		$rows = array();
		foreach ($values as $name => $value) {
Qiang Xue committed
154
			$rows[] = '<tr><th style="width: 200px;">' . Html::encode($name) . '</th><td>' . Html::encode(var_export($value, 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
}