選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AssetPanel.php 3.3KB

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