AssetPanel.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\debug\panels;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use yii\helpers\Html;
  11. use yii\debug\Panel;
  12. use yii\web\AssetBundle;
  13. use yii\web\AssetManager;
  14. /**
  15. * Debugger panel that collects and displays asset bundles data.
  16. *
  17. * @author Artur Fursa <arturfursa@gmail.com>
  18. * @since 2.0
  19. */
  20. class AssetPanel extends Panel
  21. {
  22. /**
  23. * @inheritdoc
  24. */
  25. public function getName()
  26. {
  27. return 'Asset Bundles';
  28. }
  29. /**
  30. * @inheritdoc
  31. */
  32. public function getSummary()
  33. {
  34. return Yii::$app->view->render('panels/assets/summary', ['panel' => $this]);
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function getDetail()
  40. {
  41. return Yii::$app->view->render('panels/assets/detail', ['panel' => $this]);
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function save()
  47. {
  48. $bundles = Yii::$app->view->assetManager->bundles;
  49. if (empty($bundles)) { // bundles can be false
  50. return [];
  51. }
  52. $data = [];
  53. foreach ($bundles as $name => $bundle) {
  54. if ($bundle instanceof AssetBundle) {
  55. $bundleData = (array) $bundle;
  56. if (isset($bundleData['publishOptions']['beforeCopy']) && $bundleData['publishOptions']['beforeCopy'] instanceof \Closure) {
  57. $bundleData['publishOptions']['beforeCopy'] = '\Closure';
  58. }
  59. if (isset($bundleData['publishOptions']['afterCopy']) && $bundleData['publishOptions']['afterCopy'] instanceof \Closure) {
  60. $bundleData['publishOptions']['afterCopy'] = '\Closure';
  61. }
  62. $data[$name] = $bundleData;
  63. }
  64. }
  65. return $data;
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public function isEnabled()
  71. {
  72. try {
  73. Yii::$app->view->assetManager;
  74. } catch (InvalidConfigException $exception) {
  75. return false;
  76. }
  77. return true;
  78. }
  79. /**
  80. * Additional formatting for view.
  81. *
  82. * @param AssetBundle[] $bundles Array of bundles to formatting.
  83. *
  84. * @return AssetBundle[]
  85. */
  86. protected function format(array $bundles)
  87. {
  88. foreach ($bundles as $bundle) {
  89. $this->cssCount += count($bundle->css);
  90. $this->jsCount += count($bundle->js);
  91. array_walk($bundle->css, function(&$file, $key, $userdata) {
  92. $file = Html::a($file, $userdata->baseUrl . '/' . $file, ['target' => '_blank']);
  93. }, $bundle);
  94. array_walk($bundle->js, function(&$file, $key, $userdata) {
  95. $file = Html::a($file, $userdata->baseUrl . '/' . $file, ['target' => '_blank']);
  96. }, $bundle);
  97. array_walk($bundle->depends, function(&$depend) {
  98. $depend = Html::a($depend, '#' . $depend);
  99. });
  100. $this->formatOptions($bundle->publishOptions);
  101. $this->formatOptions($bundle->jsOptions);
  102. $this->formatOptions($bundle->cssOptions);
  103. }
  104. return $bundles;
  105. }
  106. /**
  107. * Format associative array of params to simple value.
  108. *
  109. * @param array $params
  110. *
  111. * @return array
  112. */
  113. protected function formatOptions(array &$params)
  114. {
  115. if (!is_array($params)) {
  116. return $params;
  117. }
  118. foreach ($params as $param => $value) {
  119. $params[$param] = Html::tag('strong', '\'' . $param . '\' => ') . (string) $value;
  120. }
  121. return $params;
  122. }
  123. }