Assets
in package
Assets class.
To use Assets class, you have to call these methods by order.
-
addAsset()
,addMultipleAssets()
. You can use one of these methods but no need to call them all.
- If you want to add more inline style or JavaScript please call
addCssInline()
,addJsInline()
.
- If you want to add JS object (to access them easily in JSON format) please call
addJsObject()
.
- In the views page call to
renderAssets()
.
That's all the basic usage.
Tags
Table of Contents
Properties
- $addedAssets : array<string|int, mixed>
- $assetsSorted : array<string|int, mixed>
- $addedAssets : array<string|int, mixed>
- $assetsSorted : array<string|int, mixed>
- $Container : Container
Methods
- __construct() : mixed
- Class constructor.
- __get() : mixed
- Magic get.
- addAsset() : mixed
- Add an asset to the queue by type.
- addCssInline() : mixed
- Add in-line stylesheet to extend current css file.
- addJsInline() : mixed
- Add in-line JavaScript to extend current js file.
- addJsObject() : mixed
- Add JavaScript object for use the data between js and php such as translation text.
- addMultipleAssets() : mixed
- Add multiple assets at once to the queue by type.
- assetIs() : bool
- Check that if asset is ...(action)... Example: check if asset is already "added" or "printed".
- clear() : mixed
- Clear any added assets and begins again.
- mergeAssetsData() : array<string|int, mixed>
- Merge selected type of assets data.
- removeAsset() : mixed
- Remove the added asset from specified handle name.
- renderAssets() : string
- Render the assets into HTML elements.
- addMissedDependencies() : mixed
- Automatically add missed dependencies.
- generateAssetUrlWithVersion() : Return
- Generate asset URL with ?v=version_number append.
- generateAttributes() : string
- Generate HTML attributes by filter out disallow attributes.
- generateInlineScript() : string
- Generate in-line JavaScript.
- generateInlineStyle() : string
- Generate in-line stylesheet.
- generateJsObject() : string
- Generate JavaScript object.
- getDependencyExists() : array<string|int, mixed>
- Check and separate dependencies into exists and not exists.
- topologicalSort() : mixed
- Topological sort the assets by type (js, css).
- verifyType() : string
- Verify asset type.
- renderAssetCss() : string
- Render individual asset (CSS).
- renderAssetJs() : string
- Render individual asset (JS).
Properties
$addedAssets read-only
public
array<string|int, mixed>
$addedAssets
Contain the associative array with js
, css
keys and its asset files that was added.
$assetsSorted read-only
public
array<string|int, mixed>
$assetsSorted
Contain the associative array with css
, js
keys and its value is boolean for checking before do topological sort.
$addedAssets
protected
array<string|int, mixed>
$addedAssets
= ['css' => [], 'js' => []]
Contain the associative array with js
, css
keys and its asset files that was added.
$assetsSorted
protected
array<string|int, mixed>
$assetsSorted
= ['css' => false, 'js' => false]
Contain the associative array with css
, js
keys and its value is boolean for checking before do topological sort.
$Container
protected
Container
$Container
Methods
__construct()
Class constructor.
public
__construct(Container $Container) : mixed
Parameters
- $Container : Container
-
The DI container class.
__get()
Magic get.
public
__get(string $name) : mixed
Parameters
- $name : string
Tags
addAsset()
Add an asset to the queue by type.
public
addAsset(string $type, string $handle, string $file[, array<string|int, mixed> $dependency = [] ][, string|bool $version = true ][, array<string|int, mixed> $attributes = [] ][, string|null $group = null ]) : mixed
Parameters
- $type : string
-
The asset type (css, js).
- $handle : string
-
The handle name of the asset. It must be unique to prevent overriding and duplication.
- $file : string
-
The URL to file, it can be full URL or just relative path.
If the file has special characters, it should berawurlencode()
before calling this. - $dependency : array<string|int, mixed> = []
-
An array of added asset handles that this asset is depend on.
The css and js dependency cannot be cross type.
This class is not support cross group dependency yet. - $version : string|bool = true
-
The version number to added to the query string of this asset URL. If
false
means no version is added. - $attributes : array<string|int, mixed> = []
-
An array of HTML attributes for this asset. Do not add id attribute because it will be auto generated.
- $group : string|null = null
-
The group name. This can be helped when you want to render the assets by group.
Tags
addCssInline()
Add in-line stylesheet to extend current css file.
public
addCssInline(string $handle, string $data) : mixed
You have to escape anything that should not be HTML by yourself before add to data.
Parameters
- $handle : string
-
The handle name of exists css file.
- $data : string
-
The stylesheet content. Do not include
<style>...</style>
tag.
addJsInline()
Add in-line JavaScript to extend current js file.
public
addJsInline(string $handle, string $data[, string $position = 'after' ]) : mixed
Parameters
- $handle : string
-
The handle name of exists js file.
- $data : string
-
The JavaScript content. Do not include
<script>...</script>
tag. - $position : string = 'after'
-
Position to add this in-line js. The value is "before", "after". Please call this method after
addAsset()
method even if position is before, otherwise it will not show up.
addJsObject()
Add JavaScript object for use the data between js and php such as translation text.
public
addJsObject(string $handle, string $name, array<string|int, mixed> $data) : mixed
You have to escape anything that should not be HTML by yourself before add to data.
Parameters
- $handle : string
-
The asset handle name of exists js file.
- $name : string
-
The JavaScript object name. This should be unique to prevent conflict with others.
- $data : array<string|int, mixed>
-
Recommended use associative array where the key will becomes js property.
addMultipleAssets()
Add multiple assets at once to the queue by type.
public
addMultipleAssets(string $type, array<string|int, mixed> $handles, array<string|int, mixed> $assetsData) : mixed
This method will add those dependencies for each asset handle automatically.
Parameters
- $type : string
-
The asset type (css, js).
- $handles : array<string|int, mixed>
-
The array list of asset's handle name that you want to add. The handle should exists in
$assetsData
. - $assetsData : array<string|int, mixed>
-
The assets data in array value. These assets will be chosen by
$handles
and add to the class.
The data structure will be...array( 'css' => array( // contain the asset type in array key. array( // the array keys name in this will be matched Assets->addAsset() arguments except the type. 'handle' => (string)'xxx', // this is required 'file' => (string)'xxx/xxx/xxx.xx', // this is required 'dependency' => array('the', 'array', 'of', 'handle', 'this', 'asset', 'depend', 'on'), 'version' => 'x.x.x', // string or boolean 'attributes' => array('title' => 'my element title', 'data-name' => 'my data-name attribute'), 'group' => null, // string or null ), array( .... ), ), 'js' => array( // contain the asset type in array key. array( // the array keys name in this will be matched Assets->addAsset() arguments except the type. 'handle' => (string)'xxx', // this is required 'file' => (string)'xxx/xxx/xxx.xx', // this is required 'dependency' => array('the', 'array', 'of', 'handle', 'this', 'asset', 'depend', 'on'), 'version' => 'x.x.x', // string or boolean 'attributes' => array('title' => 'my element title', 'data-name' => 'my data-name attribute'), 'group' => null, // string or null ), array( .... ), ), )
This class is not support cross group dependency yet.
assetIs()
Check that if asset is ...(action)... Example: check if asset is already "added" or "printed".
public
assetIs(string $type, string $handle[, string $action = 'added' ]) : bool
Parameters
- $type : string
-
The asset type (css, js).
- $handle : string
-
The asset's handle name by type.
- $action : string = 'added'
-
The action to check. The value is "added", "printed"
Return values
bool —Return true if the selected action already did. Return false for not.
clear()
Clear any added assets and begins again.
public
clear(string $type) : mixed
It will be clear by asset type (css, js).
Parameters
- $type : string
-
The asset type to be cleared. The value is css, js. Set this to empty means clear all.
mergeAssetsData()
Merge selected type of assets data.
public
mergeAssetsData(string $type, array<string|int, mixed> ...$assetsData) : array<string|int, mixed>
The last asset data will be append to the end of previous asset data.
Parameters
- $type : string
-
'css' or 'js'.
- $assetsData : array<string|int, mixed>
-
The assets data in array value. The array format must contain type as key. Example array('css' => array());
Return values
array<string|int, mixed> —Return merged assets data.
removeAsset()
Remove the added asset from specified handle name.
public
removeAsset(string $type, string $handle) : mixed
Parameters
- $type : string
-
The asset type (css, js).
- $handle : string
-
The asset handle name.
renderAssets()
Render the assets into HTML elements.
public
renderAssets(string $type[, string|null $group = null ]) : string
This will be call to topologicalSort()
and then render.
This class is not support cross group dependency yet.
Parameters
- $type : string
-
The asset type (css, js).
- $group : string|null = null
-
The asset group that was specified via
addCss()
method.
Return values
string —Return the generated HTML elements into string.
addMissedDependencies()
Automatically add missed dependencies.
protected
addMissedDependencies(string $type, array<string|int, mixed> $handles, array<string|int, mixed> $assetsData) : mixed
This method was called from addMultipleAssets()
method.
Parameters
- $type : string
-
The asset type (css, js).
- $handles : array<string|int, mixed>
-
The array list of asset's handle name to check that its dependency is already added or not.
- $assetsData : array<string|int, mixed>
-
The assets data in array value.
generateAssetUrlWithVersion()
Generate asset URL with ?v=version_number append.
protected
generateAssetUrlWithVersion(array<string|int, mixed> $item) : Return
This method was called from renderAssets()
method.
Parameters
- $item : array<string|int, mixed>
-
The item should have "file" and "version" in array key.
Return values
Return —generated asset URL. The returned value did not escape for HTML, you must use htmlspecialchars()
to make something such as &
to be &
which is correct HTML value.
generateAttributes()
Generate HTML attributes by filter out disallow attributes.
protected
generateAttributes(array<string|int, mixed> $attributes[, array<string|int, mixed> $disallowAttributes = [] ]) : string
This method was called from renderAssets()
method.
Parameters
- $attributes : array<string|int, mixed>
-
The associative array of attributes. Example:
['title' => 'My Title', 'data-id' => 3]
will becomestitle="My Title" data-id="3"
- $disallowAttributes : array<string|int, mixed> = []
-
The 2D array of disallow attribute names. Example:
['id', 'class']
so, the$attributes
that contain id and class will be removed.
Return values
string —Return generated HTML attributes with trim spaces. The returned value is already escape attribute value with htmlspecialchars()
.
generateInlineScript()
Generate in-line JavaScript.
protected
generateInlineScript(array<string|int, mixed> $item[, string $position = 'after' ]) : string
This method was called from renderAssets()
method.
Parameters
- $item : array<string|int, mixed>
-
The item should have "inline" in array key.
- $position : string = 'after'
-
The position is "before", "after".
Tags
Return values
string —Return generated in-line JavaScript.
generateInlineStyle()
Generate in-line stylesheet.
protected
generateInlineStyle(array<string|int, mixed> $item) : string
This method was called from renderAssets()
method.
Parameters
- $item : array<string|int, mixed>
-
The item should have "inline" in array key.
Return values
string —Return generated in-line stylesheet.
generateJsObject()
Generate JavaScript object.
protected
generateJsObject(array<string|int, mixed> $item) : string
This method was called from renderAssets()
method.
Parameters
- $item : array<string|int, mixed>
-
The item should have
jsobject
in array key.
Tags
Return values
string —Return generated JS object.
getDependencyExists()
Check and separate dependencies into exists and not exists.
protected
getDependencyExists(string $type, array<string|int, mixed> $dependency) : array<string|int, mixed>
This method was called from topologicalSort()
method.
Parameters
- $type : string
-
The asset type (css, js).
- $dependency : array<string|int, mixed>
-
The dependencies list array.
Return values
array<string|int, mixed> —Return associative array with exists
and not_exists
in keys. The array values of exists
, not_exists
are from $dependency
list.
topologicalSort()
Topological sort the assets by type (js, css).
protected
topologicalSort(string $type) : mixed
Topological sort is sorting by dependency comes before and then follow by order.
Thanks to marcj/topsort ( https://github.com/marcj/topsort.php ) to make this easy.
This method was called from renderAssets()
method.
Parameters
- $type : string
-
The asset type value is css, js.
verifyType()
Verify asset type.
protected
verifyType(string $type) : string
This is common use in many methods to verify asset type (css, js).
Parameters
- $type : string
-
The asset type (css, js).
Return values
string —Return the correct asset type. If incorrect then return js
.
renderAssetCss()
Render individual asset (CSS).
private
renderAssetCss(string $handle, array<string|int, mixed> $item) : string
Parameters
- $handle : string
-
The handle name.
- $item : array<string|int, mixed>
-
The array item.
Return values
string —Return rendered HTML.
renderAssetJs()
Render individual asset (JS).
private
renderAssetJs(string $handle, array<string|int, mixed> $item) : string
Parameters
- $handle : string
-
The handle name.
- $item : array<string|int, mixed>
-
The array item.
Tags
Return values
string —Return rendered HTML.