mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-30 22:48:19 +08:00
debug toolbar WIP
This commit is contained in:
@@ -25,13 +25,21 @@ use yii\widgets\FragmentCache;
|
||||
class View extends Component
|
||||
{
|
||||
/**
|
||||
* @event ViewEvent an event that is triggered by [[beginPage()]].
|
||||
* @event Event an event that is triggered by [[beginPage()]].
|
||||
*/
|
||||
const EVENT_BEGIN_PAGE = 'beginPage';
|
||||
/**
|
||||
* @event ViewEvent an event that is triggered by [[endPage()]].
|
||||
* @event Event an event that is triggered by [[endPage()]].
|
||||
*/
|
||||
const EVENT_END_PAGE = 'endPage';
|
||||
/**
|
||||
* @event Event an event that is triggered by [[beginBody()]].
|
||||
*/
|
||||
const EVENT_BEGIN_BODY = 'beginBody';
|
||||
/**
|
||||
* @event Event an event that is triggered by [[endBody()]].
|
||||
*/
|
||||
const EVENT_END_BODY = 'endBody';
|
||||
/**
|
||||
* @event ViewEvent an event that is triggered by [[renderFile()]] right before it renders a view file.
|
||||
*/
|
||||
@@ -532,6 +540,7 @@ class View extends Component
|
||||
public function beginBody()
|
||||
{
|
||||
echo self::PL_BODY_BEGIN;
|
||||
$this->trigger(self::EVENT_BEGIN_BODY);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -539,6 +548,7 @@ class View extends Component
|
||||
*/
|
||||
public function endBody()
|
||||
{
|
||||
$this->trigger(self::EVENT_END_BODY);
|
||||
echo self::PL_BODY_END;
|
||||
}
|
||||
|
||||
|
||||
54
framework/yii/debug/Debugger.php
Normal file
54
framework/yii/debug/Debugger.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\debug;
|
||||
|
||||
use Yii;
|
||||
use yii\base\Component;
|
||||
use yii\base\View;
|
||||
use yii\helpers\Html;
|
||||
|
||||
/**
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class Debugger extends Component
|
||||
{
|
||||
public $debugAction = 'debug/default/toolbar';
|
||||
public $panels;
|
||||
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
Yii::$app->setModule('debug', array(
|
||||
'class' => 'yii\debug\Module',
|
||||
'panels' => $this->panels,
|
||||
));
|
||||
Yii::$app->log->targets[] = new LogTarget;
|
||||
Yii::$app->getView()->on(View::EVENT_END_BODY, array($this, 'renderToolbar'));
|
||||
}
|
||||
|
||||
public function renderToolbar($event)
|
||||
{
|
||||
if (Yii::$app->getModule('debug', false) !== null) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var View $view */
|
||||
$id = 'yii-debug-toolbar';
|
||||
$url = Yii::$app->getUrlManager()->createUrl($this->debugAction, array(
|
||||
'tag' => Yii::getLogger()->tag,
|
||||
));
|
||||
$view = $event->sender;
|
||||
$view->registerJs("yii.debug.load('$id', '$url');");
|
||||
$view->registerAssetBundle('yii/debug');
|
||||
echo Html::tag('div', '', array(
|
||||
'id' => $id,
|
||||
'style' => 'display: none',
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -5,15 +5,16 @@
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\logging;
|
||||
namespace yii\debug;
|
||||
|
||||
use Yii;
|
||||
use yii\logging\Target;
|
||||
|
||||
/**
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class DebugTarget extends Target
|
||||
class LogTarget extends Target
|
||||
{
|
||||
public $maxLogFiles = 20;
|
||||
|
||||
@@ -72,6 +73,7 @@ class DebugTarget extends Target
|
||||
$iterator = new \DirectoryIterator(Yii::$app->getRuntimePath() . '/debug');
|
||||
$files = array();
|
||||
foreach ($iterator as $file) {
|
||||
/** @var \DirectoryIterator $file */
|
||||
if (preg_match('/^[\d\-]+\.log$/', $file->getFileName()) && $file->isFile()) {
|
||||
$files[] = $file->getPathname();
|
||||
}
|
||||
@@ -14,4 +14,5 @@ namespace yii\debug;
|
||||
class Module extends \yii\base\Module
|
||||
{
|
||||
public $controllerNamespace = 'yii\debug\controllers';
|
||||
public $panels;
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\debug;
|
||||
|
||||
use Yii;
|
||||
use yii\base\Widget;
|
||||
use yii\helpers\Html;
|
||||
|
||||
/**
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class Toolbar extends Widget
|
||||
{
|
||||
public $debugAction = 'debug/default/toolbar';
|
||||
|
||||
public function run()
|
||||
{
|
||||
if (Yii::$app->hasModule('debug')) {
|
||||
$id = 'yii-debug-toolbar';
|
||||
$url = Yii::$app->getUrlManager()->createUrl($this->debugAction, array(
|
||||
'tag' => Yii::getLogger()->tag,
|
||||
));
|
||||
$view = $this->getView();
|
||||
$view->registerJs("yii.debug.load('$id', '$url');");
|
||||
$view->registerAssetBundle('yii/debug');
|
||||
echo Html::tag('div', '', array(
|
||||
'id' => $id,
|
||||
'style' => 'display: none',
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,9 +16,11 @@ use yii\web\Controller;
|
||||
*/
|
||||
class DefaultController extends Controller
|
||||
{
|
||||
public $layout = 'main';
|
||||
|
||||
public function actionIndex($tag)
|
||||
{
|
||||
echo $tag;
|
||||
return $this->render('index');
|
||||
}
|
||||
|
||||
public function actionToolbar($tag)
|
||||
@@ -26,9 +28,10 @@ class DefaultController extends Controller
|
||||
$file = Yii::$app->getRuntimePath() . "/debug/$tag.log";
|
||||
if (preg_match('/^[\w\-]+$/', $tag) && is_file($file)) {
|
||||
$data = json_decode(file_get_contents($file), true);
|
||||
echo $this->renderPartial('toolbar', $data);
|
||||
$data['tag'] = $tag;
|
||||
return $this->renderPartial('toolbar', $data);
|
||||
} else {
|
||||
echo "Unable to find debug data tagged with '$tag'.";
|
||||
return "Unable to find debug data tagged with '$tag'.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
1
framework/yii/debug/views/default/index.php
Normal file
1
framework/yii/debug/views/default/index.php
Normal file
@@ -0,0 +1 @@
|
||||
here we are
|
||||
@@ -19,21 +19,22 @@ echo Html::style("
|
||||
margin: 0 10px;
|
||||
");
|
||||
?>
|
||||
<div id="yii-debug-toolbar">
|
||||
<div class="yii-debug-toolbar-block">
|
||||
<?php echo Html::a('more details', array('index', 'tag' => $tag)); ?>
|
||||
</div>
|
||||
|
||||
<div class="yii-debug-toolbar-block">
|
||||
<div class="yii-debug-toolbar-block">
|
||||
Peak memory: <?php echo sprintf('%.2fMB', $memory / 1048576); ?>
|
||||
</div>
|
||||
|
||||
<div class="yii-debug-toolbar-block">
|
||||
Time spent: <?php echo sprintf('%.3fs', $time); ?>
|
||||
</div>
|
||||
|
||||
<div class="yii-debug-toolbar-block">
|
||||
</div>
|
||||
|
||||
<div class="yii-debug-toolbar-block">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="yii-debug-toolbar-block">
|
||||
Peak memory: <?php echo sprintf('%.2fMB', $memory / 1048576); ?>
|
||||
</div>
|
||||
|
||||
<div class="yii-debug-toolbar-block">
|
||||
Time spent: <?php echo sprintf('%.3fs', $time); ?>
|
||||
</div>
|
||||
|
||||
<div class="yii-debug-toolbar-block">
|
||||
</div>
|
||||
|
||||
<div class="yii-debug-toolbar-block">
|
||||
</div>
|
||||
|
||||
|
||||
21
framework/yii/debug/views/layouts/main.php
Normal file
21
framework/yii/debug/views/layouts/main.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
/**
|
||||
* @var \yii\base\View $this
|
||||
* @var string $content
|
||||
*/
|
||||
use yii\helpers\Html;
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<?php $this->beginPage(); ?>
|
||||
<head>
|
||||
<title><?php echo Html::encode($this->title); ?></title>
|
||||
<?php $this->head(); ?>
|
||||
</head>
|
||||
<body>
|
||||
<?php $this->beginBody(); ?>
|
||||
<?php echo $content; ?>
|
||||
<?php $this->endBody(); ?>
|
||||
</body>
|
||||
<?php $this->endPage(); ?>
|
||||
</html>
|
||||
Reference in New Issue
Block a user