From c7e40f5c7a910dc39e89390054d52bd0d1dab4dd Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Wed, 8 May 2013 22:58:32 -0400 Subject: [PATCH] Debug toolbar WIP. --- apps/bootstrap/protected/config/main.php | 5 +++ apps/bootstrap/protected/views/layouts/main.php | 1 + framework/assets.php | 7 +++++ framework/assets/yii.debug.js | 26 ++++++++++++++++ framework/assets/yii.validation.js | 2 +- framework/debug/Module.php | 17 ++++++++++ framework/debug/Toolbar.php | 38 +++++++++++++++++++++++ framework/debug/controllers/DefaultController.php | 22 +++++++++++++ framework/logging/Logger.php | 6 ++++ 9 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 framework/assets/yii.debug.js create mode 100644 framework/debug/Module.php create mode 100644 framework/debug/Toolbar.php create mode 100644 framework/debug/controllers/DefaultController.php diff --git a/apps/bootstrap/protected/config/main.php b/apps/bootstrap/protected/config/main.php index 20b5e7e..f19dead 100644 --- a/apps/bootstrap/protected/config/main.php +++ b/apps/bootstrap/protected/config/main.php @@ -4,6 +4,11 @@ return array( 'id' => 'hello', 'basePath' => dirname(__DIR__), 'preload' => array('log'), + 'modules' => array( + 'debug' => array( + 'class' => 'yii\debug\Module', + ) + ), 'components' => array( 'cache' => array( 'class' => 'yii\caching\FileCache', diff --git a/apps/bootstrap/protected/views/layouts/main.php b/apps/bootstrap/protected/views/layouts/main.php index 1e8a3af..a81f983 100644 --- a/apps/bootstrap/protected/views/layouts/main.php +++ b/apps/bootstrap/protected/views/layouts/main.php @@ -58,6 +58,7 @@ $this->registerAssetBundle('app'); endBody(); ?> +widget('yii\debug\Toolbar'); ?> endPage(); ?> diff --git a/framework/assets.php b/framework/assets.php index 10a450a..7ee177d 100644 --- a/framework/assets.php +++ b/framework/assets.php @@ -35,4 +35,11 @@ return array( ), 'depends' => array('yii'), ), + 'yii/debug' => array( + 'sourcePath' => __DIR__ . '/assets', + 'js' => array( + 'yii.debug.js', + ), + 'depends' => array('yii'), + ), ); diff --git a/framework/assets/yii.debug.js b/framework/assets/yii.debug.js new file mode 100644 index 0000000..4e32d89 --- /dev/null +++ b/framework/assets/yii.debug.js @@ -0,0 +1,26 @@ +/** + * Yii debug module. + * + * This JavaScript module provides the functions needed by the Yii debug toolbar. + * + * @link http://www.yiiframework.com/ + * @copyright Copyright (c) 2008 Yii Software LLC + * @license http://www.yiiframework.com/license/ + * @author Qiang Xue + * @since 2.0 + */ + +yii.debug = (function ($) { + return { + load: function (id, url) { + $.ajax({ + url: url, + //dataType: 'json', + success: function(data) { + var $e = $('#' + id); + $e.html(data); + } + }); + } + }; +})(jQuery); diff --git a/framework/assets/yii.validation.js b/framework/assets/yii.validation.js index fd098be..5fa8492 100644 --- a/framework/assets/yii.validation.js +++ b/framework/assets/yii.validation.js @@ -1,7 +1,7 @@ /** * Yii validation module. * - * This JavaScript module provides the validation methods for the built-in validaotrs. + * This JavaScript module provides the validation methods for the built-in validators. * * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC diff --git a/framework/debug/Module.php b/framework/debug/Module.php new file mode 100644 index 0000000..3421d95 --- /dev/null +++ b/framework/debug/Module.php @@ -0,0 +1,17 @@ + + * @since 2.0 + */ +class Module extends \yii\base\Module +{ + public $controllerNamespace = 'yii\debug\controllers'; +} \ No newline at end of file diff --git a/framework/debug/Toolbar.php b/framework/debug/Toolbar.php new file mode 100644 index 0000000..84b55c8 --- /dev/null +++ b/framework/debug/Toolbar.php @@ -0,0 +1,38 @@ + + * @since 2.0 + */ +class Toolbar extends Widget +{ + public $debugAction = 'debug'; + public $enabled = YII_DEBUG; + + public function run() + { + if ($this->enabled) { + $id = 'yii-debug-toolbar'; + $url = Yii::$app->getUrlManager()->createUrl($this->debugAction, array( + 'tag' => Yii::getLogger()->tag, + )); + $this->view->registerJs("yii.debug.load('$id', '$url');"); + $this->view->registerAssetBundle('yii/debug'); + echo Html::tag('div', '', array( + 'id' => $id, + 'style' => 'display: none', + )); + } + } +} diff --git a/framework/debug/controllers/DefaultController.php b/framework/debug/controllers/DefaultController.php new file mode 100644 index 0000000..ca90920 --- /dev/null +++ b/framework/debug/controllers/DefaultController.php @@ -0,0 +1,22 @@ + + * @since 2.0 + */ +class DefaultController extends Controller +{ + public function actionIndex($tag) + { + echo $tag; + } +} \ No newline at end of file diff --git a/framework/logging/Logger.php b/framework/logging/Logger.php index 5c9a89d..b557c5e 100644 --- a/framework/logging/Logger.php +++ b/framework/logging/Logger.php @@ -82,6 +82,11 @@ class Logger extends Component * @var Router the log target router registered with this logger. */ public $router; + /** + * @var string a tag that uniquely identifies the current request. This can be used + * to differentiate the log messages for different requests. + */ + public $tag; /** * Initializes the logger by registering [[flush()]] as a shutdown function. @@ -89,6 +94,7 @@ class Logger extends Component public function init() { parent::init(); + $this->tag = date('Ymd-His', microtime(true)); register_shutdown_function(array($this, 'flush'), true); }