Final Class Yiisoft\Assets\AssetManager
| Inheritance | Yiisoft\Assets\AssetManager |
|---|
AssetManager manages asset bundle configuration and loading.
Psalm Types
| Name | Value |
|---|---|
| CssFile | array{0: string}|array{0: string, 1: integer} |
| CssString | array{0: mixed}|array{0: string, 1: integer} |
| JsFile | array{0: string}|array{0: string, 1: integer} |
| JsString | array{0: mixed}|array{0: string, 1: integer} |
| JsVar | array{0: string, 1: mixed, 2?: integer} |
| CustomizedBundles | array<string, Yiisoft\Assets\AssetBundle|array<string, mixed>|false> |
Public Methods
Method Details
| public __construct( \Yiisoft\Aliases\Aliases $aliases, Yiisoft\Assets\AssetLoaderInterface $loader, string[] $allowedBundleNames = [], array $customizedBundles = [] ): mixed | ||
| $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 register() and obtained getBundle(). Also, specifying names allows to export 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 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 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);
}
Checks whether asset bundle are allowed by name $allowedBundleNames.
| public checkAllowedBundleName( string $name ): void | ||
| $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.");
}
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 register() all the required asset bundles before exporting.
| public export( Yiisoft\Assets\AssetExporterInterface $exporter ): void | ||
| $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);
}
Returns the actual URL for the specified asset.
| public getAssetUrl( string $name, string $path ): string | ||
| $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);
}
Returns a cloned named asset bundle.
This method will first look for the bundle in $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 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 getBundle( string $name ): Yiisoft\Assets\AssetBundle | ||
| $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;
}
| public getCssFiles( ): array | ||
| return | array |
Config array of CSS files. |
|---|---|---|
public function getCssFiles(): array
{
return $this->registrar->getCssFiles();
}
| public getCssStrings( ): array | ||
| return | array |
CSS blocks. |
|---|---|---|
public function getCssStrings(): array
{
return $this->registrar->getCssStrings();
}
| public getJsFiles( ): array | ||
| return | array |
Config array of JavaScript files. |
|---|---|---|
public function getJsFiles(): array
{
return $this->registrar->getJsFiles();
}
| public getJsStrings( ): array | ||
| return | array |
JavaScript code blocks. |
|---|---|---|
public function getJsStrings(): array
{
return $this->registrar->getJsStrings();
}
| public getJsVars( ): array | ||
| return | array |
JavaScript variables. |
|---|---|---|
public function getJsVars(): array
{
return $this->registrar->getJsVars();
}
Returns the actual URL for the specified asset.
| public getUrl( string $name, string $path ): string | ||
| $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);
}
Returns whether the asset bundle is registered.
| public isRegisteredBundle( string $name ): boolean | ||
| $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]);
}
Registers asset bundle by name.
| public register( string $name, integer|null $jsPosition = null, integer|null $cssPosition = null ): void | ||
| $name | string |
The class name of the asset bundle (without the leading backslash). |
| $jsPosition | integer|null | |
| $cssPosition | integer|null | |
| 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);
}
Registers all allowed asset bundles.
| public registerAllAllowed( ): void | ||
| 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);
}
}
Registers an asset bundle by name with custom configuration.
This method is similar to 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 registerCustomized( string $bundleName, array $bundleConfig ): void | ||
| $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);
}
Registers many asset bundles by names.
| public registerMany( string[] $names, integer|null $jsPosition = null, integer|null $cssPosition = null ): void | ||
| $names | string[] |
The many class names of the asset bundles (without the leading backslash). |
| $jsPosition | integer|null | |
| $cssPosition | integer|null | |
| 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);
}
}
Returns a new instance with the specified converter.
| public withConverter( Yiisoft\Assets\AssetConverterInterface $converter ): self | ||
| $converter | Yiisoft\Assets\AssetConverterInterface | |
public function withConverter(AssetConverterInterface $converter): self
{
$new = clone $this;
$new->registrar = $new->registrar->withConverter($converter);
return $new;
}
Returns a new instance with the specified loader.
| public withLoader( Yiisoft\Assets\AssetLoaderInterface $loader ): self | ||
| $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;
}
Returns a new instance with the specified publisher.
| public withPublisher( Yiisoft\Assets\AssetPublisherInterface $publisher ): self | ||
| $publisher | Yiisoft\Assets\AssetPublisherInterface | |
public function withPublisher(AssetPublisherInterface $publisher): self
{
$new = clone $this;
$new->publisher = $publisher;
return $new;
}
Signup or Login in order to comment.