Issues (902)

framework/web/AssetBundle.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use Yii;
11
use yii\base\BaseObject;
12
use yii\helpers\ArrayHelper;
13
use yii\helpers\Url;
14
15
/**
16
 * AssetBundle represents a collection of asset files, such as CSS, JS, images.
17
 *
18
 * Each asset bundle has a unique name that globally identifies it among all asset bundles used in an application.
19
 * The name is the [fully qualified class name](https://www.php.net/manual/en/language.namespaces.rules.php)
20
 * of the class representing it.
21
 *
22
 * An asset bundle can depend on other asset bundles. When registering an asset bundle
23
 * with a view, all its dependent asset bundles will be automatically registered.
24
 *
25
 * For more details and usage information on AssetBundle, see the [guide article on assets](guide:structure-assets).
26
 *
27
 * @author Qiang Xue <[email protected]>
28
 * @since 2.0
29
 */
30
class AssetBundle extends BaseObject
31
{
32
    /**
33
     * @var string|null the directory that contains the source asset files for this asset bundle.
34
     * A source asset file is a file that is part of your source code repository of your Web application.
35
     *
36
     * You must set this property if the directory containing the source asset files is not Web accessible.
37
     * By setting this property, [[AssetManager]] will publish the source asset files
38
     * to a Web-accessible directory automatically when the asset bundle is registered on a page.
39
     *
40
     * If you do not set this property, it means the source asset files are located under [[basePath]].
41
     *
42
     * You can use either a directory or an alias of the directory.
43
     * @see publishOptions
44
     */
45
    public $sourcePath;
46
    /**
47
     * @var string the Web-accessible directory that contains the asset files in this bundle.
48
     *
49
     * If [[sourcePath]] is set, this property will be *overwritten* by [[AssetManager]]
50
     * when it publishes the asset files from [[sourcePath]].
51
     *
52
     * You can use either a directory or an alias of the directory.
53
     */
54
    public $basePath;
55
    /**
56
     * @var string the base URL for the relative asset files listed in [[js]] and [[css]].
57
     *
58
     * If [[sourcePath]] is set, this property will be *overwritten* by [[AssetManager]]
59
     * when it publishes the asset files from [[sourcePath]].
60
     *
61
     * You can use either a URL or an alias of the URL.
62
     */
63
    public $baseUrl;
64
    /**
65
     * @var array list of bundle class names that this bundle depends on.
66
     *
67
     * For example:
68
     *
69
     * ```php
70
     * public $depends = [
71
     *    'yii\web\YiiAsset',
72
     *    'yii\bootstrap\BootstrapAsset',
73
     * ];
74
     * ```
75
     */
76
    public $depends = [];
77
    /**
78
     * @var array list of JavaScript files that this bundle contains. Each JavaScript file can be
79
     * specified in one of the following formats:
80
     *
81
     * - an absolute URL representing an external asset. For example,
82
     *   `https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js` or
83
     *   `//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js`.
84
     * - a relative path representing a local asset (e.g. `js/main.js`). The actual file path of a local
85
     *   asset can be determined by prefixing [[basePath]] to the relative path, and the actual URL
86
     *   of the asset can be determined by prefixing [[baseUrl]] to the relative path.
87
     * - an array, with the first entry being the URL or relative path as described before, and a list of key => value pairs
88
     *   that will be used to overwrite [[jsOptions]] settings for this entry.
89
     *   This functionality is available since version 2.0.7.
90
     *
91
     * Note that only a forward slash "/" should be used as directory separator.
92
     */
93
    public $js = [];
94
    /**
95
     * @var array list of CSS files that this bundle contains. Each CSS file can be specified
96
     * in one of the three formats as explained in [[js]].
97
     *
98
     * Note that only a forward slash "/" should be used as directory separator.
99
     */
100
    public $css = [];
101
    /**
102
     * @var array the options that will be passed to [[View::registerJsFile()]]
103
     * when registering the JS files in this bundle.
104
     */
105
    public $jsOptions = [];
106
    /**
107
     * @var array the options that will be passed to [[View::registerCssFile()]]
108
     * when registering the CSS files in this bundle.
109
     */
110
    public $cssOptions = [];
111
    /**
112
     * @var array the options to be passed to [[AssetManager::publish()]] when the asset bundle
113
     * is being published. This property is used only when [[sourcePath]] is set.
114
     */
115
    public $publishOptions = [];
116
117
118
    /**
119
     * Registers this asset bundle with a view.
120
     * @param View $view the view to be registered with
121
     * @return static the registered asset bundle instance
122
     */
123 34
    public static function register($view)
124
    {
125 34
        return $view->registerAssetBundle(get_called_class());
126
    }
127
128
    /**
129
     * Initializes the bundle.
130
     * If you override this method, make sure you call the parent implementation in the last.
131
     */
132 42
    public function init()
133
    {
134 42
        if ($this->sourcePath !== null) {
135 16
            $this->sourcePath = rtrim(Yii::getAlias($this->sourcePath), '/\\');
0 ignored issues
show
It seems like Yii::getAlias($this->sourcePath) can also be of type false; however, parameter $string of rtrim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
            $this->sourcePath = rtrim(/** @scrutinizer ignore-type */ Yii::getAlias($this->sourcePath), '/\\');
Loading history...
136
        }
137 42
        if ($this->basePath !== null) {
138 27
            $this->basePath = rtrim(Yii::getAlias($this->basePath), '/\\');
139
        }
140 42
        if ($this->baseUrl !== null) {
141 27
            $this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
142
        }
143
    }
144
145
    /**
146
     * Registers the CSS and JS files with the given view.
147
     * @param \yii\web\View $view the view that the asset files are to be registered with.
148
     */
149 15
    public function registerAssetFiles($view)
150
    {
151 15
        $manager = $view->getAssetManager();
152 15
        foreach ($this->js as $js) {
153 13
            if (is_array($js)) {
154 3
                $file = array_shift($js);
155 3
                $options = ArrayHelper::merge($this->jsOptions, $js);
156 3
                $view->registerJsFile($manager->getAssetUrl($this, $file, ArrayHelper::getValue($options, 'appendTimestamp')), $options);
157 11
            } elseif ($js !== null) {
158 11
                $view->registerJsFile($manager->getAssetUrl($this, $js), $this->jsOptions);
159
            }
160
        }
161 15
        foreach ($this->css as $css) {
162 10
            if (is_array($css)) {
163 3
                $file = array_shift($css);
164 3
                $options = ArrayHelper::merge($this->cssOptions, $css);
165 3
                $view->registerCssFile($manager->getAssetUrl($this, $file, ArrayHelper::getValue($options, 'appendTimestamp')), $options);
166 8
            } elseif ($css !== null) {
167 8
                $view->registerCssFile($manager->getAssetUrl($this, $css), $this->cssOptions);
168
            }
169
        }
170
    }
171
172
    /**
173
     * Publishes the asset bundle if its source code is not under Web-accessible directory.
174
     * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding
175
     * CSS or JS files using [[AssetManager::converter|asset converter]].
176
     * @param AssetManager $am the asset manager to perform the asset publishing
177
     */
178 39
    public function publish($am)
179
    {
180 39
        if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
181 13
            list($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
182
        }
183
184 38
        if (isset($this->basePath, $this->baseUrl) && ($converter = $am->getConverter()) !== null) {
185 33
            foreach ($this->js as $i => $js) {
186 32
                if (is_array($js)) {
187 1
                    $file = array_shift($js);
188 1
                    if (Url::isRelative($file)) {
189 1
                        $js = ArrayHelper::merge($this->jsOptions, $js);
190 1
                        array_unshift($js, $converter->convert($file, $this->basePath));
191 1
                        $this->js[$i] = $js;
192
                    }
193 32
                } elseif (Url::isRelative($js)) {
194 32
                    $this->js[$i] = $converter->convert($js, $this->basePath);
195
                }
196
            }
197 33
            foreach ($this->css as $i => $css) {
198 23
                if (is_array($css)) {
199 1
                    $file = array_shift($css);
200 1
                    if (Url::isRelative($file)) {
201 1
                        $css = ArrayHelper::merge($this->cssOptions, $css);
202 1
                        array_unshift($css, $converter->convert($file, $this->basePath));
203 1
                        $this->css[$i] = $css;
204
                    }
205 23
                } elseif (Url::isRelative($css)) {
206 23
                    $this->css[$i] = $converter->convert($css, $this->basePath);
207
                }
208
            }
209
        }
210
    }
211
}
212