Final Class Yiisoft\Assets\AssetLoader
| Inheritance | Yiisoft\Assets\AssetLoader |
|---|---|
| Implements | Yiisoft\Assets\AssetLoaderInterface |
AssetLoader is responsible for executing the loading of the assets from {@see AssetBundle::$basePath} to
{@see AssetBundle::$baseUrl}.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Assets\AssetLoader | |
| getAssetUrl() | Yiisoft\Assets\AssetLoader | |
| loadBundle() | Yiisoft\Assets\AssetLoader | |
| withAppendTimestamp() | Returns a new instance with the specified append timestamp. | Yiisoft\Assets\AssetLoader |
| withAssetMap() | Returns a new instance with the specified asset map. | Yiisoft\Assets\AssetLoader |
| withBasePath() | Returns a new instance with the specified base path. | Yiisoft\Assets\AssetLoader |
| withBaseUrl() | Returns a new instance with the specified base URL. | Yiisoft\Assets\AssetLoader |
| withCssDefaultOptions() | Returns a new instance with the specified global $css default options for all assets bundle. |
Yiisoft\Assets\AssetLoader |
| withCssDefaultPosition() | Yiisoft\Assets\AssetLoader | |
| withJsDefaultOptions() | Returns a new instance with the specified global $js default options for all assets bundle. |
Yiisoft\Assets\AssetLoader |
| withJsDefaultPosition() | Yiisoft\Assets\AssetLoader |
Method Details
| public mixed __construct ( \Yiisoft\Aliases\Aliases $aliases, boolean $appendTimestamp = false, array<string, string> $assetMap = [], string|null $basePath = null, string|null $baseUrl = null ) | ||
| $aliases | \Yiisoft\Aliases\Aliases |
The aliases instance. |
| $appendTimestamp | boolean |
Whether to append a timestamp to the URL of every published asset. See {@see \Yiisoft\Assets\withAppendTimestamp()}. |
| $assetMap | array<string, string> |
Mapping from source asset files to target asset files. See {@see \Yiisoft\Assets\withAssetMap()}. |
| $basePath | string|null |
The root directory storing the asset files. See {@see \Yiisoft\Assets\withBasePath()}. |
| $baseUrl | string|null |
The base URL that can be used to access the asset files. See {@see \Yiisoft\Assets\withBaseUrl()}. |
public function __construct(
private readonly Aliases $aliases,
private bool $appendTimestamp = false,
private array $assetMap = [],
private ?string $basePath = null,
private ?string $baseUrl = null
) {
}
| public string getAssetUrl ( Yiisoft\Assets\AssetBundle $bundle, string $assetPath ) | ||
| $bundle | Yiisoft\Assets\AssetBundle | |
| $assetPath | string | |
public function getAssetUrl(AssetBundle $bundle, string $assetPath): string
{
if (!$bundle->cdn && empty($this->basePath) && empty($bundle->basePath)) {
throw new InvalidConfigException(
'basePath must be set in AssetLoader->withBasePath($path) or ' .
'AssetBundle property public ?string $basePath = $path'
);
}
if (!$bundle->cdn && $this->baseUrl === null && $bundle->baseUrl === null) {
throw new InvalidConfigException(
'baseUrl must be set in AssetLoader->withBaseUrl($path) or ' .
'AssetBundle property public ?string $baseUrl = $path'
);
}
$asset = AssetUtil::resolveAsset($bundle, $assetPath, $this->assetMap);
if (!empty($asset)) {
$assetPath = $asset;
}
if ($bundle->cdn) {
return $bundle->baseUrl === null
? $assetPath
: $bundle->baseUrl . '/' . $assetPath;
}
if (!AssetUtil::isRelative($assetPath) || str_starts_with($assetPath, '/')) {
return $assetPath;
}
$path = "{$this->getBundleBasePath($bundle)}/{$assetPath}";
$url = "{$this->getBundleBaseUrl($bundle)}/{$assetPath}";
if (!is_file($path)) {
throw new InvalidConfigException("Asset files not found: \"{$path}\".");
}
if ($this->appendTimestamp && ($timestamp = FileHelper::lastModifiedTime($path)) > 0) {
return "{$url}?v={$timestamp}";
}
return $url;
}
| public Yiisoft\Assets\AssetBundle loadBundle ( string $name, array $config = [] ) | ||
| $name | string | |
| $config | array | |
public function loadBundle(string $name, array $config = []): AssetBundle
{
$bundle = AssetUtil::createAsset($name, $config);
$bundle->basePath = $this->getBundleBasePath($bundle);
$bundle->baseUrl = $this->getBundleBaseUrl($bundle);
$bundle->sourcePath = $bundle->sourcePath === null ? null : $this->aliases->get($bundle->sourcePath);
$bundle->cssOptions = array_merge($bundle->cssOptions, $this->cssDefaultOptions);
$bundle->cssPosition ??= $this->cssDefaultPosition;
$bundle->jsOptions = array_merge($bundle->jsOptions, $this->jsDefaultOptions);
$bundle->jsPosition ??= $this->jsDefaultPosition;
return $bundle;
}
Returns a new instance with the specified append timestamp.
| public self withAppendTimestamp ( boolean $appendTimestamp ) | ||
| $appendTimestamp | boolean |
Whether to append a timestamp to the URL of every published asset. Default is |
public function withAppendTimestamp(bool $appendTimestamp): self
{
$new = clone $this;
$new->appendTimestamp = $appendTimestamp;
return $new;
}
Returns a new instance with the specified asset map.
| public self withAssetMap ( array<string, string> $assetMap ) | ||
| $assetMap | array<string, string> |
Mapping from source asset files (keys) to target asset files (values). Default is empty array. This property is provided to support fixing incorrect asset file paths in some
asset bundles. When an asset bundle is registered with a view, each relative asset file in its
{@see \Yiisoft\Assets\AssetBundle::$css} and {@see \Yiisoft\Assets\AssetBundle::$js} arrays will be examined against this map.
If any of the keys is found to be the last part of an asset file (which is prefixed with
{@see \Yiisoft\Assets\AssetBundle::$sourcePath} if available), the corresponding value will replace the asset
and be registered with the view. For example, an asset file Note that the target asset files should be absolute URLs, domain relative URLs (starting from '/') or paths relative to {@see \Yiisoft\Assets\withBaseUrl()} and {@see \Yiisoft\Assets\withBasePath()}. In the following example, any assets ending with
|
public function withAssetMap(array $assetMap): self
{
$new = clone $this;
$new->assetMap = $assetMap;
return $new;
}
Returns a new instance with the specified base path.
| public self withBasePath ( string|null $basePath ) | ||
| $basePath | string|null |
The root directory storing the asset files. Default is |
public function withBasePath(?string $basePath): self
{
$new = clone $this;
$new->basePath = $basePath;
return $new;
}
Returns a new instance with the specified base URL.
| public self withBaseUrl ( string|null $baseUrl ) | ||
| $baseUrl | string|null |
The base URL that can be used to access the asset files. Default is |
public function withBaseUrl(?string $baseUrl): self
{
$new = clone $this;
$new->baseUrl = $baseUrl;
return $new;
}
Returns a new instance with the specified global $css default options for all assets bundle.
| public self withCssDefaultOptions ( array $cssDefaultOptions ) | ||
| $cssDefaultOptions | array |
The options that will be passed to {@see \Yiisoft\View\WebView::registerCssFile()} when registering the CSS files all assets bundle. |
public function withCssDefaultOptions(array $cssDefaultOptions): self
{
$new = clone $this;
$new->cssDefaultOptions = $cssDefaultOptions;
return $new;
}
See also Yiisoft\Assets\AssetBundle::$cssPosition.
| public self withCssDefaultPosition ( integer|null $position ) | ||
| $position | integer|null |
Specifies where the |
public function withCssDefaultPosition(?int $position): self
{
$new = clone $this;
$new->cssDefaultPosition = $position;
return $new;
}
Returns a new instance with the specified global $js default options for all assets bundle.
| public self withJsDefaultOptions ( array $jsDefaultOptions ) | ||
| $jsDefaultOptions | array |
The options that will be passed to {@see \Yiisoft\View\WebView::registerJsFile()} when registering the JS files all assets bundle. |
public function withJsDefaultOptions(array $jsDefaultOptions): self
{
$new = clone $this;
$new->jsDefaultOptions = $jsDefaultOptions;
return $new;
}
See also Yiisoft\Assets\AssetBundle::$jsPosition.
| public self withJsDefaultPosition ( integer|null $position ) | ||
| $position | integer|null |
Specifies where the |
public function withJsDefaultPosition(?int $position): self
{
$new = clone $this;
$new->jsDefaultPosition = $position;
return $new;
}
Signup or Login in order to comment.