DefaultController.php 2.31 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\controllers;

Qiang Xue committed
10
use Yii;
Qiang Xue committed
11
use yii\web\Controller;
Qiang Xue committed
12
use yii\web\HttpException;
Qiang Xue committed
13 14 15 16 17 18 19

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class DefaultController extends Controller
{
Qiang Xue committed
20
	public $layout = 'main';
Qiang Xue committed
21 22 23 24 25 26 27 28
	/**
	 * @var  \yii\debug\Module
	 */
	public $module;
	/**
	 * @var array the summary data (e.g. URL, time)
	 */
	public $summary;
Qiang Xue committed
29

Qiang Xue committed
30 31
	public function actionIndex()
	{
Alexander Makarov committed
32
		return $this->render('index', ['manifest' => $this->getManifest()]);
Qiang Xue committed
33 34 35
	}

	public function actionView($tag = null, $panel = null)
Qiang Xue committed
36
	{
Qiang Xue committed
37 38
		if ($tag === null) {
			$tags = array_keys($this->getManifest());
Qiang Xue committed
39
			$tag = reset($tags);
Qiang Xue committed
40
		}
Qiang Xue committed
41
		$this->loadData($tag);
Qiang Xue committed
42 43 44
		if (isset($this->module->panels[$panel])) {
			$activePanel = $this->module->panels[$panel];
		} else {
Qiang Xue committed
45
			$activePanel = $this->module->panels['request'];
Qiang Xue committed
46
		}
Alexander Makarov committed
47
		return $this->render('view', [
Qiang Xue committed
48
			'tag' => $tag,
Qiang Xue committed
49
			'summary' => $this->summary,
Qiang Xue committed
50
			'manifest' => $this->getManifest(),
Qiang Xue committed
51 52
			'panels' => $this->module->panels,
			'activePanel' => $activePanel,
Alexander Makarov committed
53
		]);
Qiang Xue committed
54
	}
Qiang Xue committed
55 56

	public function actionToolbar($tag)
Qiang Xue committed
57 58
	{
		$this->loadData($tag);
Alexander Makarov committed
59
		return $this->renderPartial('toolbar', [
Qiang Xue committed
60
			'tag' => $tag,
Qiang Xue committed
61
			'panels' => $this->module->panels,
Alexander Makarov committed
62
		]);
Qiang Xue committed
63 64
	}

Qiang Xue committed
65 66 67 68 69
	public function actionPhpinfo()
	{
		phpinfo();
	}

Qiang Xue committed
70 71 72 73 74
	private $_manifest;

	protected function getManifest()
	{
		if ($this->_manifest === null) {
Qiang Xue committed
75
			$indexFile = $this->module->dataPath . '/index.data';
Qiang Xue committed
76
			if (is_file($indexFile)) {
77
				$this->_manifest = array_reverse(unserialize(file_get_contents($indexFile)), true);
Qiang Xue committed
78
			} else {
Alexander Makarov committed
79
				$this->_manifest = [];
Qiang Xue committed
80 81 82 83 84
			}
		}
		return $this->_manifest;
	}

Qiang Xue committed
85
	protected function loadData($tag)
Qiang Xue committed
86
	{
Qiang Xue committed
87 88
		$manifest = $this->getManifest();
		if (isset($manifest[$tag])) {
Qiang Xue committed
89
			$dataFile = $this->module->dataPath . "/$tag.data";
90
			$data = unserialize(file_get_contents($dataFile));
Qiang Xue committed
91
			foreach ($this->module->panels as $id => $panel) {
Qiang Xue committed
92
				if (isset($data[$id])) {
Qiang Xue committed
93
					$panel->tag = $tag;
Qiang Xue committed
94
					$panel->load($data[$id]);
Qiang Xue committed
95 96 97 98 99
				} else {
					// remove the panel since it has not received any data
					unset($this->module->panels[$id]);
				}
			}
Qiang Xue committed
100
			$this->summary = $data['summary'];
Qiang Xue committed
101
		} else {
Qiang Xue committed
102
			throw new HttpException(404, "Unable to find debug data tagged with '$tag'.");
Qiang Xue committed
103 104
		}
	}
resurtm committed
105
}