-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathAssetBundle.php
105 lines (93 loc) · 2.73 KB
/
AssetBundle.php
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* @package yii2-krajee-base
* @author Kartik Visweswaran <[email protected]>
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2022
* @version 3.0.5
*/
namespace kartik\base;
use Exception;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\web\View;
/**
* Asset bundle used for all Krajee extensions with bootstrap and jquery dependency.
*
* @author Kartik Visweswaran <[email protected]>
*/
class AssetBundle extends BaseAssetBundle implements BootstrapInterface
{
use BootstrapTrait;
/**
* @var bool whether to enable the dependency with yii2 bootstrap asset bundle (depending on [[bsVersion]])
*/
public $bsDependencyEnabled;
/**
* @var bool whether the bootstrap JS plugins are to be loaded and enabled
*/
public $bsPluginEnabled = false;
/**
* @inheritdoc
*/
public $depends = [
'yii\web\YiiAsset',
];
/**
* @inheritdoc
* @throws InvalidConfigException
* @throws Exception
*/
public function init()
{
if (!isset($this->bsDependencyEnabled)) {
$this->bsDependencyEnabled = ArrayHelper::getValue(Yii::$app->params, 'bsDependencyEnabled', true);
}
if ($this->bsDependencyEnabled) {
$this->initBsAssets();
}
parent::init();
}
/**
* Adds asset bundle dependency
* @param string $lib
*/
protected function addDependency($lib)
{
$index = array_search($lib, $this->depends);
if ($index !== 0 && empty($index)) {
$this->depends[] = $lib;
}
}
/**
* Initialize bootstrap assets dependencies
*/
protected function initBsAssets()
{
$lib = $this->getBsExtBasename();
$this->addDependency("yii\\{$lib}\\BootstrapAsset");
if ($this->bsPluginEnabled) {
$this->addDependency("yii\\{$lib}\\BootstrapPluginAsset");
}
}
/**
* Registers this asset bundle with a view after validating the bootstrap version
* @param View $view the view to be registered with
* @param string $bsVer the bootstrap version
* @return static the registered asset bundle instance
* @throws Exception
*/
public static function registerBundle($view, $bsVer = null)
{
$currVer = ArrayHelper::getValue(Yii::$app->params, 'bsVersion', null);
if (empty($bsVer) || static::isSameVersion($currVer, $bsVer)) {
return static::register($view);
}
Yii::$app->params['bsVersion'] = $bsVer;
$out = static::register($view);
if (!empty($currVer)) {
Yii::$app->params['bsVersion'] = $currVer;
}
return $out;
}
}