Commit 4b328374 by artur Committed by Qiang Xue

Improvements for debug toolbar (#838) - add panel to display used asset bundles

parent f9c039e8
......@@ -210,6 +210,7 @@ class Module extends \yii\base\Module implements BootstrapInterface
'profiling' => ['class' => 'yii\debug\panels\ProfilingPanel'],
'db' => ['class' => 'yii\debug\panels\DbPanel'],
'mail' => ['class' => 'yii\debug\panels\MailPanel'],
'assets' => ['class' => 'yii\debug\panels\AssetPanel'],
];
}
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\debug\panels;
use Yii;
use yii\base\Event;
use yii\helpers\Html;
use yii\web\Application;
use yii\debug\Panel;
use yii\web\AssetBundle;
use yii\web\AssetManager;
/**
* Debugger panel that collects and displays asset bundles data.
*
* @author Artur Fursa <arturfursa@gmail.com>
* @since 2.0
*/
class AssetPanel extends Panel
{
/**
* @var integer
*/
private $cssCount = 0;
/**
* @var integer
*/
private $jsCount = 0;
/**
* @var AssetBundle[]
*/
private $bundles = [];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
Event::on(Application::className(), Application::EVENT_AFTER_ACTION, function () {
$this->bundles = $this->format(Yii::$app->view->assetManager->bundles);
});
}
/**
* @inheritdoc
*/
public function getName()
{
return 'Asset bundles';
}
/**
* @inheritdoc
*/
public function getSummary()
{
return Yii::$app->view->render('panels/assets/summary', ['panel' => $this]);
}
/**
* @inheritdoc
*/
public function getDetail()
{
return Yii::$app->view->render('panels/assets/detail', ['panel' => $this]);
}
/**
* @inheritdoc
*/
public function save()
{
$data = [
'totalBundles' => count($this->bundles),
'totalCssFiles' => $this->cssCount,
'totalJsFiles' => $this->jsCount,
'bundles' => $this->bundles,
];
return $data;
}
/**
* Additional formatting for view.
*
* @param AssetBundle[] $bundles Array of bundles to formatting.
*
* @return AssetManager
*/
protected function format(array $bundles)
{
foreach ($bundles as $bundle) {
$this->cssCount += count($bundle->css);
$this->jsCount += count($bundle->js);
array_walk($bundle->css, function(&$file, $key, $userdata) {
$file = Html::a($file, $userdata->baseUrl . '/' . $file, ['target' => '_blank']);
}, $bundle);
array_walk($bundle->js, function(&$file, $key, $userdata) {
$file = Html::a($file, $userdata->baseUrl . '/' . $file, ['target' => '_blank']);
}, $bundle);
array_walk($bundle->depends, function(&$depend) {
$depend = Html::a($depend, '#' . $depend);
});
$this->formatOptions($bundle->publishOptions);
$this->formatOptions($bundle->jsOptions);
$this->formatOptions($bundle->cssOptions);
}
return $bundles;
}
/**
* Format associative array of params to simple value.
*
* @param array $params
*
* @return array
*/
protected function formatOptions(array &$params)
{
if (!is_array($params)) {
return $params;
}
foreach ($params as $param => $value) {
$params[$param] = Html::tag('strong', '\'' . $param . '\' => ') . (string) $value;
}
return $params;
}
}
<?php
/* @var $panel yii\debug\panels\AssetPanel */
/* @var $bundles \yii\web\AssetBundle[] array */
use yii\helpers\Html;
?>
<h1>Asset bundles</h1>
<table class="table table-striped table-bordered">
<caption>
<p><b>Total bundles: <?= $panel->data['totalBundles'] ?></b>.</p>
<p>CSS files: <?= $panel->data['totalCssFiles'] ?>, JS files: <?= $panel->data['totalJsFiles'] ?></p>
</caption>
<?php
foreach ($panel->data['bundles'] as $key => $bundle) {
?>
<thead>
<tr>
<td colspan="2"><h3 id="<?= $key ?>"><?= $key ?></h3></td>
</tr>
</thead>
<tbody>
<tr>
<th>sourcePath</th>
<td><?= Html::ul([$bundle->sourcePath], ['class' => 'trace', 'encode' => false]) ?></td>
</tr>
<tr>
<th>css</th>
<td><?= Html::ul($bundle->css, ['class' => 'trace', 'encode' => false]) ?></td>
</tr>
<tr>
<th>js</th>
<td><?= Html::ul($bundle->js, ['class' => 'trace', 'encode' => false]) ?></td>
</tr>
<tr>
<th>depends</th>
<td><?= Html::ul($bundle->depends, ['class' => 'trace', 'encode' => false]) ?></td>
</tr>
<tr>
<th>publishOptions</th>
<td><?= Html::ul($bundle->publishOptions, ['class' => 'trace', 'encode' => false]) ?></td>
</tr>
<tr>
<th>basePath</th>
<td><?= Html::ul([$bundle->basePath], ['class' => 'trace', 'encode' => false]) ?></td>
</tr>
<tr>
<th>baseUrl</th>
<td><?= Html::ul([$bundle->baseUrl], ['class' => 'trace', 'encode' => false]) ?></td>
</tr>
<tr>
<th>jsOptions</th>
<td><?= Html::ul($bundle->jsOptions, ['class' => 'trace']) ?></td>
</tr>
<tr>
<th>cssOptions</th>
<td><?= Html::ul($bundle->cssOptions, ['class' => 'trace']) ?></td>
</tr>
</tbody>
<?php
}
?>
</table>
<?php
/* @var $panel yii\debug\panels\AssetPanel */
?>
<div class="yii-debug-toolbar-block">
<a href="<?= $panel->getUrl() ?>" title="Asset bundles count">Asset bundles <span class="label label-info"><?= $panel->data['totalBundles'] ?></span></a>
</div>
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