0 follower

Final Class Yiisoft\Assets\AssetManager

InheritanceYiisoft\Assets\AssetManager

AssetManager manages asset bundle configuration and loading.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Assets\AssetManager
checkAllowedBundleName() Checks whether asset bundle are allowed by name {@see $allowedBundleNames}. Yiisoft\Assets\AssetManager
export() Exports registered asset bundles. Yiisoft\Assets\AssetManager
getAssetUrl() Returns the actual URL for the specified asset. Yiisoft\Assets\AssetManager
getBundle() Returns a cloned named asset bundle. Yiisoft\Assets\AssetManager
getCssFiles() Yiisoft\Assets\AssetManager
getCssStrings() Yiisoft\Assets\AssetManager
getJsFiles() Yiisoft\Assets\AssetManager
getJsStrings() Yiisoft\Assets\AssetManager
getJsVars() Yiisoft\Assets\AssetManager
getUrl() Returns the actual URL for the specified asset. Yiisoft\Assets\AssetManager
isRegisteredBundle() Returns whether the asset bundle is registered. Yiisoft\Assets\AssetManager
register() Registers asset bundle by name. Yiisoft\Assets\AssetManager
registerAllAllowed() Registers all allowed asset bundles. Yiisoft\Assets\AssetManager
registerCustomized() Registers an asset bundle by name with custom configuration. Yiisoft\Assets\AssetManager
registerMany() Registers many asset bundles by names. Yiisoft\Assets\AssetManager
withConverter() Returns a new instance with the specified converter. Yiisoft\Assets\AssetManager
withLoader() Returns a new instance with the specified loader. Yiisoft\Assets\AssetManager
withPublisher() Returns a new instance with the specified publisher. Yiisoft\Assets\AssetManager

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\Aliases\Aliases $aliases, Yiisoft\Assets\AssetLoaderInterface $loader, string[] $allowedBundleNames = [], array $customizedBundles = [] )
$aliases \Yiisoft\Aliases\Aliases

The aliases instance.

$loader Yiisoft\Assets\AssetLoaderInterface

The loader instance.

$allowedBundleNames string[]

List of names of allowed asset bundles. If the array is empty, then any asset bundles are allowed. If the names of allowed asset bundles were specified, only these asset bundles or their dependencies can be registered {@see \Yiisoft\Assets\register()} and obtained {@see \Yiisoft\Assets\getBundle()}. Also, specifying names allows to export {@see \Yiisoft\Assets\export()} asset bundles automatically without first registering them manually.

$customizedBundles array

The asset bundle configurations. Provided to customize asset bundles. When a bundle is being loaded by {@see \Yiisoft\Assets\getBundle()}, if it has a corresponding configuration specified here, the configuration will be applied to the bundle. The array keys are the asset class bundle names (without leading backslash). If a value is false, it means the corresponding asset bundle is disabled and {@see \Yiisoft\Assets\getBundle()} should return an instance of the specified asset bundle with empty property values.

                public function __construct(
    Aliases $aliases,
    private AssetLoaderInterface $loader,
    private readonly array $allowedBundleNames = [],
    private array $customizedBundles = []
) {
    $this->registrar = new AssetRegistrar($aliases, $this->loader);
}

            
checkAllowedBundleName() public method

Checks whether asset bundle are allowed by name {@see $allowedBundleNames}.

public void checkAllowedBundleName ( string $name )
$name string

The asset bundle name to check.

throws Yiisoft\Assets\Exception\InvalidConfigException

For invalid asset bundle configuration.

throws RuntimeException

If The asset bundle name is not allowed.

                public function checkAllowedBundleName(string $name): void
{
    if (isset($this->loadedBundles[$name]) || in_array($name, $this->allowedBundleNames, true)) {
        return;
    }
    foreach ($this->allowedBundleNames as $bundleName) {
        if ($this->isAllowedBundleDependencies($name, $this->loadBundle($bundleName))) {
            return;
        }
    }
    throw new RuntimeException("The \"{$name}\" asset bundle is not allowed.");
}

            
export() public method

