Commit 7fa240be by Qiang Xue

initial implementation of Yii debugger.

parent 30ec0afd
...@@ -3,6 +3,13 @@ ...@@ -3,6 +3,13 @@
return array( return array(
'id' => 'bootstrap', 'id' => 'bootstrap',
'basePath' => dirname(__DIR__), 'basePath' => dirname(__DIR__),
'preload' => array('debug'),
'modules' => array(
'debug' => array(
'class' => 'yii\debug\Module',
'enabled' => YII_DEBUG && YII_ENV === 'dev',
),
),
'components' => array( 'components' => array(
'cache' => array( 'cache' => array(
'class' => 'yii\caching\FileCache', 'class' => 'yii\caching\FileCache',
......
...@@ -18,8 +18,8 @@ $this->registerAssetBundle('app'); ...@@ -18,8 +18,8 @@ $this->registerAssetBundle('app');
<?php $this->head(); ?> <?php $this->head(); ?>
</head> </head>
<body> <body>
<?php $this->beginBody(); ?>
<div class="container"> <div class="container">
<?php $this->beginBody(); ?>
<div class="masthead"> <div class="masthead">
<h3 class="muted">My Company</h3> <h3 class="muted">My Company</h3>
...@@ -57,8 +57,8 @@ $this->registerAssetBundle('app'); ...@@ -57,8 +57,8 @@ $this->registerAssetBundle('app');
Template by <a href="http://twitter.github.io/bootstrap/">Twitter Bootstrap</a> Template by <a href="http://twitter.github.io/bootstrap/">Twitter Bootstrap</a>
</p> </p>
</div> </div>
<?php $this->endBody(); ?>
</div> </div>
<?php $this->endBody(); ?>
</body> </body>
</html> </html>
<?php $this->endPage(); ?> <?php $this->endPage(); ?>
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// comment out the following line to disable debug mode // comment out the following line to disable debug mode
defined('YII_DEBUG') or define('YII_DEBUG', true); 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/yiisoft/yii2/yii/Yii.php');
require(__DIR__ . '/../vendor/autoload.php'); require(__DIR__ . '/../vendor/autoload.php');
......
...@@ -53,9 +53,6 @@ return array( ...@@ -53,9 +53,6 @@ return array(
'css' => array( 'css' => array(
'main.css', 'main.css',
), ),
'js' => array(
'yii.debug.js',
),
'depends' => array( 'depends' => array(
'yii', 'yii',
'yii/bootstrap/responsive', 'yii/bootstrap/responsive',
......
...@@ -281,8 +281,8 @@ class Command extends \yii\base\Component ...@@ -281,8 +281,8 @@ class Command extends \yii\base\Component
return 0; return 0;
} }
$token = "SQL: $sql";
try { try {
$token = "SQL: $sql";
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$this->prepare(); $this->prepare();
...@@ -403,8 +403,8 @@ class Command extends \yii\base\Component ...@@ -403,8 +403,8 @@ class Command extends \yii\base\Component
} }
} }
$token = "SQL: $sql";
try { try {
$token = "SQL: $sql";
Yii::beginProfile($token, __METHOD__); Yii::beginProfile($token, __METHOD__);
$this->prepare(); $this->prepare();
......
...@@ -40,11 +40,14 @@ class Module extends \yii\base\Module ...@@ -40,11 +40,14 @@ class Module extends \yii\base\Module
*/ */
public $dataPath = '@runtime/debug'; public $dataPath = '@runtime/debug';
public $historySize = 50; public $historySize = 50;
public $enabled = true;
public function init() public function init()
{ {
parent::init(); parent::init();
if (!$this->enabled) {
return;
}
$this->dataPath = Yii::getAlias($this->dataPath); $this->dataPath = Yii::getAlias($this->dataPath);
$this->logTarget = Yii::$app->getLog()->targets['debug'] = new LogTarget($this); $this->logTarget = Yii::$app->getLog()->targets['debug'] = new LogTarget($this);
Yii::$app->getView()->on(View::EVENT_END_BODY, array($this, 'renderToolbar')); Yii::$app->getView()->on(View::EVENT_END_BODY, array($this, 'renderToolbar'));
...@@ -72,19 +75,17 @@ class Module extends \yii\base\Module ...@@ -72,19 +75,17 @@ class Module extends \yii\base\Module
public function renderToolbar($event) public function renderToolbar($event)
{ {
/** @var View $view */
$id = 'yii-debug-toolbar';
$tag = $this->logTarget->tag;
$url = Yii::$app->getUrlManager()->createUrl('debug/default/toolbar', array( $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( echo Html::tag('div', '', array(
'id' => $id, 'id' => 'yii-debug-toolbar',
'data-url' => $url,
'style' => 'display: none', 'style' => 'display: none',
)); ));
/** @var View $view */
$view = $event->sender;
echo '<script>' . $view->renderFile(__DIR__ . '/views/default/toolbar.js') . '</script>';
} }
protected function corePanels() protected function corePanels()
......
...@@ -41,6 +41,17 @@ ...@@ -41,6 +41,17 @@
vertical-align: middle; 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 { span.indent {
color: #ccc; color: #ccc;
} }
/**
* 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 <qiang.xue@gmail.com>
* @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);
...@@ -55,7 +55,7 @@ EOD; ...@@ -55,7 +55,7 @@ EOD;
return "<h1>Configuration</h1>\n" return "<h1>Configuration</h1>\n"
. $this->renderData('Application Configuration', $app) . "\n" . $this->renderData('Application Configuration', $app) . "\n"
. $this->renderData('PHP Configuration', $php) . "\n" . $this->renderData('PHP Configuration', $php) . "\n"
. '<div>' . Html::a('Complete phpinfo()', array('phpinfo'), array('class' => 'btn btn-info')) . "</div>\n"; . '<div>' . Html::a('phpinfo()', array('phpinfo'), array('class' => 'btn btn-info')) . "</div>\n";
} }
protected function renderData($caption, $values) protected function renderData($caption, $values)
......
...@@ -28,16 +28,12 @@ class LogPanel extends Panel ...@@ -28,16 +28,12 @@ class LogPanel extends Panel
{ {
$output = array(); $output = array();
$errorCount = count(Target::filterMessages($this->data['messages'], Logger::LEVEL_ERROR)); $errorCount = count(Target::filterMessages($this->data['messages'], Logger::LEVEL_ERROR));
if ($errorCount === 1) { if ($errorCount) {
$output[] = '1 error'; $output[] = '<span class="label label-important">$errorCount</span> ' . ($errorCount > 1 ? 'errors' : 'error');
} elseif ($errorCount > 1) {
$output[] = "$errorCount errors";
} }
$warningCount = count(Target::filterMessages($this->data['messages'], Logger::LEVEL_WARNING)); $warningCount = count(Target::filterMessages($this->data['messages'], Logger::LEVEL_WARNING));
if ($warningCount === 1) { if ($warningCount) {
$output[] = '1 warning'; $output[] = '<span class="label label-warning">$warningCount</span> ' . ($warningCount > 1 ? 'warnings' : 'warning');
} elseif ($warningCount > 1) {
$output[] = "$warningCount warnings";
} }
if (!empty($output)) { if (!empty($output)) {
$log = implode(', ', $output); $log = implode(', ', $output);
......
...@@ -25,8 +25,8 @@ class RequestPanel extends Panel ...@@ -25,8 +25,8 @@ class RequestPanel extends Panel
public function getSummary() public function getSummary()
{ {
$memory = sprintf('%.1f MB', $this->data['memory'] / 1048576); $memory = '<span class="label">' . sprintf('%.1f MB', $this->data['memory'] / 1048576) . '</span>';
$time = number_format($this->data['time'] * 1000) . ' ms'; $time = '<span class="label">' . number_format($this->data['time'] * 1000) . ' ms</span>';
return <<<EOD return <<<EOD
<div class="yii-debug-toolbar-block"> <div class="yii-debug-toolbar-block">
...@@ -36,6 +36,10 @@ Peak memory: $memory ...@@ -36,6 +36,10 @@ Peak memory: $memory
<div class="yii-debug-toolbar-block"> <div class="yii-debug-toolbar-block">
Time spent: $time Time spent: $time
</div> </div>
<div class="yii-debug-toolbar-block">
Action: {$this->data['action']}
</div>
EOD; EOD;
} }
......
...@@ -18,6 +18,9 @@ $this->title = 'Yii Debugger'; ...@@ -18,6 +18,9 @@ $this->title = 'Yii Debugger';
<div class="navbar"> <div class="navbar">
<div class="navbar-inner"> <div class="navbar-inner">
<div class="container"> <div class="container">
<div class="yii-debug-toolbar-block title">
Yii Debugger
</div>
<?php foreach ($panels as $panel): ?> <?php foreach ($panels as $panel): ?>
<?php echo $panel->getSummary(); ?> <?php echo $panel->getSummary(); ?>
<?php endforeach; ?> <?php endforeach; ?>
......
#yii-debug-toolbar {
position: fixed;
left: 0;
right: 0;
bottom: 0;
margin: 0;
padding: 0;
z-index: 1000000;
font: 12px Verdana, Arial, sans-serif;
text-align: left;
height: 38px;
border-top: 1px solid #ccc;
background: rgb(237,237,237);
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2VkZWRlZCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjUzJSIgc3RvcC1jb2xvcj0iI2Y2ZjZmNiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNmZmZmZmYiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
background: -moz-linear-gradient(top, rgba(237,237,237,1) 0%, rgba(246,246,246,1) 53%, rgba(255,255,255,1) 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(237,237,237,1)), color-stop(53%,rgba(246,246,246,1)), color-stop(100%,rgba(255,255,255,1)));
background: -webkit-linear-gradient(top, rgba(237,237,237,1) 0%,rgba(246,246,246,1) 53%,rgba(255,255,255,1) 100%);
background: -o-linear-gradient(top, rgba(237,237,237,1) 0%,rgba(246,246,246,1) 53%,rgba(255,255,255,1) 100%);
background: -ms-linear-gradient(top, rgba(237,237,237,1) 0%,rgba(246,246,246,1) 53%,rgba(255,255,255,1) 100%);
background: linear-gradient(to bottom, rgba(237,237,237,1) 0%,rgba(246,246,246,1) 53%,rgba(255,255,255,1) 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ededed', endColorstr='#ffffff',GradientType=0 );
}
.yii-debug-toolbar-block {
float: left;
margin: 0;
border-right: 1px solid #e4e4e4;
padding: 4px 8px;
line-height: 32px;
}
.yii-debug-toolbar-block a {
text-decoration: none;
color: black;
}
.yii-debug-toolbar-block span {
}
.yii-debug-toolbar-block img {
vertical-align: middle;
}
#yii-debug-toolbar .label {
display: inline-block;
padding: 2px 4px;
font-size: 11.844px;
font-weight: bold;
line-height: 14px;
color: #ffffff;
text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
white-space: nowrap;
vertical-align: baseline;
background-color: #999999;
}
#yii-debug-toolbar .label {
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}
#yii-debug-toolbar .label:empty {
display: none;
}
#yii-debug-toolbar a.label:hover,
#yii-debug-toolbar a.label:focus {
color: #ffffff;
text-decoration: none;
cursor: pointer;
}
#yii-debug-toolbar .label-important {
background-color: #b94a48;
}
#yii-debug-toolbar .label-important[href] {
background-color: #953b39;
}
#yii-debug-toolbar .label-warning,
#yii-debug-toolbar .badge-warning {
background-color: #f89406;
}
#yii-debug-toolbar .label-warning[href] {
background-color: #c67605;
}
#yii-debug-toolbar .label-success {
background-color: #468847;
}
#yii-debug-toolbar .label-success[href] {
background-color: #356635;
}
#yii-debug-toolbar .label-info {
background-color: #3a87ad;
}
#yii-debug-toolbar .label-info[href] {
background-color: #2d6987;
}
#yii-debug-toolbar .label-inverse,
#yii-debug-toolbar .badge-inverse {
background-color: #333333;
}
#yii-debug-toolbar .label-inverse[href],
#yii-debug-toolbar .badge-inverse[href] {
background-color: #1a1a1a;
}
#yii-debug-toolbar .debugger-link a {
padding: 3px 5px;
font-weight: normal;
color: white;
}
(function() {
var ajax = function(url, settings) {
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
settings = settings || {};
xhr.open(settings.method || 'GET', url, true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onreadystatechange = function(state) {
if (xhr.readyState == 4) {
if (xhr.status == 200 && settings.success) {
settings.success(xhr);
} else if (xhr.status != 200 && settings.error) {
settings.error(xhr);
}
}
};
xhr.send(settings.data || '');
};
var e = document.getElementById('yii-debug-toolbar');
if (e) {
e.style.display = 'block';
var url = e.getAttribute('data-url');
ajax(url, {
success: function(xhr) {
e.innerHTML = xhr.responseText;
},
error: function(xhr) {
e.innerHTML = xhr.responseText;
}
});
}
})();
...@@ -7,51 +7,12 @@ ...@@ -7,51 +7,12 @@
use yii\helpers\Html; use yii\helpers\Html;
?> ?>
<style> <style>
#yii-debug-toolbar { <?php echo $this->renderFile(__DIR__ . '/toolbar.css'); ?>
position: fixed;
left: 0;
right: 0;
bottom: 0;
margin: 0;
padding: 0;
z-index: 1000000;
font: 11px Verdana, Arial, sans-serif;
text-align: left;
height: 38px;
border-top: 1px solid #ccc;
background: rgb(237,237,237);
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2VkZWRlZCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjUzJSIgc3RvcC1jb2xvcj0iI2Y2ZjZmNiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNmZmZmZmYiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
background: -moz-linear-gradient(top, rgba(237,237,237,1) 0%, rgba(246,246,246,1) 53%, rgba(255,255,255,1) 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(237,237,237,1)), color-stop(53%,rgba(246,246,246,1)), color-stop(100%,rgba(255,255,255,1)));
background: -webkit-linear-gradient(top, rgba(237,237,237,1) 0%,rgba(246,246,246,1) 53%,rgba(255,255,255,1) 100%);
background: -o-linear-gradient(top, rgba(237,237,237,1) 0%,rgba(246,246,246,1) 53%,rgba(255,255,255,1) 100%);
background: -ms-linear-gradient(top, rgba(237,237,237,1) 0%,rgba(246,246,246,1) 53%,rgba(255,255,255,1) 100%);
background: linear-gradient(to bottom, rgba(237,237,237,1) 0%,rgba(246,246,246,1) 53%,rgba(255,255,255,1) 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ededed', endColorstr='#ffffff',GradientType=0 );
}
.yii-debug-toolbar-block {
float: left;
margin: 0;
border-right: 1px solid #e4e4e4;
padding: 4px 8px;
line-height: 32px;
}
.yii-debug-toolbar-block a {
text-decoration: none;
color: black !important;
}
.yii-debug-toolbar-block span {
}
.yii-debug-toolbar-block img {
vertical-align: middle;
}
</style> </style>
<div id="yii-debug-toolbar"> <div id="yii-debug-toolbar">
<div class="yii-debug-toolbar-block debugger-link">
<?php echo Html::a('Yii Debugger', array('index', 'tag' => $tag), array('class' => 'label')); ?>
</div>
<?php foreach ($panels as $panel): ?> <?php foreach ($panels as $panel): ?>
<?php echo $panel->getSummary(); ?> <?php echo $panel->getSummary(); ?>
<?php endforeach; ?> <?php endforeach; ?>
......
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