DefaultController.php 2.35 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 32
	public function actionIndex()
	{
		return $this->render('index', array(
Qiang Xue committed
33
			'manifest' => $this->getManifest(),
Qiang Xue committed
34 35 36 37
		));
	}

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

	public function actionToolbar($tag)
Qiang Xue committed
59 60 61
	{
		$this->loadData($tag);
		return $this->renderPartial('toolbar', array(
Qiang Xue committed
62
			'tag' => $tag,
Qiang Xue committed
63 64 65 66
			'panels' => $this->module->panels,
		));
	}

Qiang Xue committed
67 68 69 70 71
	public function actionPhpinfo()
	{
		phpinfo();
	}

Qiang Xue committed
72 73 74 75 76 77 78
	private $_manifest;

	protected function getManifest()
	{
		if ($this->_manifest === null) {
			$indexFile = $this->module->dataPath . '/index.json';
			if (is_file($indexFile)) {
Qiang Xue committed
79
				$this->_manifest = array_reverse(json_decode(file_get_contents($indexFile), true), true);
Qiang Xue committed
80 81 82 83 84 85 86
			} else {
				$this->_manifest = array();
			}
		}
		return $this->_manifest;
	}

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