|
|
@ -10,7 +10,6 @@ namespace yii\web; |
|
|
|
use Yii; |
|
|
|
use Yii; |
|
|
|
use yii\base\Component; |
|
|
|
use yii\base\Component; |
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
use yii\helpers\Url; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* AssetManager manages asset bundle configuration and loading. |
|
|
|
* AssetManager manages asset bundle configuration and loading. |
|
|
@ -84,6 +83,8 @@ class AssetManager extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $assetMap = []; |
|
|
|
public $assetMap = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private $_dummyBundles = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Initializes the component. |
|
|
|
* Initializes the component. |
|
|
@ -114,7 +115,7 @@ class AssetManager extends Component |
|
|
|
public function getBundle($name) |
|
|
|
public function getBundle($name) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($this->bundles === false) { |
|
|
|
if ($this->bundles === false) { |
|
|
|
return null; |
|
|
|
return $this->loadDummyBundle($name); |
|
|
|
} elseif (!isset($this->bundles[$name])) { |
|
|
|
} elseif (!isset($this->bundles[$name])) { |
|
|
|
return $this->bundles[$name] = $this->loadBundle($name); |
|
|
|
return $this->bundles[$name] = $this->loadBundle($name); |
|
|
|
} elseif ($this->bundles[$name] instanceof AssetBundle) { |
|
|
|
} elseif ($this->bundles[$name] instanceof AssetBundle) { |
|
|
@ -122,7 +123,7 @@ class AssetManager extends Component |
|
|
|
} elseif (is_array($this->bundles[$name])) { |
|
|
|
} elseif (is_array($this->bundles[$name])) { |
|
|
|
return $this->bundles[$name] = $this->loadBundle($name, $this->bundles[$name]); |
|
|
|
return $this->bundles[$name] = $this->loadBundle($name, $this->bundles[$name]); |
|
|
|
} elseif ($this->bundles[$name] === false) { |
|
|
|
} elseif ($this->bundles[$name] === false) { |
|
|
|
return null; |
|
|
|
return $this->loadDummyBundle($name); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
throw new InvalidConfigException("Invalid asset bundle configuration: $name"); |
|
|
|
throw new InvalidConfigException("Invalid asset bundle configuration: $name"); |
|
|
|
} |
|
|
|
} |
|
|
@ -143,49 +144,42 @@ class AssetManager extends Component |
|
|
|
return $bundle; |
|
|
|
return $bundle; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
protected function loadDummyBundle($name) |
|
|
|
* @param View $view |
|
|
|
|
|
|
|
* @param AssetBundle $bundle |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function registerAssetFiles($view, $bundle) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
foreach ($bundle->js as $js) { |
|
|
|
if (!isset($this->_dummyBundles[$name])) { |
|
|
|
$view->registerJsFile($this->getAssetUrl($bundle, $js), $bundle->jsOptions); |
|
|
|
$this->_dummyBundles[$name] = $this->loadBundle($name, [ |
|
|
|
} |
|
|
|
'js' => [], |
|
|
|
foreach ($bundle->css as $css) { |
|
|
|
'css' => [], |
|
|
|
$view->registerCssFile($this->getAssetUrl($bundle, $css), $bundle->cssOptions); |
|
|
|
'depends' => [], |
|
|
|
|
|
|
|
]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return $this->_dummyBundles[$name]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected function getAssetUrl($bundle, $file) |
|
|
|
public function resolveAsset($asset) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (($mappedFile = $this->mapAsset($file)) !== false) { |
|
|
|
if (isset($this->assetMap[$asset])) { |
|
|
|
return Url::isRelative($mappedFile) ? $this->baseUrl . '/' . $mappedFile : $mappedFile; |
|
|
|
return $this->assetMap[$asset]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (strncmp($file, '@/', 2) === 0) { |
|
|
|
$n = strlen($asset); |
|
|
|
$file = $this->baseUrl . substr($file, 1); |
|
|
|
|
|
|
|
} elseif (Url::isRelative($file)) { |
|
|
|
|
|
|
|
$file = $bundle->baseUrl . '/' . $file; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $file; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function mapAsset($file) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (isset($this->assetMap[$file])) { |
|
|
|
|
|
|
|
return $this->assetMap[$file]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$n = strlen($file); |
|
|
|
|
|
|
|
foreach ($this->assetMap as $from => $to) { |
|
|
|
foreach ($this->assetMap as $from => $to) { |
|
|
|
$n2 = strlen($from); |
|
|
|
$n2 = strlen($from); |
|
|
|
if ($n2 <= $n && substr_compare($file, $from, $n - $n2, $n2) === 0) { |
|
|
|
if ($n2 <= $n && substr_compare($asset, $from, $n - $n2, $n2) === 0) { |
|
|
|
return $to; |
|
|
|
return $to; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getAssetUrl($asset) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return $this->baseUrl . '/' . ltrim($asset, '/'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getAssetPath($asset) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return $this->basePath . '/' . ltrim($asset, '/'); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|