Commit 840a84db by Qiang Xue

debug toolbar refactoring.

parent b6ffa7c9
...@@ -20,12 +20,14 @@ class LogTarget extends Target ...@@ -20,12 +20,14 @@ class LogTarget extends Target
* @var Module * @var Module
*/ */
public $module; public $module;
public $maxLogFiles = 20; public $tag;
public $historySize = 20;
public function __construct($module, $config = array()) public function __construct($module, $config = array())
{ {
parent::__construct($config); parent::__construct($config);
$this->module = $module; $this->module = $module;
$this->tag = date('Ymd-His', microtime(true));
} }
/** /**
...@@ -38,8 +40,7 @@ class LogTarget extends Target ...@@ -38,8 +40,7 @@ class LogTarget extends Target
if (!is_dir($path)) { if (!is_dir($path)) {
mkdir($path); mkdir($path);
} }
$tag = Yii::$app->getLog()->getTag(); $file = "$path/{$this->tag}.log";
$file = "$path/$tag.log";
$data = array(); $data = array();
foreach ($this->module->panels as $panel) { foreach ($this->module->panels as $panel) {
$data[$panel->id] = $panel->save(); $data[$panel->id] = $panel->save();
...@@ -78,8 +79,8 @@ class LogTarget extends Target ...@@ -78,8 +79,8 @@ class LogTarget extends Target
} }
} }
sort($files); sort($files);
if (count($files) > $this->maxLogFiles) { if (count($files) > $this->historySize) {
$n = count($files) - $this->maxLogFiles; $n = count($files) - $this->historySize;
foreach ($files as $i => $file) { foreach ($files as $i => $file) {
if ($i < $n) { if ($i < $n) {
unlink($file); unlink($file);
......
...@@ -63,8 +63,9 @@ class Module extends \yii\base\Module ...@@ -63,8 +63,9 @@ class Module extends \yii\base\Module
{ {
/** @var View $view */ /** @var View $view */
$id = 'yii-debug-toolbar'; $id = 'yii-debug-toolbar';
$tag = Yii::$app->getLog()->targets['debug']->tag;
$url = Yii::$app->getUrlManager()->createUrl('debug/default/toolbar', array( $url = Yii::$app->getUrlManager()->createUrl('debug/default/toolbar', array(
'tag' => Yii::$app->getLog()->getTag(), 'tag' => $tag,
)); ));
$view = $event->sender; $view = $event->sender;
$view->registerJs("yii.debug.load('$id', '$url');"); $view->registerJs("yii.debug.load('$id', '$url');");
......
...@@ -40,6 +40,7 @@ class DefaultController extends Controller ...@@ -40,6 +40,7 @@ class DefaultController extends Controller
{ {
$this->loadData($tag); $this->loadData($tag);
return $this->renderPartial('toolbar', array( return $this->renderPartial('toolbar', array(
'tag' => $tag,
'panels' => $this->module->panels, 'panels' => $this->module->panels,
)); ));
} }
......
...@@ -24,12 +24,10 @@ class ConfigPanel extends Panel ...@@ -24,12 +24,10 @@ class ConfigPanel extends Panel
public function getSummary() public function getSummary()
{ {
$link = Html::a('more details', array('index', 'tag' => $this->data['tag']));
return <<<EOD return <<<EOD
<div class="yii-debug-toolbar-block"> <div class="yii-debug-toolbar-block">
PHP: {$this->data['phpVersion']}, PHP: {$this->data['phpVersion']},
Yii: {$this->data['phpVersion']}, Yii: {$this->data['phpVersion']}
$link
</div> </div>
EOD; EOD;
} }
...@@ -42,7 +40,6 @@ EOD; ...@@ -42,7 +40,6 @@ EOD;
public function save() public function save()
{ {
return array( return array(
'tag' => Yii::$app->getLog()->getTag(),
'phpVersion' => PHP_VERSION, 'phpVersion' => PHP_VERSION,
'yiiVersion' => Yii::getVersion(), 'yiiVersion' => Yii::getVersion(),
); );
......
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
/** /**
* @var \yii\base\View $this * @var \yii\base\View $this
* @var \yii\debug\Panel[] $panels * @var \yii\debug\Panel[] $panels
* @var string $tag
*/ */
use yii\helpers\Html;
?> ?>
<style> <style>
#yii-debug-toolbar { #yii-debug-toolbar {
...@@ -28,4 +30,7 @@ ...@@ -28,4 +30,7 @@
<?php foreach ($panels as $panel): ?> <?php foreach ($panels as $panel): ?>
<?php echo $panel->getSummary(); ?> <?php echo $panel->getSummary(); ?>
<?php endforeach; ?> <?php endforeach; ?>
<div class="yii-debug-toolbar-block">
<?php echo Html::a('more details', array('index', 'tag' => $tag)); ?>
</div>
</div> </div>
...@@ -129,13 +129,6 @@ class Logger extends Component ...@@ -129,13 +129,6 @@ class Logger extends Component
*/ */
public $targets = array(); public $targets = array();
/**
* @var string
*/
private $_tag;
/** /**
* Initializes the logger by registering [[flush()]] as a shutdown function. * Initializes the logger by registering [[flush()]] as a shutdown function.
*/ */
...@@ -197,25 +190,6 @@ class Logger extends Component ...@@ -197,25 +190,6 @@ class Logger extends Component
} }
/** /**
* @return string a tag that uniquely identifies the current request.
*/
public function getTag()
{
if ($this->_tag === null) {
$this->_tag = date('Ymd-His', microtime(true));
}
return $this->_tag;
}
/**
* @param string $tag a tag that uniquely identifies the current request.
*/
public function setTag($tag)
{
$this->_tag = $tag;
}
/**
* Returns the total elapsed time since the start of the current request. * Returns the total elapsed time since the start of the current request.
* This method calculates the difference between now and the timestamp * This method calculates the difference between now and the timestamp
* defined by constant `YII_BEGIN_TIME` which is evaluated at the beginning * defined by constant `YII_BEGIN_TIME` which is evaluated at the beginning
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment