0 follower

Final Class Yiisoft\Assets\AssetLoader

InheritanceYiisoft\Assets\AssetLoader
ImplementsYiisoft\Assets\AssetLoaderInterface

AssetLoader is responsible for executing the loading of the assets from {@see AssetBundle::$basePath} to {@see AssetBundle::$baseUrl}.

Public Methods

Hide inherited 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

Hide inherited methods

__construct() public method

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

            
getAssetUrl() public method

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

            
loadBundle() public method

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

            
withAppendTimestamp() public method

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 false. When this is true, the URL of a published asset may look like /path/to/asset?v=timestamp, where timestamp is the last modification time of the published asset file. You normally would want to set this property to true when you have enabled HTTP caching for assets, because it allows you to bust caching when the assets are updated.

                public function withAppendTimestamp(bool $appendTimestamp): self
{
    $new = clone $this;
    $new->appendTimestamp = $appendTimestamp;
    return $new;
}

            
withAssetMap() public method

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 my/path/to/jquery.js matches a key jquery.js.

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 jquery.min.js will be replaced with jquery/dist/jquery.js which is relative to {@see \Yiisoft\Assets\withBaseUrl()} and {@see \Yiisoft\Assets\withBasePath()}.

[
    'jquery.min.js' => 'jquery/dist/jquery.js',
]

                public function withAssetMap(array $assetMap): self
{
    $new = clone $this;
    $new->assetMap = $assetMap;
    return $new;
}

            
withBasePath() public method

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 null.

                public function withBasePath(?string $basePath): self
{
    $new = clone $this;
    $new->basePath = $basePath;
    return $new;
}

            
withBaseUrl() public method

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 null.

                public function withBaseUrl(?string $baseUrl): self
{
    $new = clone $this;
    $new->baseUrl = $baseUrl;
    return $new;
}

            
withCssDefaultOptions() public method

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

            
withCssDefaultPosition() public method
public self withCssDefaultPosition ( integer|null $position )
$position integer|null

Specifies where the <style> tag should be inserted in a page.

                public function withCssDefaultPosition(?int $position): self
{
    $new = clone $this;
    $new->cssDefaultPosition = $position;
    return $new;
}

            
withJsDefaultOptions() public method

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

            
withJsDefaultPosition() public method
public self withJsDefaultPosition ( integer|null $position )
$position integer|null

Specifies where the <script> tag should be inserted in a page.

                public function withJsDefaultPosition(?int $position): self
{
    $new = clone $this;
    $new->jsDefaultPosition = $position;
    return $new;
}