| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- namespace yiins\web;
- use yiins\Yii;
- /**
- * Class AssetBundle
- * @package base\web
- *
- */
- class AssetBundle extends \CApplicationComponent
- {
- /**
- * @var AssetBundle[]
- */
- protected static $registeredBundles = [];
- /**
- * @var string the directory that contains the source asset assets 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 assets is not Web accessible.
- * By setting this property, [[AssetManager]] will publish the source asset assets
- * to a Web-accessible directory automatically when the asset bundle is registered on a page.
- */
- public $sourcePath;
- /**
- * @var array list of bundle class names that this bundle depends on.
- *
- * For example:
- *
- * ```php
- * public $depends = [
- * 'base\assets\JQueryAssets',
- * '\base\bootstrap\BootstrapAssets',
- * ];
- * ```
- */
- public $depends = [];
- /**
- * @var array list of JavaScript assets that this bundle contains. Each JavaScript file can be
- * specified in one of the following formats:
- *
- * - an absolute URL representing an external asset. For example,
- * `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.
- * - an array, with the first entry being the URL or relative path as described before, and a list of key => value pairs
- * that will be used to overwrite [[jsOptions]] settings for this entry.
- * This functionality is available since version 2.0.7.
- *
- * Note that only a forward slash "/" should be used as directory separator.
- */
- public $js = [];
- /**
- * @var array list of CSS assets that this bundle contains. Each CSS file can be specified
- * in one of the three formats as explained in [[js]].
- *
- * Note that only a forward slash "/" should be used as directory separator.
- */
- public $css = [];
- /**
- * @var \CAssetManager
- */
- protected $assetManager = null;
- /**
- * @var string the base URL for the relative asset assets listed in [[js]] and [[css]].
- */
- protected $baseUrl;
- private $isRegistered = false;
- public function __construct()
- {
- $this->init();
- }
- /**
- * Initializes the bundle.
- * If you override this method, make sure you call the parent implementation in the last.
- */
- public function init()
- {
- if ($this->sourcePath !== null) {
- $sourcePath = trim($this->sourcePath);
- $path = Yii::getPathOfAlias(str_replace(['\\', '/'], '.', trim($sourcePath, '/\\')));
- if (!$path) {
- $path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, rtrim($sourcePath, '/\\'));
- }
- $this->sourcePath = $path;
- }
- if ($this->baseUrl !== null) {
- $this->baseUrl = rtrim($this->baseUrl, '/');
- }
- parent::init();
- }
- /**
- * Get asset URL
- * @param $asset
- * @return string URL of the asset
- */
- public static function getAsset($asset)
- {
- if (!isset(self::$registeredBundles[static::class])) {
- self::$registeredBundles[static::class] = new static();
- self::$registeredBundles[static::class]->publish();
- }
- return self::$registeredBundles[static::class]->getAssetUrl($asset);
- }
- protected function publish()
- {
- if ($this->sourcePath) {
- $this->baseUrl = $this->getAssetManager()->publish($this->sourcePath);
- }
- }
- public function getAssetManager()
- {
- return Yii::web()->getAssetManager();
- }
- /**
- * @param $asset
- * @return string
- */
- protected function getAssetUrl($asset)
- {
- if (strncmp($asset, 'http', 4) === 0 || strncmp($asset, '//', 2) === 0) {
- return $asset;
- } else {
- return $this->baseUrl . '/' . trim($asset);
- }
- }
- public static function getUrl()
- {
- if (!isset(self::$registeredBundles[static::class])) {
- self::$registeredBundles[static::class] = new static();
- self::$registeredBundles[static::class]->publish();
- }
- return self::$registeredBundles[static::class]->baseUrl;
- }
- public static function register($scriptFilesPosition = false, $defaultRegisterDependencies = true)
- {
- if (isset(self::$registeredBundles[static::class]) && self::$registeredBundles[static::class]->isRegistered) {
- return true;
- }
- $instance = new static;
- return $instance->registerBundle($scriptFilesPosition, $defaultRegisterDependencies);
- }
- public function registerBundle($scriptFilesPosition, $defaultRegisterDependencies = true)
- {
- if ($defaultRegisterDependencies) {
- $this->resolveDependencies($scriptFilesPosition);
- }
- if ($scriptFilesPosition === false) {
- $scriptFilesPosition = Yii::web()->clientScript->defaultScriptFilePosition;
- }
- $this->publish();
- foreach ($this->css as $key => $value) {
- $file = $value;
- $media = '';
- if (is_string($key)) {
- $file = $key;
- $media = $value;
- }
- Yii::web()->clientScript
- ->registerCssFile($this->getAssetUrl($file), $media);
- }
- foreach ($this->js as $key => $value) {
- $file = $value;
- $params = [];
- if (is_array($value)) {
- $file = $value[0];
- unset($value[0]);
- $params = $value;
- $position = $scriptFilesPosition;
- if (isset($params['position'])) {
- $position = $params['position'];
- }
- $this->registerScriptFile($file, $position, $params);
- } elseif (is_string($key)) {
- $this->registerScriptFile($key, $value, $params);
- } else {
- $this->registerScriptFile($file, $scriptFilesPosition, $params);
- }
- }
- self::$registeredBundles[static::class] = $this;
- self::$registeredBundles[static::class]->isRegistered = true;
- return true;
- }
- protected function resolveDependencies($scriptFilesPosition)
- {
- $defaultScriptFilePosition = $scriptFilesPosition;
- $defaultRegisterDependencies = true;
- foreach ($this->depends as $key => $value) {
- $bundle = $value;
- $registerDependencies = $defaultRegisterDependencies;
- if (is_string($key)) {
- $bundle = $key;
- if (is_array($value)) {
- $scriptFilesPosition = $value[0];
- $registerDependencies = $value[1];
- } else {
- $scriptFilesPosition = $value;
- }
- }
- $bundle::register($scriptFilesPosition, $registerDependencies);
- $scriptFilesPosition = $defaultScriptFilePosition;
- }
- }
- private function registerScriptFile($file, $position, $params)
- {
- if (!Yii::web()->clientScript->isScriptFileRegistered($this->getAssetUrl($file), $position)) {
- Yii::web()->clientScript
- ->registerScriptFile($this->getAssetUrl($file), $position, $params);
- }
- }
- }
|