Back to WebAssetManager class

Method registerAsset

public self
registerAsset
(string $type, mixed $asset, string $uri = '', array $options = [], array $attributes = [], array $dependencies = [])
Register a new asset.
Parameters
  • string $type The asset type, script or style
  • \Joomla\CMS\WebAsset\WebAssetItem|string $asset The asset name or instance to register
  • string $uri The URI for the asset
  • array $options Additional options for the asset
  • array $attributes Attributes for the asset
  • array $dependencies Asset dependencies
Returns
  • self
Since
  • 4.0.0
-
  • \InvalidArgumentException

Method registerAsset - Source code

/**
 * Register a new asset.
 * Allow to register WebAssetItem instance in the registry, by call registerAsset($type, $assetInstance)
 * Or create an asset on fly (from name and Uri) and register in the registry, by call registerAsset($type, $assetName, $uri, $options ....)
 *
 * @param   string               $type          The asset type, script or style
 * @param   WebAssetItem|string  $asset         The asset name or instance to register
 * @param   string               $uri           The URI for the asset
 * @param   array                $options       Additional options for the asset
 * @param   array                $attributes    Attributes for the asset
 * @param   array                $dependencies  Asset dependencies
 *
 * @return  self
 *
 * @since  4.0.0
 *
 * @throws  \InvalidArgumentException
 */
public function registerAsset(string $type, $asset, string $uri = '', array $options = [], array $attributes = [], array $dependencies = [])
{
    if ($asset instanceof WebAssetItemInterface) {
        $this->registry->add($type, $asset);
    } elseif (is_string($asset)) {
        $options['type'] = $type;
        $assetInstance = $this->registry->createAsset($asset, $uri, $options, $attributes, $dependencies);
        $this->registry->add($type, $assetInstance);
    } else {
        throw new \InvalidArgumentException(sprintf('%s(): Argument #2 ($asset) must be a string or an instance of %s, %s given.', __METHOD__, WebAssetItemInterface::class, \is_object($asset) ? \get_class($asset) : \gettype($asset)));
    }
    return $this;
}