AssetWidget.php
1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* Created by PhpStorm.
* User: timur
* Date: 19.06.17
* Time: 14:20
*/
namespace frontend\widgets;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\helpers\Html;
use yii\web\AssetBundle;
class AssetWidget extends Widget
{
public $assets = [];
public function init()
{
parent::init();
if(!empty($this->assets) && !is_array($this->assets)) {
throw new InvalidConfigException('Assets must be an array');
}
}
public function run()
{
if(!empty($this->assets)) {
$assets = $this->assets;
$bundles = $this->view->assetBundles;
foreach ($assets as $asset) {
if(array_key_exists($asset, $bundles)) {
/**
* @var AssetBundle $bundle
*/
$bundle = $bundles[$asset];
foreach ($bundle->css as $item) {
if(isset((new $asset)->sourcePath)){
$QQQ = \Yii::$app->assetManager->publish(
(new $asset)->sourcePath."/".$item,
[]
);
$item = $QQQ[1]; // [0] - full path, [1] - /assets/...
}
echo Html::cssFile($item);
}
$bundle->css = [];
}
}
}
}
}