You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.3 KiB

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\debug\controllers;
use Yii;
use yii\web\Controller;
12 years ago
use yii\web\HttpException;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class DefaultController extends Controller
{
12 years ago
/** @var \yii\debug\Module */
public $module;
12 years ago
public $layout = 'main';
12 years ago
public function actionIndex($tag, $panel = null)
{
12 years ago
$meta = $this->loadData($tag);
12 years ago
if (isset($this->module->panels[$panel])) {
$activePanel = $this->module->panels[$panel];
} else {
$activePanel = reset($this->module->panels);
}
return $this->render('index', array(
'tag' => $tag,
12 years ago
'meta' => $meta,
'manifest' => $this->getManifest(),
12 years ago
'panels' => $this->module->panels,
'activePanel' => $activePanel,
));
}
public function actionToolbar($tag)
{
12 years ago
$this->loadData($tag);
return $this->renderPartial('toolbar', array(
'tag' => $tag,
12 years ago
'panels' => $this->module->panels,
));
}
12 years ago
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;
}
12 years ago
protected function loadData($tag)
{
12 years ago
$manifest = $this->getManifest();
if (isset($manifest[$tag])) {
$dataFile = $this->module->dataPath . "/$tag.json";
$data = json_decode(file_get_contents($dataFile), true);
12 years ago
foreach ($this->module->panels as $id => $panel) {
12 years ago
if (isset($data[$id])) {
$panel->load($data[$id]);
12 years ago
} else {
// remove the panel since it has not received any data
unset($this->module->panels[$id]);
}
}
12 years ago
return $manifest[$tag];
} else {
12 years ago
throw new HttpException(404, "Unable to find debug data tagged with '$tag'.");
}
}
12 years ago
}