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); } } }