AssetMaker

Asset Maker Trait Adds asset based methods to a class

 trait System\Traits\AssetMaker

Properties

public $assetPath : string

Specifies a path to the asset directory.

protected $assets : array

Collection of assets to display in the layout.

Methods

public addCss (array | string $name, array $attributes = []) : void

Adds StyleSheet asset to the asset list. Call $this->makeAssets() in a view to output corresponding markup.

Parameters
Property Description
$name
array | string

Specifies a path (URL) or an array of paths to the stylesheet(s).

$attributes
array

Adds extra HTML attributes to the asset link.

Returns
void

public addJs (array | string $name, array $attributes = []) : void

Adds JavaScript asset to the asset list. Call $this->makeAssets() in a view to output corresponding markup.

Parameters
Property Description
$name
array | string

Specifies a path (URL) or an array of paths to the script(s).

$attributes
array

Adds extra HTML attributes to the asset link.

Returns
void

public addRss (string $name, array $attributes = []) : void

Adds an RSS link asset to the asset list. Call $this->makeAssets() in a view to output corresponding markup.

Parameters
Property Description
$name
string

Specifies a path (URL) to the RSS channel

$attributes
array

Adds extra HTML attributes to the asset link.

Returns
void

public combineAssets (array $assets, string $localPath = "") : string

Run the provided assets through the Asset Combiner

Parameters
Property Description
$assets
array

Collection of assets

$localPath
string

Prefix all assets with this path (optional)

Returns
string

public flushAssets () : void

Disables the use, and subequent broadcast, of assets. This is useful to call during an AJAX request to speed things up. This method works by specifically targeting the hasAssetsDefined method.

Returns
void

public getAssetPath (string $fileName, string $assetPath = null) : string

Locates a file based on it's definition. If the file starts with a forward slash, it will be returned in context of the application public path, otherwise it will be returned in context of the asset path.

Parameters
Property Description
$fileName
string

File to load.

$assetPath
string

Explicitly define an asset path.

Returns
string

Relative path to the asset file.

public getAssetPaths () : array

Returns an array of all registered asset paths.

Returns
array

public hasAssetsDefined () : bool

Returns true if assets any have been added.

Returns
bool

public makeAssets ($type = null) : string | null

Outputs <link> and <script> tags to load assets previously added with addJs, addCss, & addRss method calls depending on the provided $type

Parameters
Property Description
$type
mixed
Returns
string | null

protected addAsset (string $type, string $path, array $attributes)

Adds the provided asset to the internal asset collections

Parameters
Property Description
$type
string

The type of the asset: 'js' || 'css' || 'rss'

$path
string

The path to the asset

$attributes
array

The attributes for the asset

Returns
mixed

protected getAssetEntryBuildPath (array $asset) : string

Internal helper, attaches a build code to an asset path

Parameters
Property Description
$asset
array

Stored asset array

Returns
string

protected getAssetScheme (string $asset) : string

Internal helper, get asset scheme

Parameters
Property Description
$asset
string

Specifies a path (URL) to the asset.

Returns
string

protected getLocalPath (string | null $relativePath)

Parameters
Property Description
$relativePath
string | null
Returns
mixed

protected removeDuplicates () : void

Removes duplicate assets from the entire collection.

Returns
void
Copyright © 2024 Winter CMS