diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 74fe91e..aba35d5 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -44,6 +44,7 @@ Yii Framework 2 Change Log - Enh #1646: Added postgresql `QueryBuilder::checkIntegrity` and `QueryBuilder::resetSequence` (Ragazzo) - Enh #1645: Added `Connection::$pdoClass` property (Ragazzo) - Enh #1681: Added support for automatically adjusting the "for" attribute of label generated by `ActiveField::label()` (qiangxue) +- Enh #1706: Added support for registering a single JS/CSS file with dependency (qiangxue) - Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark) - Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue) - Enh: Support for file aliases in console command 'message' (omnilight) diff --git a/framework/yii/web/AssetBundle.php b/framework/yii/web/AssetBundle.php index 964fe98..d588993 100644 --- a/framework/yii/web/AssetBundle.php +++ b/framework/yii/web/AssetBundle.php @@ -165,8 +165,6 @@ class AssetBundle extends Object * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding * CSS or JS files using [[AssetManager::converter|asset converter]]. * @param AssetManager $am the asset manager to perform the asset publishing - * @throws InvalidConfigException if [[baseUrl]] or [[basePath]] is not set when the bundle - * contains internal CSS or JS files. */ public function publish($am) { @@ -179,7 +177,7 @@ class AssetBundle extends Object if (isset($this->basePath, $this->baseUrl)) { $this->js[$i] = $converter->convert($js, $this->basePath, $this->baseUrl); } else { - throw new InvalidConfigException('Both of the "baseUrl" and "basePath" properties must be set.'); + $this->js[$i] = '/' . $js; } } } @@ -188,7 +186,7 @@ class AssetBundle extends Object if (isset($this->basePath, $this->baseUrl)) { $this->css[$i] = $converter->convert($css, $this->basePath, $this->baseUrl); } else { - throw new InvalidConfigException('Both of the "baseUrl" and "basePath" properties must be set.'); + $this->css[$i] = '/' . $css; } } } diff --git a/framework/yii/web/View.php b/framework/yii/web/View.php index f29a1e4..6960f19 100644 --- a/framework/yii/web/View.php +++ b/framework/yii/web/View.php @@ -312,15 +312,26 @@ class View extends \yii\base\View /** * Registers a CSS file. * @param string $url the CSS file to be registered. + * @param array $depends the names of the asset bundles that this CSS file depends on * @param array $options the HTML attributes for the link tag. * @param string $key the key that identifies the CSS script file. If null, it will use * $url as the key. If two CSS files are registered with the same key, the latter * will overwrite the former. */ - public function registerCssFile($url, $options = [], $key = null) + public function registerCssFile($url, $depends = [], $options = [], $key = null) { $key = $key ?: $url; - $this->cssFiles[$key] = Html::cssFile($url, $options); + if (empty($depends)) { + $this->cssFiles[$key] = Html::cssFile($url, $options); + } else { + $am = Yii::$app->getAssetManager(); + $am->bundles[$key] = new AssetBundle([ + 'css' => [$url], + 'cssOptions' => $options, + 'depends' => (array)$depends, + ]); + $this->registerAssetBundle($key); + } } /** @@ -350,9 +361,8 @@ class View extends \yii\base\View /** * Registers a JS file. - * Please note that when this file depends on other JS files to be registered before, - * for example jQuery, you should use [[registerAssetBundle]] instead. * @param string $url the JS file to be registered. + * @param array $depends the names of the asset bundles that this JS file depends on * @param array $options the HTML attributes for the script tag. A special option * named "position" is supported which specifies where the JS script tag should be inserted * in a page. The possible values of "position" are: @@ -365,12 +375,22 @@ class View extends \yii\base\View * $url as the key. If two JS files are registered with the same key, the latter * will overwrite the former. */ - public function registerJsFile($url, $options = [], $key = null) + public function registerJsFile($url, $depends = [], $options = [], $key = null) { - $position = isset($options['position']) ? $options['position'] : self::POS_END; - unset($options['position']); $key = $key ?: $url; - $this->jsFiles[$position][$key] = Html::jsFile($url, $options); + if (empty($depends)) { + $position = isset($options['position']) ? $options['position'] : self::POS_END; + unset($options['position']); + $this->jsFiles[$position][$key] = Html::jsFile($url, $options); + } else { + $am = Yii::$app->getAssetManager(); + $am->bundles[$key] = new AssetBundle([ + 'js' => [$url], + 'jsOptions' => $options, + 'depends' => (array)$depends, + ]); + $this->registerAssetBundle($key); + } } /**