Exports registered asset bundles.

When using the allowed asset bundles, the export result will always be the same, since the asset bundles are registered before the export. If do not use the allowed asset bundles mode, must register {@see \Yiisoft\Assets\register()} all the required asset bundles before exporting.

public void export ( Yiisoft\Assets\AssetExporterInterface $exporter )
$exporter Yiisoft\Assets\AssetExporterInterface

The exporter instance.

throws Yiisoft\Assets\Exception\InvalidConfigException

If an error occurs during registration when using allowed asset bundles.

throws RuntimeException

If no asset bundles were registered or an error occurred during the export.

                public function export(AssetExporterInterface $exporter): void
{
    if (!empty($this->allowedBundleNames)) {
        $this->registerAllAllowed();
    }
    if (empty($this->registeredBundles)) {
        throw new RuntimeException('Not a single asset bundle was registered.');
    }
    $exporter->export($this->registeredBundles);
}

            
getAssetUrl() public method
Deprecated Use {@see \Yiisoft\Assets\getUrl()} instead.

Returns the actual URL for the specified asset.

public string getAssetUrl ( string $name, string $path )
$name string

The asset bundle name.

$path string

The asset path.

return string

The actual URL for the specified asset.

throws Yiisoft\Assets\Exception\InvalidConfigException

If asset files are not found.

                public function getAssetUrl(string $name, string $path): string
{
    return $this->getUrl($name, $path);
}

            
getBundle() public method

Returns a cloned named asset bundle.

This method will first look for the bundle in {@see $customizedBundles}. If not found, it will treat $name as the class of the asset bundle and create a new instance of it. If $name is not a class name, an {@see \Yiisoft\Assets\AssetBundle} instance will be created.

Cloning is used to prevent an asset bundle instance from being modified in a non-context of the asset manager.

public Yiisoft\Assets\AssetBundle getBundle ( string $name )
$name string

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

return Yiisoft\Assets\AssetBundle

The asset bundle instance.

throws Yiisoft\Assets\Exception\InvalidConfigException

For invalid asset bundle configuration.

                public function getBundle(string $name): AssetBundle
{
    if (!empty($this->allowedBundleNames)) {
        $this->checkAllowedBundleName($name);
    }
    $bundle = $this->loadBundle($name);
    $bundle = $this->publishBundle($bundle);
    return clone $bundle;
}

            
getCssFiles() public method

public array getCssFiles ( )
return array

Config array of CSS files.

                public function getCssFiles(): array
{
    return $this->registrar->getCssFiles();
}

            
getCssStrings() public method

public array getCssStrings ( )
return array

CSS blocks.

                public function getCssStrings(): array
{
    return $this->registrar->getCssStrings();
}

            
getJsFiles() public method

public array getJsFiles ( )
return array

Config array of JavaScript files.

                public function getJsFiles(): array
{
    return $this->registrar->getJsFiles();
}

            
getJsStrings() public method

public array getJsStrings ( )
return array

JavaScript code blocks.

                public function getJsStrings(): array
{
    return $this->registrar->getJsStrings();
}

            
getJsVars() public method

public array getJsVars ( )
return array

JavaScript variables.

                public function getJsVars(): array
{
    return $this->registrar->getJsVars();
}

            
getUrl() public method

Returns the actual URL for the specified asset.

public string getUrl ( string $name, string $path )
$name string

The asset bundle name.

$path string

The asset path.

return string

The actual URL for the specified asset.

throws Yiisoft\Assets\Exception\InvalidConfigException

If asset files are not found.

                public function getUrl(string $name, string $path): string
{
    return $this->loader->getAssetUrl($this->getBundle($name), $path);
}

            
isRegisteredBundle() public method

Returns whether the asset bundle is registered.

public boolean isRegisteredBundle ( string $name )
$name string

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

return boolean

