PHP Class yii\console\controllers\AssetController

Usage: 1. Create a configuration file using the template action: yii asset/template /path/to/myapp/config.php 2. Edit the created config file, adjusting it for your web application needs. 3. Run the 'compress' action, using created config: yii asset /path/to/myapp/config.php /path/to/myapp/config/assets_compressed.php 4. Adjust your web application config to use compressed assets. Note: in the console environment some path aliases like @webroot and @web may not exist, so corresponding paths inside the configuration should be specified directly. Note: by default this command relies on an external tools to perform actual files compression, check [[jsCompressor]] and [[cssCompressor]] for more details.
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\console\Controller
Datei anzeigen Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$bundles list of asset bundles to be compressed.
$cssCompressor CSS file compressor. If a string, it is treated as shell command template, which should contain placeholders {from} - source file name - and {to} - output file name. Otherwise, it is treated as PHP callback, which should perform the compression. Default value relies on usage of "YUI Compressor"
$defaultAction controller default action ID.
$deleteSource whether to delete asset source files after compression. This option affects only those bundles, which have [[\yii\web\AssetBundle::sourcePath]] is set.
$jsCompressor JavaScript file compressor. If a string, it is treated as shell command template, which should contain placeholders {from} - source file name - and {to} - output file name. Otherwise, it is treated as PHP callback, which should perform the compression. Default value relies on usage of "Closure Compiler"
$targets list of asset bundles, which represents output compressed files. You can specify the name of the output compressed file using 'css' and 'js' keys: For example: php 'app\config\AllAsset' => [ 'js' => 'js/all-{hash}.js', 'css' => 'css/all-{hash}.css', 'depends' => [ ... ], ] File names can contain placeholder "{hash}", which will be filled by the hash of the resulting file. You may specify several target bundles in order to compress different groups of assets. In this case you should use 'depends' key to specify, which bundles should be covered with particular target bundle. You may leave 'depends' to be empty for single bundle, which will compress all remaining bundles in this case. For example: php 'allShared' => [ 'js' => 'js/all-shared-{hash}.js', 'css' => 'css/all-shared-{hash}.css', 'depends' => [ Include all assets shared between 'backend' and 'frontend' 'yii\web\YiiAsset', 'app\assets\SharedAsset', ], ], 'allBackEnd' => [ 'js' => 'js/all-{hash}.js', 'css' => 'css/all-{hash}.css', 'depends' => [ Include only 'backend' assets: 'app\assets\AdminAsset' ], ], 'allFrontEnd' => [ 'js' => 'js/all-{hash}.js', 'css' => 'css/all-{hash}.css', 'depends' => [], // Include all remaining assets ],

Public Methods

Method Description
actionCompress ( string $configFile, string $bundleFile ) Combines and compresses the asset files according to the given configuration.
actionTemplate ( string $configFile ) : integer Creates template of configuration file for [[actionCompress]].
combineCssFiles ( array $inputFiles, string $outputFile ) Combines CSS files into a single one.
combineJsFiles ( array $inputFiles, string $outputFile ) Combines JavaScript files into a single one.
getAssetManager ( ) : AssetManager Returns the asset manager instance.
setAssetManager ( AssetManager | array $assetManager ) Sets asset manager instance or configuration.

Protected Methods

Method Description
adjustCssUrl ( string $cssContent, string $inputFilePath, string $outputFilePath ) : string Adjusts CSS content allowing URL references pointing to the original resources.
adjustDependency ( AssetBundle[] $targets, AssetBundle[] $bundles ) : AssetBundle[] Adjust dependencies between asset bundles in the way source bundles begin to depend on output ones.
buildTarget ( AssetBundle $target, string $type, AssetBundle[] $bundles ) Builds output asset bundle.
compressCssFiles ( array $inputFiles, string $outputFile ) Compresses given CSS files and combines them into the single one.
compressJsFiles ( array $inputFiles, string $outputFile ) Compresses given JavaScript files and combines them into the single one.
loadBundles ( string[] $bundles ) : AssetBundle[] Creates full list of source asset bundles.
loadConfiguration ( string $configFile ) Applies configuration from the given file to self instance.
loadDependency ( AssetBundle $bundle, array &$result ) Loads asset bundle dependencies recursively.
loadTargets ( array $targets, AssetBundle[] $bundles ) : AssetBundle[] Creates full list of output asset bundles.
registerBundle ( AssetBundle[] $bundles, string $name, array &$registered ) Registers asset bundles including their dependencies.
saveTargets ( AssetBundle[] $targets, string $bundleFile ) Saves new asset bundles configuration.

Private Methods

Method Description
composeBundleConfig ( AssetBundle $bundle ) : array
composeCircularDependencyTrace ( string $circularDependencyName, array $registered ) : string Composes trace info for bundle circular dependency.
deletePublishedAssets ( AssetBundle[] $bundles ) Deletes bundle asset files, which have been published from sourcePath.
findRealPath ( string $path ) : string Returns canonicalized absolute pathname.
isBundleExternal ( AssetBundle $bundle ) : boolean

Method Details

actionCompress() public method

During the process new asset bundle configuration file will be created. You should replace your original asset bundle configuration with this file in order to use compressed files.
public actionCompress ( string $configFile, string $bundleFile )
$configFile string configuration file name.
$bundleFile string output asset bundles configuration file name.

actionTemplate() public method

Creates template of configuration file for [[actionCompress]].
public actionTemplate ( string $configFile ) : integer
$configFile string output file name.
return integer CLI exit code

adjustCssUrl() protected method

Adjusts CSS content allowing URL references pointing to the original resources.
protected adjustCssUrl ( string $cssContent, string $inputFilePath, string $outputFilePath ) : string
$cssContent string source CSS content.
$inputFilePath string input CSS file name.
$outputFilePath string output CSS file name.
return string adjusted CSS content.

adjustDependency() protected method

Adjust dependencies between asset bundles in the way source bundles begin to depend on output ones.
protected adjustDependency ( AssetBundle[] $targets, AssetBundle[] $bundles ) : AssetBundle[]
$targets yii\web\AssetBundle[] output asset bundles.
$bundles yii\web\AssetBundle[] source asset bundles.
return yii\web\AssetBundle[] output asset bundles.

buildTarget() protected method

Builds output asset bundle.
protected buildTarget ( AssetBundle $target, string $type, AssetBundle[] $bundles )
$target yii\web\AssetBundle output asset bundle
$type string either 'js' or 'css'.
$bundles yii\web\AssetBundle[] source asset bundles.

combineCssFiles() public method

Combines CSS files into a single one.
public combineCssFiles ( array $inputFiles, string $outputFile )
$inputFiles array source file names.
$outputFile string output file name.

combineJsFiles() public method

Combines JavaScript files into a single one.
public combineJsFiles ( array $inputFiles, string $outputFile )
$inputFiles array source file names.
$outputFile string output file name.

compressCssFiles() protected method

Compresses given CSS files and combines them into the single one.
protected compressCssFiles ( array $inputFiles, string $outputFile )
$inputFiles array list of source file names.
$outputFile string output file name.

compressJsFiles() protected method

Compresses given JavaScript files and combines them into the single one.
protected compressJsFiles ( array $inputFiles, string $outputFile )
$inputFiles array list of source file names.
$outputFile string output file name.

getAssetManager() public method

Returns the asset manager instance.
public getAssetManager ( ) : AssetManager
return yii\web\AssetManager asset manager instance.

loadBundles() protected method

Creates full list of source asset bundles.
protected loadBundles ( string[] $bundles ) : AssetBundle[]
$bundles string[] list of asset bundle names
return yii\web\AssetBundle[] list of source asset bundles.

loadConfiguration() protected method

Applies configuration from the given file to self instance.
protected loadConfiguration ( string $configFile )
$configFile string configuration file name.

loadDependency() protected method

Loads asset bundle dependencies recursively.
protected loadDependency ( AssetBundle $bundle, array &$result )
$bundle yii\web\AssetBundle bundle instance
$result array already loaded bundles list.

loadTargets() protected method

Creates full list of output asset bundles.
protected loadTargets ( array $targets, AssetBundle[] $bundles ) : AssetBundle[]
$targets array output asset bundles configuration.
$bundles yii\web\AssetBundle[] list of source asset bundles.
return yii\web\AssetBundle[] list of output asset bundles.

registerBundle() protected method

Registers asset bundles including their dependencies.
protected registerBundle ( AssetBundle[] $bundles, string $name, array &$registered )
$bundles yii\web\AssetBundle[] asset bundles list.
$name string bundle name.
$registered array stores already registered names.

saveTargets() protected method

Saves new asset bundles configuration.
protected saveTargets ( AssetBundle[] $targets, string $bundleFile )
$targets yii\web\AssetBundle[] list of asset bundles to be saved.
$bundleFile string output file name.

setAssetManager() public method

Sets asset manager instance or configuration.
public setAssetManager ( AssetManager | array $assetManager )
$assetManager yii\web\AssetManager | array asset manager instance or its array configuration.

Property Details

$bundles public_oe property

list of asset bundles to be compressed.
public $bundles

$cssCompressor public_oe property

CSS file compressor. If a string, it is treated as shell command template, which should contain placeholders {from} - source file name - and {to} - output file name. Otherwise, it is treated as PHP callback, which should perform the compression. Default value relies on usage of "YUI Compressor"
See also: https://github.com/yui/yuicompressor/
public $cssCompressor

$defaultAction public_oe property

controller default action ID.
public $defaultAction

$deleteSource public_oe property

whether to delete asset source files after compression. This option affects only those bundles, which have [[\yii\web\AssetBundle::sourcePath]] is set.
Since: 2.0.10
public $deleteSource

$jsCompressor public_oe property

JavaScript file compressor. If a string, it is treated as shell command template, which should contain placeholders {from} - source file name - and {to} - output file name. Otherwise, it is treated as PHP callback, which should perform the compression. Default value relies on usage of "Closure Compiler"
See also: https://developers.google.com/closure/compiler/
public $jsCompressor

$targets public_oe property

list of asset bundles, which represents output compressed files. You can specify the name of the output compressed file using 'css' and 'js' keys: For example: php 'app\config\AllAsset' => [ 'js' => 'js/all-{hash}.js', 'css' => 'css/all-{hash}.css', 'depends' => [ ... ], ] File names can contain placeholder "{hash}", which will be filled by the hash of the resulting file. You may specify several target bundles in order to compress different groups of assets. In this case you should use 'depends' key to specify, which bundles should be covered with particular target bundle. You may leave 'depends' to be empty for single bundle, which will compress all remaining bundles in this case. For example: php 'allShared' => [ 'js' => 'js/all-shared-{hash}.js', 'css' => 'css/all-shared-{hash}.css', 'depends' => [ Include all assets shared between 'backend' and 'frontend' 'yii\web\YiiAsset', 'app\assets\SharedAsset', ], ], 'allBackEnd' => [ 'js' => 'js/all-{hash}.js', 'css' => 'css/all-{hash}.css', 'depends' => [ Include only 'backend' assets: 'app\assets\AdminAsset' ], ], 'allFrontEnd' => [ 'js' => 'js/all-{hash}.js', 'css' => 'css/all-{hash}.css', 'depends' => [], // Include all remaining assets ],
public $targets