AssetManager

Type
Class
Namespace
craft\web
Inherits
craft\web\AssetManager » yii\web\AssetManager (opens new window) » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
Implements
yii\base\Configurable (opens new window)
Since
3.0.0

View source (opens new window)

# Public Properties

Property Description
afterCopy (opens new window) callable (opens new window), null (opens new window) – A PHP callback that is called after a sub-directory or file is successfully copied.
appendTimestamp (opens new window) boolean (opens new window) – Whether to append a timestamp to the URL of every published asset.
assetMap (opens new window) string (opens new window)[] – Mapping from source asset files (keys) to target asset files (values).
basePath (opens new window) string (opens new window) – The root directory storing the published asset files.
baseUrl (opens new window) string (opens new window) – The base URL through which the published asset files can be accessed.
beforeCopy (opens new window) callable (opens new window), null (opens new window) – A PHP callback that is called before copying each sub-directory or file.
behaviors (opens new window) yii\base\Behavior (opens new window) – List of behaviors attached to this component.
bundles (opens new window) array (opens new window), false (opens new window) – List of asset bundle configurations.
converter (opens new window) yii\web\AssetConverterInterface (opens new window) – The asset converter.
dirMode (opens new window) integer (opens new window) – The permission to be set for newly generated asset directories.
fileMode (opens new window) integer (opens new window), null (opens new window) – The permission to be set for newly published asset files.
forceCopy (opens new window) boolean (opens new window) – Whether the directory being published should be copied even if it is found in the target directory.
hashCallback (opens new window) callable (opens new window), null (opens new window) – A callback that will be called to produce hash for asset directory generation.
linkAssets (opens new window) boolean (opens new window) – Whether to use symbolic link to publish asset files.

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__clone() (opens new window) This method is called after the object is created by cloning an existing one.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns the value of a component property.
__isset() (opens new window) Checks if a property is set, i.e. defined and not null.
__set() (opens new window) Sets the value of a component property.
__unset() (opens new window) Sets a component property to be null.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
behaviors() (opens new window) Returns a list of behaviors that this component should behave as.
canGetProperty() (opens new window) Returns a value indicating whether a property can be read.
canSetProperty() (opens new window) Returns a value indicating whether a property can be set.
checkBasePathPermission() (opens new window) Check whether the basePath exists and is writeable.
className() (opens new window) Returns the fully qualified name of this class.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
ensureBehaviors() (opens new window) Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component.
getActualAssetUrl() (opens new window) Returns the actual URL for the specified asset. Without parameters.
getAssetPath() (opens new window) Returns the actual file path for the specified asset.
getAssetUrl() (opens new window) Returns the actual URL for the specified asset.
getBehavior() (opens new window) Returns the named behavior object.
getBehaviors() (opens new window) Returns all behaviors attached to this component.
getBundle() (opens new window) Returns the named asset bundle.
getConverter() (opens new window) Returns the asset converter.
getPublishedPath() Returns the published path of a file/directory path.
getPublishedUrl() Returns the URL of a published file/directory path.
hasEventHandlers() (opens new window) Returns a value indicating whether there is any handler attached to the named event.
hasMethod() (opens new window) Returns a value indicating whether a method is defined.
hasProperty() (opens new window) Returns a value indicating whether a property is defined for this component.
init() (opens new window) Initializes the component.
off() (opens new window) Detaches an existing event handler from this component.
on() (opens new window) Attaches an event handler to an event.
publish() Publishes a file or a directory.
setConverter() (opens new window) Sets the asset converter.
trigger() (opens new window) Triggers an event.

# getPublishedPath()

Returns the published path of a file/directory path.

View source (opens new window)

Arguments

Returns

string (opens new window), false (opens new window) – The published file or directory path, or false if $publish is false and the file or directory does not exist

# getPublishedUrl()

Returns the URL of a published file/directory path.

View source (opens new window)

Arguments

Returns

string (opens new window), false (opens new window) – The published URL for the file or directory, or false if $publish is false and the file or directory does not exist

# publish()

Publishes a file or a directory.

This method will copy the specified file or directory to basePath (opens new window) so that it can be accessed via the Web server.

If the asset is a file, its file modification time will be checked to avoid unnecessary file copying.

If the asset is a directory, all files and subdirectories under it will be published recursively. Note, in case $forceCopy is false the method only checks the existence of the target directory to avoid repetitive copying (which is very expensive).

By default, when publishing a directory, subdirectories and files whose name starts with a dot "." will NOT be published. If you want to change this behavior, you may specify the "beforeCopy" option as explained in the $options parameter.

Note: On rare scenario, a race condition can develop that will lead to a one-time-manifestation of a non-critical problem in the creation of the directory that holds the published assets. This problem can be avoided altogether by 'requesting' in advance all the resources that are supposed to trigger a 'publish()' call, and doing that in the application deployment phase, before system goes live. See more in the following discussion: https://code.google.com/archive/p/yii/issues/2579

View source (opens new window)

Arguments

  • $path (string (opens new window)) – The asset (file or directory) to be published

  • $options (array (opens new window)) – The options to be applied when publishing a directory. The following options are supported:

  • only: array, list of patterns that the file paths should match if they want to be copied.

  • except: array, list of patterns that the files or directories should match if they want to be excluded from being copied.

  • caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to true.

  • beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. This overrides beforeCopy (opens new window) if set.

  • afterCopy: callback, a PHP callback that is called after a sub-directory or file is successfully copied. This overrides afterCopy (opens new window) if set.

  • forceCopy: boolean, whether the directory being published should be copied even if it is found in the target directory. This option is used only when publishing a directory. This overrides forceCopy (opens new window) if set.

Returns

array (opens new window) – The path (directory or file path) and the URL that the asset is published as.

Throws

# Protected Methods

Method Description
hash() Generate a CRC32 hash for the directory path. Collisions are higher than MD5 but generates a much smaller hash string.
loadBundle() (opens new window) Loads asset bundle class by name.
loadDummyBundle() (opens new window) Loads dummy bundle by name.
publishDirectory() Publishes a directory.
publishFile() Publishes a file.
resolveAsset() (opens new window)

# hash()

Generate a CRC32 hash for the directory path. Collisions are higher than MD5 but generates a much smaller hash string.

View source (opens new window)

Arguments

Returns

string (opens new window) – Hashed string.

# publishDirectory()

Publishes a directory.

View source (opens new window)

Arguments

  • $src (string (opens new window)) – The asset directory to be published

  • $options (array (opens new window)) – The options to be applied when publishing a directory. The following options are supported:

  • only: array, list of patterns that the file paths should match if they want to be copied.

  • except: array, list of patterns that the files or directories should match if they want to be excluded from being copied.

  • caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to true.

  • beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. This overrides beforeCopy (opens new window) if set.

  • afterCopy: callback, a PHP callback that is called after a sub-directory or file is successfully copied. This overrides afterCopy (opens new window) if set.

  • forceCopy: boolean, whether the directory being published should be copied even if it is found in the target directory. This option is used only when publishing a directory. This overrides forceCopy (opens new window) if set.

Returns

string (opens new window)[] – The path directory and the URL that the asset is published as.

Throws

# publishFile()

Publishes a file.

View source (opens new window)

Arguments

Returns

string (opens new window)[] – The path and the URL that the asset is published as.

Throws