You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
505 lines
15 KiB
505 lines
15 KiB
12 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\console\controllers;
|
||
|
|
||
|
use Yii;
|
||
|
use yii\console\Exception;
|
||
|
use yii\console\Controller;
|
||
|
|
||
|
/**
|
||
12 years ago
|
* This command allows you to combine and compress your JavaScript and CSS files.
|
||
|
*
|
||
12 years ago
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
12 years ago
|
class AssetController extends Controller
|
||
12 years ago
|
{
|
||
12 years ago
|
/**
|
||
|
* @var string controller default action ID.
|
||
|
*/
|
||
12 years ago
|
public $defaultAction = 'compress';
|
||
12 years ago
|
/**
|
||
|
* @var array list of asset bundles to be compressed.
|
||
|
* The keys are the bundle names, and the values are the configuration
|
||
|
* arrays for creating the [[yii\web\AssetBundle]] objects.
|
||
|
*/
|
||
12 years ago
|
public $bundles = array();
|
||
12 years ago
|
/**
|
||
|
* @var array list of paths to the extensions, which assets should be also compressed.
|
||
|
* Each path should contain asset manifest file named "assets.php".
|
||
|
*/
|
||
12 years ago
|
public $extensions = array();
|
||
|
/**
|
||
12 years ago
|
* @var array 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:
|
||
12 years ago
|
* ~~~
|
||
|
* 'all' => array(
|
||
|
* 'css' => 'all.css',
|
||
|
* 'js' => 'js.css',
|
||
|
* 'depends' => array( ... ),
|
||
|
* )
|
||
|
* ~~~
|
||
12 years ago
|
* File names can contain placeholder "{ts}", which will be filled by current timestamp, while
|
||
|
* file creation.
|
||
12 years ago
|
*/
|
||
|
public $targets = array();
|
||
12 years ago
|
/**
|
||
|
* @var array configuration for [[yii\web\AssetManager]] instance, which will be used
|
||
|
* for assets publishing.
|
||
|
*/
|
||
12 years ago
|
public $assetManager = array();
|
||
12 years ago
|
/**
|
||
|
* @var string|callback Java Script 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.
|
||
|
* If an array, it is treated as PHP callback, which should perform the compression.
|
||
|
*
|
||
|
* Default value relies on usage of "Closure Compiler"
|
||
|
* @see https://developers.google.com/closure/compiler/
|
||
|
*/
|
||
12 years ago
|
public $jsCompressor = 'java -jar compiler.jar --js {from} --js_output_file {to}';
|
||
12 years ago
|
/**
|
||
|
* @var string|callback 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.
|
||
|
* If an array, it is treated as PHP callback, which should perform the compression.
|
||
|
*
|
||
|
* Default value relies on usage of "YUI Compressor"
|
||
|
* @see https://github.com/yui/yuicompressor/
|
||
|
*/
|
||
12 years ago
|
public $cssCompressor = 'java -jar yuicompressor.jar {from} -o {to}';
|
||
12 years ago
|
|
||
12 years ago
|
/**
|
||
12 years ago
|
* Combines and compresses the asset files according to the given configuration.
|
||
12 years ago
|
* 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.
|
||
12 years ago
|
* @param string $configFile configuration file name.
|
||
12 years ago
|
* @param string $bundleFile output asset bundles configuration file name.
|
||
12 years ago
|
*/
|
||
12 years ago
|
public function actionCompress($configFile, $bundleFile)
|
||
|
{
|
||
|
$this->loadConfiguration($configFile);
|
||
|
$bundles = $this->loadBundles($this->bundles, $this->extensions);
|
||
12 years ago
|
$targets = $this->loadTargets($this->targets, $bundles);
|
||
12 years ago
|
$this->publishBundles($bundles, $this->assetManager);
|
||
12 years ago
|
$timestamp = time();
|
||
12 years ago
|
foreach ($targets as $name => $target) {
|
||
|
echo "Creating output bundle '{$name}':\n";
|
||
12 years ago
|
if (!empty($target->js)) {
|
||
12 years ago
|
$this->buildTarget($target, 'js', $bundles, $timestamp);
|
||
|
}
|
||
12 years ago
|
if (!empty($target->css)) {
|
||
12 years ago
|
$this->buildTarget($target, 'css', $bundles, $timestamp);
|
||
|
}
|
||
12 years ago
|
echo "\n";
|
||
12 years ago
|
}
|
||
|
|
||
|
$targets = $this->adjustDependency($targets, $bundles);
|
||
12 years ago
|
$this->saveTargets($targets, $bundleFile);
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Applies configuration from the given file to self instance.
|
||
|
* @param string $configFile configuration file name.
|
||
|
* @throws \yii\console\Exception on failure.
|
||
|
*/
|
||
12 years ago
|
protected function loadConfiguration($configFile)
|
||
|
{
|
||
12 years ago
|
echo "Loading configuration from '{$configFile}'...\n";
|
||
|
|
||
12 years ago
|
foreach (require($configFile) as $name => $value) {
|
||
|
if (property_exists($this, $name)) {
|
||
|
$this->$name = $value;
|
||
|
} else {
|
||
12 years ago
|
throw new Exception("Unknown configuration option: $name");
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
if (!isset($this->assetManager['basePath'])) {
|
||
|
throw new Exception("Please specify 'basePath' for the 'assetManager' option.");
|
||
12 years ago
|
}
|
||
12 years ago
|
if (!isset($this->assetManager['baseUrl'])) {
|
||
|
throw new Exception("Please specify 'baseUrl' for the 'assetManager' option.");
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
12 years ago
|
* Creates full list of source asset bundles.
|
||
12 years ago
|
* @param array[] $bundles list of asset bundle configurations.
|
||
|
* @param array $extensions list of the extension paths.
|
||
12 years ago
|
* @return \yii\web\AssetBundle[] list of source asset bundles.
|
||
12 years ago
|
*/
|
||
12 years ago
|
protected function loadBundles($bundles, $extensions)
|
||
|
{
|
||
12 years ago
|
echo "Collecting source bundles information...\n";
|
||
12 years ago
|
$result = array();
|
||
|
foreach ($bundles as $name => $bundle) {
|
||
|
$bundle['class'] = 'yii\\web\\AssetBundle';
|
||
|
$result[$name] = Yii::createObject($bundle);
|
||
|
}
|
||
|
foreach ($extensions as $path) {
|
||
|
$manifest = $path . '/assets.php';
|
||
|
if (!is_file($manifest)) {
|
||
|
continue;
|
||
|
}
|
||
|
foreach (require($manifest) as $name => $bundle) {
|
||
|
if (!isset($result[$name])) {
|
||
|
$bundle['class'] = 'yii\\web\\AssetBundle';
|
||
|
$result[$name] = Yii::createObject($bundle);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
12 years ago
|
* Creates full list of output asset bundles.
|
||
|
* @param array $targets output asset bundles configuration.
|
||
|
* @param \yii\web\AssetBundle[] $bundles list of source asset bundles.
|
||
|
* @return \yii\web\AssetBundle[] list of output asset bundles.
|
||
12 years ago
|
* @throws \yii\console\Exception on failure.
|
||
|
*/
|
||
12 years ago
|
protected function loadTargets($targets, $bundles)
|
||
|
{
|
||
12 years ago
|
// build the dependency order of bundles
|
||
12 years ago
|
$registered = array();
|
||
|
foreach ($bundles as $name => $bundle) {
|
||
|
$this->registerBundle($bundles, $name, $registered);
|
||
|
}
|
||
|
$bundleOrders = array_combine(array_keys($registered), range(0, count($bundles) - 1));
|
||
12 years ago
|
|
||
|
// fill up the target which has empty 'depends'.
|
||
|
$referenced = array();
|
||
|
foreach ($targets as $name => $target) {
|
||
|
if (empty($target['depends'])) {
|
||
|
if (!isset($all)) {
|
||
|
$all = $name;
|
||
|
} else {
|
||
|
throw new Exception("Only one target can have empty 'depends' option. Found two now: $all, $name");
|
||
|
}
|
||
|
} else {
|
||
|
foreach ($target['depends'] as $bundle) {
|
||
|
if (!isset($referenced[$bundle])) {
|
||
|
$referenced[$bundle] = $name;
|
||
|
} else {
|
||
|
throw new Exception("Target '{$referenced[$bundle]}' and '$name' cannot contain the bundle '$bundle' at the same time.");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if (isset($all)) {
|
||
|
$targets[$all]['depends'] = array_diff(array_keys($registered), array_keys($referenced));
|
||
|
}
|
||
|
|
||
|
// adjust the 'depends' order for each target according to the dependency order of bundles
|
||
|
// create an AssetBundle object for each target
|
||
12 years ago
|
foreach ($targets as $name => $target) {
|
||
|
if (!isset($target['basePath'])) {
|
||
|
throw new Exception("Please specify 'basePath' for the '$name' target.");
|
||
|
}
|
||
|
if (!isset($target['baseUrl'])) {
|
||
|
throw new Exception("Please specify 'baseUrl' for the '$name' target.");
|
||
|
}
|
||
|
usort($target['depends'], function ($a, $b) use ($bundleOrders) {
|
||
|
if ($bundleOrders[$a] == $bundleOrders[$b]) {
|
||
|
return 0;
|
||
|
} else {
|
||
|
return $bundleOrders[$a] > $bundleOrders[$b] ? 1 : -1;
|
||
|
}
|
||
|
});
|
||
|
$target['class'] = 'yii\\web\\AssetBundle';
|
||
|
$targets[$name] = Yii::createObject($target);
|
||
|
}
|
||
|
return $targets;
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
12 years ago
|
* Publishes given asset bundles.
|
||
|
* @param \yii\web\AssetBundle[] $bundles asset bundles to be published.
|
||
|
* @param array $options assert manager instance configuration.
|
||
12 years ago
|
*/
|
||
|
protected function publishBundles($bundles, $options)
|
||
12 years ago
|
{
|
||
12 years ago
|
echo "\nPublishing bundles:\n";
|
||
12 years ago
|
if (!isset($options['class'])) {
|
||
|
$options['class'] = 'yii\\web\\AssetManager';
|
||
|
}
|
||
|
$am = Yii::createObject($options);
|
||
12 years ago
|
foreach ($bundles as $name => $bundle) {
|
||
12 years ago
|
$bundle->publish($am);
|
||
12 years ago
|
echo " '".$name."' published.\n";
|
||
12 years ago
|
}
|
||
12 years ago
|
echo "\n";
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Builds output asset bundle.
|
||
|
* @param \yii\web\AssetBundle $target output asset bundle
|
||
|
* @param string $type either "js" or "css".
|
||
|
* @param \yii\web\AssetBundle[] $bundles source asset bundles.
|
||
|
* @param integer $timestamp current timestamp.
|
||
|
* @throws Exception on failure.
|
||
12 years ago
|
*/
|
||
12 years ago
|
protected function buildTarget($target, $type, $bundles, $timestamp)
|
||
12 years ago
|
{
|
||
12 years ago
|
$outputFile = strtr($target->$type, array(
|
||
12 years ago
|
'{ts}' => $timestamp,
|
||
|
));
|
||
|
$inputFiles = array();
|
||
12 years ago
|
|
||
|
foreach ($target->depends as $name) {
|
||
12 years ago
|
if (isset($bundles[$name])) {
|
||
|
foreach ($bundles[$name]->$type as $file) {
|
||
12 years ago
|
$inputFiles[] = $bundles[$name]->basePath . '/' . $file;
|
||
12 years ago
|
}
|
||
|
} else {
|
||
|
throw new Exception("Unknown bundle: $name");
|
||
|
}
|
||
|
}
|
||
|
if ($type === 'js') {
|
||
12 years ago
|
$this->compressJsFiles($inputFiles, $target->basePath . '/' . $outputFile);
|
||
12 years ago
|
} else {
|
||
12 years ago
|
$this->compressCssFiles($inputFiles, $target->basePath . '/' . $outputFile);
|
||
12 years ago
|
}
|
||
12 years ago
|
$target->$type = array($outputFile);
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
12 years ago
|
* Adjust dependencies between asset bundles in the way source bundles begin to depend on output ones.
|
||
|
* @param \yii\web\AssetBundle[] $targets output asset bundles.
|
||
|
* @param \yii\web\AssetBundle[] $bundles source asset bundles.
|
||
|
* @return \yii\web\AssetBundle[] output asset bundles.
|
||
12 years ago
|
*/
|
||
12 years ago
|
protected function adjustDependency($targets, $bundles)
|
||
|
{
|
||
12 years ago
|
echo "Creating new bundle configuration...\n";
|
||
|
|
||
12 years ago
|
$map = array();
|
||
|
foreach ($targets as $name => $target) {
|
||
|
foreach ($target->depends as $bundle) {
|
||
12 years ago
|
$map[$bundle] = $name;
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
|
foreach ($targets as $name => $target) {
|
||
|
$depends = array();
|
||
|
foreach ($target->depends as $bn) {
|
||
|
foreach ($bundles[$bn]->depends as $bundle) {
|
||
|
$depends[$map[$bundle]] = true;
|
||
|
}
|
||
|
}
|
||
|
unset($depends[$name]);
|
||
|
$target->depends = array_keys($depends);
|
||
|
}
|
||
|
|
||
|
// detect possible circular dependencies
|
||
|
foreach ($targets as $name => $target) {
|
||
|
$registered = array();
|
||
|
$this->registerBundle($targets, $name, $registered);
|
||
|
}
|
||
|
|
||
|
foreach ($map as $bundle => $target) {
|
||
|
$targets[$bundle] = Yii::createObject(array(
|
||
|
'class' => 'yii\\web\\AssetBundle',
|
||
|
'depends' => array($target),
|
||
|
));
|
||
|
}
|
||
12 years ago
|
return $targets;
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Registers asset bundles including their dependencies.
|
||
|
* @param \yii\web\AssetBundle[] $bundles asset bundles list.
|
||
|
* @param string $name bundle name.
|
||
|
* @param array $registered stores already registered names.
|
||
|
* @throws \yii\console\Exception if circular dependency is detected.
|
||
|
*/
|
||
12 years ago
|
protected function registerBundle($bundles, $name, &$registered)
|
||
|
{
|
||
|
if (!isset($registered[$name])) {
|
||
|
$registered[$name] = false;
|
||
|
$bundle = $bundles[$name];
|
||
|
foreach ($bundle->depends as $depend) {
|
||
|
$this->registerBundle($bundles, $depend, $registered);
|
||
|
}
|
||
|
unset($registered[$name]);
|
||
|
$registered[$name] = true;
|
||
|
} elseif ($registered[$name] === false) {
|
||
|
throw new Exception("A circular dependency is detected for target '$name'.");
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Saves new asset bundles configuration.
|
||
|
* @param \yii\web\AssetBundle[] $targets list of asset bundles to be saved.
|
||
|
* @param string $bundleFile output file name.
|
||
|
*/
|
||
12 years ago
|
protected function saveTargets($targets, $bundleFile)
|
||
|
{
|
||
|
$array = array();
|
||
|
foreach ($targets as $name => $target) {
|
||
|
foreach (array('js', 'css', 'depends', 'basePath', 'baseUrl') as $prop) {
|
||
|
if (!empty($target->$prop)) {
|
||
|
$array[$name][$prop] = $target->$prop;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
$array = var_export($array, true);
|
||
|
$version = date('Y-m-d H:i:s', time());
|
||
12 years ago
|
$bytesWritten = file_put_contents($bundleFile, <<<EOD
|
||
12 years ago
|
<?php
|
||
|
/**
|
||
12 years ago
|
* This file is generated by the "yii script" command.
|
||
12 years ago
|
* DO NOT MODIFY THIS FILE DIRECTLY.
|
||
12 years ago
|
* @version $version
|
||
|
*/
|
||
|
return $array;
|
||
|
EOD
|
||
|
);
|
||
12 years ago
|
if ($bytesWritten <= 0) {
|
||
|
throw new Exception("Unable to write output bundle configuration at '{$bundleFile}'.");
|
||
|
}
|
||
|
echo "Output bundle configuration created at '{$bundleFile}'.\n";
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Compresses given Java Script files and combines them into the single one.
|
||
|
* @param array $inputFiles list of source file names.
|
||
|
* @param string $outputFile output file name.
|
||
12 years ago
|
* @throws \yii\console\Exception on failure
|
||
12 years ago
|
*/
|
||
12 years ago
|
protected function compressJsFiles($inputFiles, $outputFile)
|
||
|
{
|
||
12 years ago
|
if (empty($inputFiles)) {
|
||
|
return;
|
||
|
}
|
||
|
echo " Compressing JavaScript files...\n";
|
||
12 years ago
|
if (is_string($this->jsCompressor)) {
|
||
|
$tmpFile = $outputFile . '.tmp';
|
||
|
$this->combineJsFiles($inputFiles, $tmpFile);
|
||
12 years ago
|
echo shell_exec(strtr($this->jsCompressor, array(
|
||
12 years ago
|
'{from}' => escapeshellarg($tmpFile),
|
||
|
'{to}' => escapeshellarg($outputFile),
|
||
12 years ago
|
)));
|
||
|
@unlink($tmpFile);
|
||
|
} else {
|
||
12 years ago
|
call_user_func($this->jsCompressor, $this, $inputFiles, $outputFile);
|
||
12 years ago
|
}
|
||
12 years ago
|
if (!file_exists($outputFile)) {
|
||
|
throw new Exception("Unable to compress JavaScript files into '{$outputFile}'.");
|
||
|
}
|
||
|
echo " JavaScript files compressed into '{$outputFile}'.\n";
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Compresses given CSS files and combines them into the single one.
|
||
|
* @param array $inputFiles list of source file names.
|
||
|
* @param string $outputFile output file name.
|
||
12 years ago
|
* @throws \yii\console\Exception on failure
|
||
12 years ago
|
*/
|
||
12 years ago
|
protected function compressCssFiles($inputFiles, $outputFile)
|
||
|
{
|
||
12 years ago
|
if (empty($inputFiles)) {
|
||
|
return;
|
||
|
}
|
||
|
echo " Compressing CSS files...\n";
|
||
12 years ago
|
if (is_string($this->cssCompressor)) {
|
||
|
$tmpFile = $outputFile . '.tmp';
|
||
|
$this->combineCssFiles($inputFiles, $tmpFile);
|
||
12 years ago
|
echo shell_exec(strtr($this->cssCompressor, array(
|
||
12 years ago
|
'{from}' => escapeshellarg($tmpFile),
|
||
|
'{to}' => escapeshellarg($outputFile),
|
||
12 years ago
|
)));
|
||
12 years ago
|
@unlink($tmpFile);
|
||
12 years ago
|
} else {
|
||
12 years ago
|
call_user_func($this->cssCompressor, $this, $inputFiles, $outputFile);
|
||
12 years ago
|
}
|
||
12 years ago
|
if (!file_exists($outputFile)) {
|
||
|
throw new Exception("Unable to compress CSS files into '{$outputFile}'.");
|
||
|
}
|
||
|
echo " CSS files compressed into '{$outputFile}'.\n";
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Combines Java Script files into a single one.
|
||
|
* @param array $inputFiles source file names.
|
||
|
* @param string $outputFile output file name.
|
||
|
*/
|
||
|
public function combineJsFiles($inputFiles, $outputFile)
|
||
12 years ago
|
{
|
||
|
$content = '';
|
||
12 years ago
|
foreach ($inputFiles as $file) {
|
||
12 years ago
|
$content .= "/*** BEGIN FILE: $file ***/\n"
|
||
|
. file_get_contents($file)
|
||
|
. "/*** END FILE: $file ***/\n";
|
||
|
}
|
||
12 years ago
|
file_put_contents($outputFile, $content);
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Combines CSS files into a single one.
|
||
|
* @param array $inputFiles source file names.
|
||
|
* @param string $outputFile output file name.
|
||
|
*/
|
||
|
public function combineCssFiles($inputFiles, $outputFile)
|
||
12 years ago
|
{
|
||
|
// todo: adjust url() references in CSS files
|
||
|
$content = '';
|
||
12 years ago
|
foreach ($inputFiles as $file) {
|
||
12 years ago
|
$content .= "/*** BEGIN FILE: $file ***/\n"
|
||
|
. file_get_contents($file)
|
||
|
. "/*** END FILE: $file ***/\n";
|
||
|
}
|
||
12 years ago
|
file_put_contents($outputFile, $content);
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Creates template of configuration file for [[actionCompress]].
|
||
|
* @param string $configFile output file name.
|
||
|
*/
|
||
12 years ago
|
public function actionTemplate($configFile)
|
||
|
{
|
||
|
$template = <<<EOD
|
||
|
<?php
|
||
|
|
||
|
return array(
|
||
|
//
|
||
|
'bundles' => require('path/to/bundles.php'),
|
||
|
//
|
||
|
'extensions' => require('path/to/namespaces.php'),
|
||
|
//
|
||
|
'targets' => array(
|
||
|
'all' => array(
|
||
|
'basePath' => __DIR__,
|
||
|
'baseUrl' => '/test',
|
||
|
'js' => 'all-{ts}.js',
|
||
|
'css' => 'all-{ts}.css',
|
||
|
),
|
||
|
),
|
||
12 years ago
|
|
||
12 years ago
|
'assetManager' => array(
|
||
|
'basePath' => __DIR__,
|
||
|
'baseUrl' => '/test',
|
||
|
),
|
||
|
);
|
||
|
EOD;
|
||
12 years ago
|
if (file_exists($configFile)) {
|
||
|
if (!$this->confirm("File '{$configFile}' already exists. Do you wish to overwrite it?")) {
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
$bytesWritten = file_put_contents($configFile, $template);
|
||
|
if ($bytesWritten<=0) {
|
||
|
echo "Error: unable to write file '{$configFile}'!\n\n";
|
||
|
} else {
|
||
|
echo "Configuration file template created at '{$configFile}'.\n\n";
|
||
|
}
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|