123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- namespace yii\web;
- use Yii;
- use yii\base\BaseObject;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Url;
- class AssetBundle extends BaseObject
- {
-
- public $sourcePath;
-
- public $basePath;
-
- public $baseUrl;
-
- public $depends = [];
-
- public $js = [];
-
- public $css = [];
-
- public $jsOptions = [];
-
- public $cssOptions = [];
-
- public $publishOptions = [];
-
- public static function register($view)
- {
- return $view->registerAssetBundle(get_called_class());
- }
-
- public function init()
- {
- if ($this->sourcePath !== null) {
- $this->sourcePath = rtrim(Yii::getAlias($this->sourcePath), '/\\');
- }
- if ($this->basePath !== null) {
- $this->basePath = rtrim(Yii::getAlias($this->basePath), '/\\');
- }
- if ($this->baseUrl !== null) {
- $this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
- }
- }
-
- public function registerAssetFiles($view)
- {
- $manager = $view->getAssetManager();
- foreach ($this->js as $js) {
- if (is_array($js)) {
- $file = array_shift($js);
- $options = ArrayHelper::merge($this->jsOptions, $js);
- $view->registerJsFile($manager->getAssetUrl($this, $file), $options);
- } else {
- if ($js !== null) {
- $view->registerJsFile($manager->getAssetUrl($this, $js), $this->jsOptions);
- }
- }
- }
- foreach ($this->css as $css) {
- if (is_array($css)) {
- $file = array_shift($css);
- $options = ArrayHelper::merge($this->cssOptions, $css);
- $view->registerCssFile($manager->getAssetUrl($this, $file), $options);
- } else {
- if ($css !== null) {
- $view->registerCssFile($manager->getAssetUrl($this, $css), $this->cssOptions);
- }
- }
- }
- }
-
- public function publish($am)
- {
- if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
- list($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
- }
- if (isset($this->basePath, $this->baseUrl) && ($converter = $am->getConverter()) !== null) {
- foreach ($this->js as $i => $js) {
- if (is_array($js)) {
- $file = array_shift($js);
- if (Url::isRelative($file)) {
- $js = ArrayHelper::merge($this->jsOptions, $js);
- array_unshift($js, $converter->convert($file, $this->basePath));
- $this->js[$i] = $js;
- }
- } elseif (Url::isRelative($js)) {
- $this->js[$i] = $converter->convert($js, $this->basePath);
- }
- }
- foreach ($this->css as $i => $css) {
- if (is_array($css)) {
- $file = array_shift($css);
- if (Url::isRelative($file)) {
- $css = ArrayHelper::merge($this->cssOptions, $css);
- array_unshift($css, $converter->convert($file, $this->basePath));
- $this->css[$i] = $css;
- }
- } elseif (Url::isRelative($css)) {
- $this->css[$i] = $converter->convert($css, $this->basePath);
- }
- }
- }
- }
- }
|