AssetBundle.php 6.58 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\web;

use yii\base\Object;
Qiang Xue committed
11
use yii\helpers\Url;
12
use Yii;
Qiang Xue committed
13 14

/**
15 16
 * AssetBundle represents a collection of asset files, such as CSS, JS, images.
 *
17 18 19
 * Each asset bundle has a unique name that globally identifies it among all asset bundles used in an application.
 * The name is the [fully qualified class name](http://php.net/manual/en/language.namespaces.rules.php)
 * of the class representing it.
20 21 22 23
 *
 * An asset bundle can depend on other asset bundles. When registering an asset bundle
 * with a view, all its dependent asset bundles will be automatically registered.
 *
Qiang Xue committed
24 25 26 27 28
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class AssetBundle extends Object
{
29
    /**
30 31 32 33 34 35 36 37 38 39 40 41 42 43
     * @var string the directory that contains the source asset files for this asset bundle.
     * A source asset file is a file that is part of your source code repository of your Web application.
     *
     * You must set this property if the directory containing the source asset files is not Web accessible.
     * By setting this property, [[AssetManager]] will publish the source asset files
     * to a Web-accessible directory automatically when the asset bundle is registered on a page.
     *
     * If you do not set this property, it means the source asset files are located under [[basePath]].
     *
     * You can use either a directory or an alias of the directory.
     */
    public $sourcePath;
    /**
     * @var string the Web-accessible directory that contains the asset files in this bundle.
44
     *
45 46
     * If [[sourcePath]] is set, this property will be *overwritten* by [[AssetManager]]
     * when it publishes the asset files from [[sourcePath]].
47 48 49 50 51
     *
     * You can use either a directory or an alias of the directory.
     */
    public $basePath;
    /**
Qiang Xue committed
52
     * @var string the base URL for the relative asset files listed in [[js]] and [[css]].
53
     *
54 55
     * If [[sourcePath]] is set, this property will be *overwritten* by [[AssetManager]]
     * when it publishes the asset files from [[sourcePath]].
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
     *
     * You can use either a URL or an alias of the URL.
     */
    public $baseUrl;
    /**
     * @var array list of bundle class names that this bundle depends on.
     *
     * For example:
     *
     * ```php
     * public $depends = [
     *    'yii\web\YiiAsset',
     *    'yii\bootstrap\BootstrapAsset',
     * ];
     * ```
     */
    public $depends = [];
    /**
Qiang Xue committed
74 75
     * @var array list of JavaScript files that this bundle contains. Each JavaScript file can be
     * specified in one of the following formats:
76
     *
Qiang Xue committed
77
     * - an absolute URL representing an external asset. For example,
78 79 80 81 82
     *   `http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js` or
     *   `//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js`.
     * - a relative path representing a local asset (e.g. `js/main.js`). The actual file path of a local
     *   asset can be determined by prefixing [[basePath]] to the relative path, and the actual URL
     *   of the asset can be determined by prefixing [[baseUrl]] to the relative path.
Qiang Xue committed
83 84
     *
     * Note that only forward slash "/" should be used as directory separators.
85 86 87
     */
    public $js = [];
    /**
Qiang Xue committed
88 89
     * @var array list of CSS files that this bundle contains. Each CSS file can be specified
     * in one of the three formats as explained in [[js]].
90 91 92 93 94
     *
     * Note that only forward slash "/" can be used as directory separator.
     */
    public $css = [];
    /**
95
     * @var array the options that will be passed to [[View::registerJsFile()]]
96 97 98 99
     * when registering the JS files in this bundle.
     */
    public $jsOptions = [];
    /**
100
     * @var array the options that will be passed to [[View::registerCssFile()]]
101 102 103
     * when registering the CSS files in this bundle.
     */
    public $cssOptions = [];
104 105 106 107 108
    /**
     * @var array the options to be passed to [[AssetManager::publish()]] when the asset bundle
     * is being published. This property is used only when [[sourcePath]] is set.
     */
    public $publishOptions = [];
Qiang Xue committed
109

110

111
    /**
Qiang Xue committed
112 113
     * Registers this asset bundle with a view.
     * @param View $view the view to be registered with
114 115 116 117 118 119
     * @return static the registered asset bundle instance
     */
    public static function register($view)
    {
        return $view->registerAssetBundle(get_called_class());
    }
120

121 122 123 124 125 126
    /**
     * Initializes the bundle.
     * If you override this method, make sure you call the parent implementation in the last.
     */
    public function init()
    {
127 128 129
        if ($this->sourcePath !== null) {
            $this->sourcePath = rtrim(Yii::getAlias($this->sourcePath), '/\\');
        }
130 131 132 133 134 135 136
        if ($this->basePath !== null) {
            $this->basePath = rtrim(Yii::getAlias($this->basePath), '/\\');
        }
        if ($this->baseUrl !== null) {
            $this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
        }
    }
Qiang Xue committed
137 138

    /**
139 140
     * Registers the CSS and JS files with the given view.
     * @param \yii\web\View $view the view that the asset files are to be registered with.
Qiang Xue committed
141 142 143 144 145
     */
    public function registerAssetFiles($view)
    {
        $manager = $view->getAssetManager();
        foreach ($this->js as $js) {
146
            $view->registerJsFile($manager->getAssetUrl($this, $js), $this->jsOptions);
Qiang Xue committed
147 148
        }
        foreach ($this->css as $css) {
149
            $view->registerCssFile($manager->getAssetUrl($this, $css), $this->cssOptions);
Qiang Xue committed
150 151 152 153
        }
    }

    /**
154 155 156 157
     * Publishes the asset bundle if its source code is not under Web-accessible directory.
     * 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
Qiang Xue committed
158
     */
159
    public function publish($am)
Qiang Xue committed
160
    {
161 162
        if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
            list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
Qiang Xue committed
163 164
        }

165 166 167 168 169 170 171 172 173 174 175
        if (isset($this->basePath, $this->baseUrl) && ($converter = $am->getConverter()) !== null) {
            foreach ($this->js as $i => $js) {
                if (Url::isRelative($js)) {
                    $this->js[$i] = $converter->convert($js, $this->basePath);
                }
            }
            foreach ($this->css as $i => $css) {
                if (Url::isRelative($css)) {
                    $this->css[$i] = $converter->convert($css, $this->basePath);
                }
            }
Qiang Xue committed
176 177
        }
    }
Zander Baldwin committed
178
}