0 follower

Final Class Yiisoft\Assets\AssetUtil

InheritanceYiisoft\Assets\AssetUtil

AssetUtil shared functions.

Public Methods

Hide inherited methods

Method Description Defined By
createAsset() Creates a new asset bundle instance. Yiisoft\Assets\AssetUtil
exportToFile() Writes a string representation of asset bundles to the specified file. Yiisoft\Assets\AssetUtil
extractFilePathsForExport() Extracts the file paths to export from each asset bundle {@see AssetBundle::$export}. Yiisoft\Assets\AssetUtil
isRelative() Returns a value indicating whether a URL is relative. Yiisoft\Assets\AssetUtil
resolveAsset() Resolves the actual URL for the specified asset. Yiisoft\Assets\AssetUtil
resolvePathAliases() Resolve path aliases for {@see AssetBundle} properties: Yiisoft\Assets\AssetUtil

Method Details

Hide inherited methods

createAsset() public static method

Creates a new asset bundle instance.

If the name is a class name, an instance of this class will be created, otherwise an instance of the {@see \Yiisoft\Assets\AssetBundle} will be created.

public static Yiisoft\Assets\AssetBundle createAsset ( string $name, array $config = [] )
$name string

The asset bundle name. Usually the asset bundle class name (without leading backslash).

$config array

The asset bundle instance configuration. If specified, it will be applied to the instance.

return Yiisoft\Assets\AssetBundle

The created asset bundle.

                public static function createAsset(string $name, array $config = []): AssetBundle
{
    /** @psalm-suppress UnsafeInstantiation */
    $bundle = is_subclass_of($name, AssetBundle::class) ? new $name() : new AssetBundle();
    foreach ($config as $property => $value) {
        $bundle->{$property} = $value;
    }
    return $bundle;
}

            
exportToFile() public static method

Writes a string representation of asset bundles to the specified file.

public static void exportToFile ( string $targetFile, string $bundles )
$targetFile string

The full path to the target file.

$bundles string

The string representation of asset bundles.

throws RuntimeException

If an error occurred while writing to the file.

                public static function exportToFile(string $targetFile, string $bundles): void
{
    $targetDirectory = dirname($targetFile);
    if (!is_dir($targetDirectory) || !is_writable($targetDirectory)) {
        throw new RuntimeException("Target directory \"{$targetDirectory}\" does not exist or is not writable.");
    }
    if (file_put_contents($targetFile, $bundles, LOCK_EX) === false) {
        throw new RuntimeException("An error occurred while writing to the \"{$targetFile}\" file.");
    }
}

            
extractFilePathsForExport() public static method

Extracts the file paths to export from each asset bundle {@see AssetBundle::$export}.

public static string[] extractFilePathsForExport ( Yiisoft\Assets\AssetBundle[] $bundles )
$bundles Yiisoft\Assets\AssetBundle[]

List of asset bundles.

return string[]

Extracted file paths.

                public static function extractFilePathsForExport(array $bundles): array
{
    $filePaths = [];
    foreach ($bundles as $bundle) {
        if ($bundle->cdn || empty($bundle->sourcePath)) {
            continue;
        }
        if (!empty($bundle->export)) {
            foreach ($bundle->export as $filePath) {
                /** @var string $filePath */
                $filePaths[] = "{$bundle->sourcePath}/{$filePath}";
            }
            continue;
        }
        foreach (array_merge($bundle->css, $bundle->js) as $item) {
            /** @psalm-var CssFile|JsFile|string $item */
            $filePath = is_array($item) ? $item[0] : $item;
            $filePaths[] = "{$bundle->sourcePath}/{$filePath}";
        }
    }
    return array_unique($filePaths);
}

            
isRelative() public static method

Returns a value indicating whether a URL is relative.

A relative URL does not have host info part.

public static boolean isRelative ( string $url )
$url string

The URL to be checked.

return boolean

Whether the URL is relative.

                public static function isRelative(string $url): bool
{
    return strncmp($url, '//', 2) && !str_contains($url, '://');
}

            
resolveAsset() public static method

Resolves the actual URL for the specified asset.

public static string|null resolveAsset ( Yiisoft\Assets\AssetBundle $bundle, string $assetPath, string[] $assetMap )
$bundle Yiisoft\Assets\AssetBundle

The asset bundle which the asset file belongs to.

$assetPath string

The asset path. This should be one of the assets listed in {@see \Yiisoft\Assets\AssetBundle::$js} or {@see \Yiisoft\Assets\AssetBundle::$css}.

$assetMap string[]

Mapping from source asset files (keys) to target asset files (values) {@see \Yiisoft\Assets\AssetPublisher::$assetMap}.

return string|null

The actual URL for the specified asset, or null if there is no mapping.

                public static function resolveAsset(AssetBundle $bundle, string $assetPath, array $assetMap): ?string
{
    if (isset($assetMap[$assetPath])) {
        return $assetMap[$assetPath];
    }
    if (!empty($bundle->sourcePath) && self::isRelative($assetPath)) {
        $assetPath = $bundle->sourcePath . '/' . $assetPath;
    }
    $n = mb_strlen($assetPath, 'utf-8');
    foreach ($assetMap as $from => $to) {
        $n2 = mb_strlen($from, 'utf-8');
        if ($n2 <= $n && substr_compare($assetPath, $from, $n - $n2, $n2) === 0) {
            return $to;
        }
    }
    return null;
}

            
resolvePathAliases() public static method

Resolve path aliases for {@see AssetBundle} properties:

  • {@see \Yiisoft\Assets\AssetBundle::$basePath}
  • {@see \Yiisoft\Assets\AssetBundle::$baseUrl}
  • {@see \Yiisoft\Assets\AssetBundle::$sourcePath}
public static Yiisoft\Assets\AssetBundle resolvePathAliases ( Yiisoft\Assets\AssetBundle $bundle, \Yiisoft\Aliases\Aliases $aliases )
$bundle Yiisoft\Assets\AssetBundle

The asset bundle instance to resolving path aliases.

$aliases \Yiisoft\Aliases\Aliases

The aliases instance to resolving path aliases.

return Yiisoft\Assets\AssetBundle

The asset bundle instance with resolved paths.

                public static function resolvePathAliases(AssetBundle $bundle, Aliases $aliases): AssetBundle
{
    if ($bundle->basePath !== null) {
        $bundle->basePath = $aliases->get($bundle->basePath);
    }
    if ($bundle->baseUrl !== null) {
        $bundle->baseUrl = $aliases->get($bundle->baseUrl);
    }
    if ($bundle->sourcePath !== null) {
        $bundle->sourcePath = $aliases->get($bundle->sourcePath);
    }
    return $bundle;
}