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 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);
}
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.");
}
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);
}
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);
}
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;
}
| public array getCssFiles ( ) | ||
| return | array |
Config array of CSS files. |
|---|---|---|
public function getCssFiles(): array
{
return $this->registrar->getCssFiles();
}
| public array getCssStrings ( ) | ||
| return | array |
CSS blocks. |
|---|---|---|
public function getCssStrings(): array
{
return $this->registrar->getCssStrings();
}
| public array getJsFiles ( ) | ||
| return | array |
Config array of JavaScript files. |
|---|---|---|
public function getJsFiles(): array
{
return $this->registrar->getJsFiles();
}
| public array getJsStrings ( ) | ||
| return | array |
JavaScript code blocks. |
|---|---|---|
public function getJsStrings(): array
{
return $this->registrar->getJsStrings();
}
| public array getJsVars ( ) | ||
| return | array |
JavaScript variables. |
|---|---|---|
public function getJsVars(): array
{
return $this->registrar->getJsVars();
}
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);
}
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]);
}
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);
}
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);
}
}
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);
}
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);
}
}
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;
}
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;
}
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;
}
Signup or Login in order to comment.