Whether the asset bundle is registered.

                public function isRegisteredBundle(string $name): bool
{
    return isset($this->registeredBundles[$name]);
}

            
register() public method

Registers asset bundle by name.

public void register ( string $name, integer|null $jsPosition null, integer|null $cssPosition null )
$name string

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

$jsPosition integer|null

{@see \Yiisoft\Assets\AssetBundle::$jsPosition}

$cssPosition integer|null

{@see \Yiisoft\Assets\AssetBundle::$cssPosition}

throws Yiisoft\Assets\Exception\InvalidConfigException
throws RuntimeException

                public function register(string $name, ?int $jsPosition = null, ?int $cssPosition = null): void
{
    if (!empty($this->allowedBundleNames)) {
        $this->checkAllowedBundleName($name);
    }
    $this->registerAssetBundle($name, $jsPosition, $cssPosition);
    $this->registerFiles($name);
}

            
registerAllAllowed() public method

Registers all allowed asset bundles.

public void registerAllAllowed ( )
throws Yiisoft\Assets\Exception\InvalidConfigException
throws RuntimeException

                public function registerAllAllowed(): void
{
    if (empty($this->allowedBundleNames)) {
        throw new RuntimeException('The allowed names of the asset bundles were not set.');
    }
    foreach ($this->allowedBundleNames as $name) {
        $this->registerAssetBundle($name);
        $this->registerFiles($name);
    }
}

            
registerCustomized() public method

Registers an asset bundle by name with custom configuration.

This method is similar to {@see \Yiisoft\Assets\register()}, except that it allows you to customize the asset bundle configuration before it is registered. It also supports registering asset bundles with virtual namespaces, which means that the corresponding asset file may not physically exist.

public void registerCustomized ( string $bundleName, array $bundleConfig )
$bundleName string

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

$bundleConfig array

The customized asset bundle configuration.

                public function registerCustomized(string $bundleName, array $bundleConfig): void
{
    $this->customizedBundles[$bundleName] = $bundleConfig;
    $this->register($bundleName);
}

            
registerMany() public method

Registers many asset bundles by names.

public void registerMany ( string[] $names, integer|null $jsPosition null, integer|null $cssPosition null )
$names string[]

The many class names of the asset bundles (without the leading backslash).

$jsPosition integer|null

{@see \Yiisoft\Assets\AssetBundle::$jsPosition}

$cssPosition integer|null

{@see \Yiisoft\Assets\AssetBundle::$cssPosition}

throws Yiisoft\Assets\Exception\InvalidConfigException
throws RuntimeException

                public function registerMany(array $names, ?int $jsPosition = null, ?int $cssPosition = null): void
{
    foreach ($names as $name) {
        $this->register($name, $jsPosition, $cssPosition);
    }
}

            
withConverter() public method

Returns a new instance with the specified converter.

public self withConverter ( Yiisoft\Assets\AssetConverterInterface $converter )
$converter Yiisoft\Assets\AssetConverterInterface

                public function withConverter(AssetConverterInterface $converter): self
{
    $new = clone $this;
    $new->registrar = $new->registrar->withConverter($converter);
    return $new;
}

            
withLoader() public method

Returns a new instance with the specified loader.

public self withLoader ( Yiisoft\Assets\AssetLoaderInterface $loader )
$loader Yiisoft\Assets\AssetLoaderInterface

                public function withLoader(AssetLoaderInterface $loader): self
{
    $new = clone $this;
    $new->loader = $loader;
    $new->registrar = $new->registrar->withLoader($new->loader);
    return $new;
}

            
withPublisher() public method

Returns a new instance with the specified publisher.

public self withPublisher ( Yiisoft\Assets\AssetPublisherInterface $publisher )
$publisher Yiisoft\Assets\AssetPublisherInterface

                public function withPublisher(AssetPublisherInterface $publisher): self
{
    $new = clone $this;
    $new->publisher = $publisher;
    return $new;
}