From 7fa240be656e52ff5a37496287e78f490ec12ca5 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sun, 7 Jul 2013 20:29:19 -0400 Subject: [PATCH] initial implementation of Yii debugger. --- apps/basic/config/web.php | 7 ++ apps/basic/views/layouts/main.php | 4 +- apps/basic/www/index.php | 1 + framework/yii/assets.php | 3 - framework/yii/db/Command.php | 4 +- framework/yii/debug/Module.php | 19 ++-- framework/yii/debug/assets/main.css | 11 +++ framework/yii/debug/assets/yii.debug.js | 26 ------ framework/yii/debug/panels/ConfigPanel.php | 2 +- framework/yii/debug/panels/LogPanel.php | 12 +-- framework/yii/debug/panels/RequestPanel.php | 8 +- framework/yii/debug/views/default/index.php | 3 + framework/yii/debug/views/default/toolbar.css | 121 ++++++++++++++++++++++++++ framework/yii/debug/views/default/toolbar.js | 31 +++++++ framework/yii/debug/views/default/toolbar.php | 47 +--------- 15 files changed, 203 insertions(+), 96 deletions(-) delete mode 100644 framework/yii/debug/assets/yii.debug.js create mode 100644 framework/yii/debug/views/default/toolbar.css create mode 100644 framework/yii/debug/views/default/toolbar.js diff --git a/apps/basic/config/web.php b/apps/basic/config/web.php index bea08cb..b25d92d 100644 --- a/apps/basic/config/web.php +++ b/apps/basic/config/web.php @@ -3,6 +3,13 @@ return array( 'id' => 'bootstrap', 'basePath' => dirname(__DIR__), + 'preload' => array('debug'), + 'modules' => array( + 'debug' => array( + 'class' => 'yii\debug\Module', + 'enabled' => YII_DEBUG && YII_ENV === 'dev', + ), + ), 'components' => array( 'cache' => array( 'class' => 'yii\caching\FileCache', diff --git a/apps/basic/views/layouts/main.php b/apps/basic/views/layouts/main.php index 0af2fe2..77f7b8d 100644 --- a/apps/basic/views/layouts/main.php +++ b/apps/basic/views/layouts/main.php @@ -18,8 +18,8 @@ $this->registerAssetBundle('app'); head(); ?> +beginBody(); ?>
- beginBody(); ?>

My Company

@@ -57,8 +57,8 @@ $this->registerAssetBundle('app'); Template by Twitter Bootstrap

- endBody(); ?>
+endBody(); ?> endPage(); ?> diff --git a/apps/basic/www/index.php b/apps/basic/www/index.php index 1290d10..9bc1565 100644 --- a/apps/basic/www/index.php +++ b/apps/basic/www/index.php @@ -2,6 +2,7 @@ // comment out the following line to disable debug mode defined('YII_DEBUG') or define('YII_DEBUG', true); +defined('YII_ENV') or define('YII_ENV', 'dev'); require(__DIR__ . '/../vendor/yiisoft/yii2/yii/Yii.php'); require(__DIR__ . '/../vendor/autoload.php'); diff --git a/framework/yii/assets.php b/framework/yii/assets.php index 0e0d2e6..fd3c731 100644 --- a/framework/yii/assets.php +++ b/framework/yii/assets.php @@ -53,9 +53,6 @@ return array( 'css' => array( 'main.css', ), - 'js' => array( - 'yii.debug.js', - ), 'depends' => array( 'yii', 'yii/bootstrap/responsive', diff --git a/framework/yii/db/Command.php b/framework/yii/db/Command.php index a32e892..20eb72a 100644 --- a/framework/yii/db/Command.php +++ b/framework/yii/db/Command.php @@ -281,8 +281,8 @@ class Command extends \yii\base\Component return 0; } + $token = "SQL: $sql"; try { - $token = "SQL: $sql"; Yii::beginProfile($token, __METHOD__); $this->prepare(); @@ -403,8 +403,8 @@ class Command extends \yii\base\Component } } + $token = "SQL: $sql"; try { - $token = "SQL: $sql"; Yii::beginProfile($token, __METHOD__); $this->prepare(); diff --git a/framework/yii/debug/Module.php b/framework/yii/debug/Module.php index 07ff330..5f66416 100644 --- a/framework/yii/debug/Module.php +++ b/framework/yii/debug/Module.php @@ -40,11 +40,14 @@ class Module extends \yii\base\Module */ public $dataPath = '@runtime/debug'; public $historySize = 50; + public $enabled = true; public function init() { parent::init(); - + if (!$this->enabled) { + return; + } $this->dataPath = Yii::getAlias($this->dataPath); $this->logTarget = Yii::$app->getLog()->targets['debug'] = new LogTarget($this); Yii::$app->getView()->on(View::EVENT_END_BODY, array($this, 'renderToolbar')); @@ -72,19 +75,17 @@ class Module extends \yii\base\Module public function renderToolbar($event) { - /** @var View $view */ - $id = 'yii-debug-toolbar'; - $tag = $this->logTarget->tag; $url = Yii::$app->getUrlManager()->createUrl('debug/default/toolbar', array( - 'tag' => $tag, + 'tag' => $this->logTarget->tag, )); - $view = $event->sender; - $view->registerJs("yii.debug.load('$id', '$url');"); - $view->registerAssetBundle('yii/debug'); echo Html::tag('div', '', array( - 'id' => $id, + 'id' => 'yii-debug-toolbar', + 'data-url' => $url, 'style' => 'display: none', )); + /** @var View $view */ + $view = $event->sender; + echo ''; } protected function corePanels() diff --git a/framework/yii/debug/assets/main.css b/framework/yii/debug/assets/main.css index 10c64df..a1649db 100644 --- a/framework/yii/debug/assets/main.css +++ b/framework/yii/debug/assets/main.css @@ -41,6 +41,17 @@ vertical-align: middle; } +.yii-debug-toolbar-block.title { + display: block; + float: left; + padding: 4px 8px 4px 20px; + margin-left: -20px; + font-size: 20px; + font-weight: 200; + color: #777777; + text-shadow: 0 1px 0 #ffffff; +} + span.indent { color: #ccc; } diff --git a/framework/yii/debug/assets/yii.debug.js b/framework/yii/debug/assets/yii.debug.js deleted file mode 100644 index e0d30f6..0000000 --- a/framework/yii/debug/assets/yii.debug.js +++ /dev/null @@ -1,26 +0,0 @@ -/** - * 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).show(); - } - }); - } - }; -})(jQuery); diff --git a/framework/yii/debug/panels/ConfigPanel.php b/framework/yii/debug/panels/ConfigPanel.php index 695856c..809e6dc 100644 --- a/framework/yii/debug/panels/ConfigPanel.php +++ b/framework/yii/debug/panels/ConfigPanel.php @@ -55,7 +55,7 @@ EOD; return "

Configuration

\n" . $this->renderData('Application Configuration', $app) . "\n" . $this->renderData('PHP Configuration', $php) . "\n" - . '
' . Html::a('Complete phpinfo()', array('phpinfo'), array('class' => 'btn btn-info')) . "
\n"; + . '
' . Html::a('phpinfo()', array('phpinfo'), array('class' => 'btn btn-info')) . "
\n"; } protected function renderData($caption, $values) diff --git a/framework/yii/debug/panels/LogPanel.php b/framework/yii/debug/panels/LogPanel.php index d84c493..9d1cc0c 100644 --- a/framework/yii/debug/panels/LogPanel.php +++ b/framework/yii/debug/panels/LogPanel.php @@ -28,16 +28,12 @@ class LogPanel extends Panel { $output = array(); $errorCount = count(Target::filterMessages($this->data['messages'], Logger::LEVEL_ERROR)); - if ($errorCount === 1) { - $output[] = '1 error'; - } elseif ($errorCount > 1) { - $output[] = "$errorCount errors"; + if ($errorCount) { + $output[] = '$errorCount ' . ($errorCount > 1 ? 'errors' : 'error'); } $warningCount = count(Target::filterMessages($this->data['messages'], Logger::LEVEL_WARNING)); - if ($warningCount === 1) { - $output[] = '1 warning'; - } elseif ($warningCount > 1) { - $output[] = "$warningCount warnings"; + if ($warningCount) { + $output[] = '$warningCount ' . ($warningCount > 1 ? 'warnings' : 'warning'); } if (!empty($output)) { $log = implode(', ', $output); diff --git a/framework/yii/debug/panels/RequestPanel.php b/framework/yii/debug/panels/RequestPanel.php index 2727a3a..b49bf63 100644 --- a/framework/yii/debug/panels/RequestPanel.php +++ b/framework/yii/debug/panels/RequestPanel.php @@ -25,8 +25,8 @@ class RequestPanel extends Panel public function getSummary() { - $memory = sprintf('%.1f MB', $this->data['memory'] / 1048576); - $time = number_format($this->data['time'] * 1000) . ' ms'; + $memory = '' . sprintf('%.1f MB', $this->data['memory'] / 1048576) . ''; + $time = '' . number_format($this->data['time'] * 1000) . ' ms'; return << @@ -36,6 +36,10 @@ Peak memory: $memory
Time spent: $time
+ +
+ Action: {$this->data['action']} +
EOD; } diff --git a/framework/yii/debug/views/default/index.php b/framework/yii/debug/views/default/index.php index ead7fb3..34990da 100644 --- a/framework/yii/debug/views/default/index.php +++ b/framework/yii/debug/views/default/index.php @@ -18,6 +18,9 @@ $this->title = 'Yii Debugger';