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;
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 21
	/** @var  \yii\debug\Module */
	public $module;
Qiang Xue committed
22 23
	public $layout = 'main';

Qiang Xue committed
24
	public function actionIndex($tag = null, $panel = null)
Qiang Xue committed
25
	{
Qiang Xue committed
26 27 28 29
		if ($tag === null) {
			$tags = array_keys($this->getManifest());
			$tag = end($tags);
		}
Qiang Xue committed
30
		$meta = $this->loadData($tag);
Qiang Xue committed
31 32 33
		if (isset($this->module->panels[$panel])) {
			$activePanel = $this->module->panels[$panel];
		} else {
Qiang Xue committed
34
			$activePanel = $this->module->panels['request'];
Qiang Xue committed
35 36 37
		}
		return $this->render('index', array(
			'tag' => $tag,
Qiang Xue committed
38 39
			'meta' => $meta,
			'manifest' => $this->getManifest(),
Qiang Xue committed
40 41 42
			'panels' => $this->module->panels,
			'activePanel' => $activePanel,
		));
Qiang Xue committed
43
	}
Qiang Xue committed
44 45

	public function actionToolbar($tag)
Qiang Xue committed
46 47 48
	{
		$this->loadData($tag);
		return $this->renderPartial('toolbar', array(
Qiang Xue committed
49
			'tag' => $tag,
Qiang Xue committed
50 51 52 53
			'panels' => $this->module->panels,
		));
	}

Qiang Xue committed
54 55 56 57 58
	public function actionPhpinfo()
	{
		phpinfo();
	}

Qiang Xue committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	private $_manifest;

	protected function getManifest()
	{
		if ($this->_manifest === null) {
			$indexFile = $this->module->dataPath . '/index.json';
			if (is_file($indexFile)) {
				$this->_manifest = json_decode(file_get_contents($indexFile), true);
			} else {
				$this->_manifest = array();
			}
			if (count($this->_manifest) > $this->module->historySize) {
				$n = count($this->_manifest) - $this->module->historySize;
				foreach (array_keys($this->_manifest) as $tag) {
					$file = $this->module->dataPath . "/$tag.json";
					@unlink($file);
					unset($this->_manifest[$tag]);
					if (--$n <= 0) {
						break;
					}
				}
				file_put_contents($indexFile, json_encode($this->_manifest));
			}
		}
		return $this->_manifest;
	}

Qiang Xue committed
86
	protected function loadData($tag)
Qiang Xue committed
87
	{
Qiang Xue committed
88 89 90 91
		$manifest = $this->getManifest();
		if (isset($manifest[$tag])) {
			$dataFile = $this->module->dataPath . "/$tag.json";
			$data = json_decode(file_get_contents($dataFile), true);
Qiang Xue committed
92
			foreach ($this->module->panels as $id => $panel) {
Qiang Xue committed
93 94
				if (isset($data[$id])) {
					$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
			return $manifest[$tag];
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
}