DefaultController.php 2.49 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;
12
use yii\web\NotFoundHttpException;
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

30 31 32 33 34 35 36 37 38
	public function actions()
	{
		$actions = [];
		foreach($this->module->panels as $panel) {
			$actions = array_merge($actions, $panel->actions);
		}
		return $actions;
	}

Qiang Xue committed
39 40
	public function actionIndex()
	{
Alexander Makarov committed
41
		return $this->render('index', ['manifest' => $this->getManifest()]);
Qiang Xue committed
42 43 44
	}

	public function actionView($tag = null, $panel = null)
Qiang Xue committed
45
	{
Qiang Xue committed
46 47
		if ($tag === null) {
			$tags = array_keys($this->getManifest());
Qiang Xue committed
48
			$tag = reset($tags);
Qiang Xue committed
49
		}
Qiang Xue committed
50
		$this->loadData($tag);
Qiang Xue committed
51 52 53
		if (isset($this->module->panels[$panel])) {
			$activePanel = $this->module->panels[$panel];
		} else {
Qiang Xue committed
54
			$activePanel = $this->module->panels['request'];
Qiang Xue committed
55
		}
Alexander Makarov committed
56
		return $this->render('view', [
Qiang Xue committed
57
			'tag' => $tag,
Qiang Xue committed
58
			'summary' => $this->summary,
Qiang Xue committed
59
			'manifest' => $this->getManifest(),
Qiang Xue committed
60 61
			'panels' => $this->module->panels,
			'activePanel' => $activePanel,
Alexander Makarov committed
62
		]);
Qiang Xue committed
63
	}
Qiang Xue committed
64 65

	public function actionToolbar($tag)
Qiang Xue committed
66 67
	{
		$this->loadData($tag);
Alexander Makarov committed
68
		return $this->renderPartial('toolbar', [
Qiang Xue committed
69
			'tag' => $tag,
Qiang Xue committed
70
			'panels' => $this->module->panels,
Alexander Makarov committed
71
		]);
Qiang Xue committed
72 73
	}

Qiang Xue committed
74 75 76 77 78
	public function actionPhpinfo()
	{
		phpinfo();
	}

Qiang Xue committed
79 80 81 82 83
	private $_manifest;

	protected function getManifest()
	{
		if ($this->_manifest === null) {
Qiang Xue committed
84
			$indexFile = $this->module->dataPath . '/index.data';
Qiang Xue committed
85
			if (is_file($indexFile)) {
86
				$this->_manifest = array_reverse(unserialize(file_get_contents($indexFile)), true);
Qiang Xue committed
87
			} else {
Alexander Makarov committed
88
				$this->_manifest = [];
Qiang Xue committed
89 90 91 92 93
			}
		}
		return $this->_manifest;
	}

94
	public function loadData($tag)
Qiang Xue committed
95
	{
Qiang Xue committed
96 97
		$manifest = $this->getManifest();
		if (isset($manifest[$tag])) {
Qiang Xue committed
98
			$dataFile = $this->module->dataPath . "/$tag.data";
99
			$data = unserialize(file_get_contents($dataFile));
Qiang Xue committed
100
			foreach ($this->module->panels as $id => $panel) {
Qiang Xue committed
101
				if (isset($data[$id])) {
Qiang Xue committed
102
					$panel->tag = $tag;
Qiang Xue committed
103
					$panel->load($data[$id]);
Qiang Xue committed
104 105 106 107 108
				} else {
					// remove the panel since it has not received any data
					unset($this->module->panels[$id]);
				}
			}
Qiang Xue committed
109
			$this->summary = $data['summary'];
Qiang Xue committed
110
		} else {
111
			throw new NotFoundHttpException("Unable to find debug data tagged with '$tag'.");
Qiang Xue committed
112 113
		}
	}
resurtm committed
114
}