DefaultController.php 1.5 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, $panel = null)
Qiang Xue committed
25
	{
Qiang Xue committed
26 27 28 29 30 31 32 33 34 35 36
		$this->loadData($tag);
		if (isset($this->module->panels[$panel])) {
			$activePanel = $this->module->panels[$panel];
		} else {
			$activePanel = reset($this->module->panels);
		}
		return $this->render('index', array(
			'tag' => $tag,
			'panels' => $this->module->panels,
			'activePanel' => $activePanel,
		));
Qiang Xue committed
37
	}
Qiang Xue committed
38 39

	public function actionToolbar($tag)
Qiang Xue committed
40 41 42 43 44 45 46 47
	{
		$this->loadData($tag);
		return $this->renderPartial('toolbar', array(
			'panels' => $this->module->panels,
		));
	}

	protected function loadData($tag)
Qiang Xue committed
48 49 50 51
	{
		$file = Yii::$app->getRuntimePath() . "/debug/$tag.log";
		if (preg_match('/^[\w\-]+$/', $tag) && is_file($file)) {
			$data = json_decode(file_get_contents($file), true);
Qiang Xue committed
52 53 54 55 56 57 58 59
			foreach ($this->module->panels as $id => $panel) {
				if (isset($data[$panel->id])) {
					$panel->load($data[$panel->id]);
				} else {
					// remove the panel since it has not received any data
					unset($this->module->panels[$id]);
				}
			}
Qiang Xue committed
60
		} else {
Qiang Xue committed
61
			throw new HttpException(404, "Unable to find debug data tagged with '$tag'.");
Qiang Xue committed
62 63
		}
	}
resurtm committed
64
}