Final Class Yiisoft\Assets\AssetLoader
| Inheritance | Yiisoft\Assets\AssetLoader |
|---|---|
| Implements | Yiisoft\Assets\AssetLoaderInterface |
AssetLoader is responsible for executing the loading of the assets from Yiisoft\Assets\AssetBundle::$basePath to
Yiisoft\Assets\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 __construct( \Yiisoft\Aliases\Aliases $aliases, boolean $appendTimestamp = false, array<string, string> $assetMap = [], string|null $basePath = null, string|null $baseUrl = null ): mixed | ||
| $aliases | \Yiisoft\Aliases\Aliases |
The aliases instance. |
| $appendTimestamp | boolean |
Whether to append a timestamp to the URL of every published asset. See withAppendTimestamp(). |
| $assetMap | array<string, string> |
Mapping from source asset files to target asset files. See withAssetMap(). |
| $basePath | string|null |
The root directory storing the asset files. See withBasePath(). |
| $baseUrl | string|null |
The base URL that can be used to access the asset files. See withBaseUrl(). |
public function __construct(
private readonly Aliases $aliases,
private bool $appendTimestamp = false,
private array $assetMap = [],
private ?string $basePath = null,
private ?string $baseUrl = null,
) {}
| public getAssetUrl( Yiisoft\Assets\AssetBundle $bundle, string $assetPath ): string | ||
| $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 loadBundle( string $name, array $config = [] ): Yiisoft\Assets\AssetBundle | ||
| $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 withAppendTimestamp( boolean $appendTimestamp ): self | ||
| $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 withAssetMap( array<string, string> $assetMap ): self | ||
| $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
Yiisoft\Assets\AssetBundle::$css and 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
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 withBaseUrl() and 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 withBasePath( string|null $basePath ): self | ||
| $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 withBaseUrl( string|null $baseUrl ): self | ||
| $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 withCssDefaultOptions( array $cssDefaultOptions ): self | ||
| $cssDefaultOptions | array |
The options that will be passed to \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 withCssDefaultPosition( integer|null $position ): self | ||
| $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 withJsDefaultOptions( array $jsDefaultOptions ): self | ||
| $jsDefaultOptions | array |
The options that will be passed to \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 withJsDefaultPosition( integer|null $position ): self | ||
| $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.