Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for debug toolbar (#838) #5089

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions extensions/debug/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ protected function corePanels()
'profiling' => ['class' => 'yii\debug\panels\ProfilingPanel'],
'db' => ['class' => 'yii\debug\panels\DbPanel'],
'mail' => ['class' => 'yii\debug\panels\MailPanel'],
'assets' => ['class' => 'yii\debug\panels\AssetPanel'],
];
}
}
141 changes: 141 additions & 0 deletions extensions/debug/panels/AssetPanel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
/**
* @link http:https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http:https://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 <[email protected]>
* @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;
}
}
64 changes: 64 additions & 0 deletions extensions/debug/views/default/panels/assets/detail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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>
6 changes: 6 additions & 0 deletions extensions/debug/views/default/panels/assets/summary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?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>