AssetController.php 19.9 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?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;

/**
15 16
 * This command allows you to combine and compress your JavaScript and CSS files.
 *
17 18 19 20 21 22 23 24
 * Usage:
 * 1. Create a configuration file using '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.
 *
25
 * Note: in the console environment some path aliases like '@webroot' and '@web' may not exist,
26 27 28 29 30
 * 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.
 *
31 32
 * @property array|\yii\web\AssetManager $assetManager asset manager, which will be used for assets processing.
 *
Qiang Xue committed
33 34 35
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
36
class AssetController extends Controller
Qiang Xue committed
37
{
38 39 40
	/**
	 * @var string controller default action ID.
	 */
Qiang Xue committed
41
	public $defaultAction = 'compress';
42 43 44
	/**
	 * @var array list of asset bundles to be compressed.
	 */
Qiang Xue committed
45 46
	public $bundles = array();
	/**
47 48 49
	 * @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:
Qiang Xue committed
50
	 *
Qiang Xue committed
51
	 * ~~~
Qiang Xue committed
52 53 54
	 * 'app\config\AllAsset' => array(
	 *     'js' => 'js/all-{ts}.js',
	 *     'css' => 'css/all-{ts}.css',
Qiang Xue committed
55 56 57
	 *     'depends' => array( ... ),
	 * )
	 * ~~~
Qiang Xue committed
58
	 *
59 60
	 * File names can contain placeholder "{ts}", which will be filled by current timestamp, while
	 * file creation.
Qiang Xue committed
61 62
	 */
	public $targets = array();
63
	/**
64
	 * @var string|callback JavaScript file compressor.
65 66
	 * If a string, it is treated as shell command template, which should contain
	 * placeholders {from} - source file name - and {to} - output file name.
67
	 * Otherwise, it is treated as PHP callback, which should perform the compression.
68 69 70 71
	 *
	 * Default value relies on usage of "Closure Compiler"
	 * @see https://developers.google.com/closure/compiler/
	 */
72
	public $jsCompressor = 'java -jar compiler.jar --js {from} --js_output_file {to}';
73 74 75 76
	/**
	 * @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.
77
	 * Otherwise, it is treated as PHP callback, which should perform the compression.
78 79 80 81
	 *
	 * Default value relies on usage of "YUI Compressor"
	 * @see https://github.com/yui/yuicompressor/
	 */
82
	public $cssCompressor = 'java -jar yuicompressor.jar {from} -o {to}';
Qiang Xue committed
83

Qiang Xue committed
84 85 86 87 88 89
	/**
	 * @var array|\yii\web\AssetManager [[yii\web\AssetManager]] instance or its array configuration, which will be used
	 * for assets processing.
	 */
	private $_assetManager = array();

90
	/**
91 92
	 * Returns the asset manager instance.
	 * @throws \yii\console\Exception on invalid configuration.
93 94 95 96 97 98 99 100 101
	 * @return \yii\web\AssetManager asset manager instance.
	 */
	public function getAssetManager()
	{
		if (!is_object($this->_assetManager)) {
			$options = $this->_assetManager;
			if (!isset($options['class'])) {
				$options['class'] = 'yii\\web\\AssetManager';
			}
102 103 104 105 106 107
			if (!isset($options['basePath'])) {
				throw new Exception("Please specify 'basePath' for the 'assetManager' option.");
			}
			if (!isset($options['baseUrl'])) {
				throw new Exception("Please specify 'baseUrl' for the 'assetManager' option.");
			}
108 109 110 111 112 113
			$this->_assetManager = Yii::createObject($options);
		}
		return $this->_assetManager;
	}

	/**
114
	 * Sets asset manager instance or configuration.
115 116 117 118 119 120 121 122 123 124 125
	 * @param \yii\web\AssetManager|array $assetManager asset manager instance or its array configuration.
	 * @throws \yii\console\Exception on invalid argument type.
	 */
	public function setAssetManager($assetManager)
	{
		if (is_scalar($assetManager)) {
			throw new Exception('"' . get_class($this) . '::assetManager" should be either object or array - "' . gettype($assetManager) . '" given.');
		}
		$this->_assetManager = $assetManager;
	}

126
	/**
127
	 * Combines and compresses the asset files according to the given configuration.
128 129
	 * 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.
130
	 * @param string $configFile configuration file name.
131
	 * @param string $bundleFile output asset bundles configuration file name.
132
	 */
Qiang Xue committed
133 134 135
	public function actionCompress($configFile, $bundleFile)
	{
		$this->loadConfiguration($configFile);
Qiang Xue committed
136
		$bundles = $this->loadBundles($this->bundles);
Qiang Xue committed
137
		$targets = $this->loadTargets($this->targets, $bundles);
138
		$this->publishBundles($bundles, $this->assetManager);
Qiang Xue committed
139
		$timestamp = time();
140 141
		foreach ($targets as $name => $target) {
			echo "Creating output bundle '{$name}':\n";
Qiang Xue committed
142
			if (!empty($target->js)) {
Qiang Xue committed
143 144
				$this->buildTarget($target, 'js', $bundles, $timestamp);
			}
Qiang Xue committed
145
			if (!empty($target->css)) {
Qiang Xue committed
146 147
				$this->buildTarget($target, 'css', $bundles, $timestamp);
			}
148
			echo "\n";
Qiang Xue committed
149 150 151
		}

		$targets = $this->adjustDependency($targets, $bundles);
Qiang Xue committed
152
		$this->saveTargets($targets, $bundleFile);
Qiang Xue committed
153 154
	}

155 156 157 158 159
	/**
	 * Applies configuration from the given file to self instance.
	 * @param string $configFile configuration file name.
	 * @throws \yii\console\Exception on failure.
	 */
Qiang Xue committed
160 161
	protected function loadConfiguration($configFile)
	{
162
		echo "Loading configuration from '{$configFile}'...\n";
Qiang Xue committed
163
		foreach (require($configFile) as $name => $value) {
164
			if (property_exists($this, $name) || $this->canSetProperty($name)) {
Qiang Xue committed
165 166
				$this->$name = $value;
			} else {
Qiang Xue committed
167
				throw new Exception("Unknown configuration option: $name");
Qiang Xue committed
168 169 170
			}
		}

171
		$this->getAssetManager(); // check if asset manager configuration is correct
Qiang Xue committed
172 173
	}

174
	/**
175
	 * Creates full list of source asset bundles.
Qiang Xue committed
176
	 * @param string[] $bundles list of asset bundle names
177
	 * @return \yii\web\AssetBundle[] list of source asset bundles.
178
	 */
Qiang Xue committed
179
	protected function loadBundles($bundles)
Qiang Xue committed
180
	{
181
		echo "Collecting source bundles information...\n";
182

Qiang Xue committed
183
		$am = $this->getAssetManager();
Qiang Xue committed
184
		$result = array();
Qiang Xue committed
185 186
		foreach ($bundles as $name) {
			$result[$name] = $am->getBundle($name);
Qiang Xue committed
187
		}
Qiang Xue committed
188 189
		foreach ($result as $bundle) {
			$this->loadDependency($bundle, $result);
190 191
		}

Qiang Xue committed
192 193 194
		return $result;
	}

195 196 197 198
	/**
	 * Loads asset bundle dependencies recursively.
	 * @param \yii\web\AssetBundle $bundle bundle instance
	 * @param array $result already loaded bundles list.
Qiang Xue committed
199
	 * @throws Exception on failure.
200
	 */
Qiang Xue committed
201
	protected function loadDependency($bundle, &$result)
resurtm committed
202
	{
Qiang Xue committed
203 204 205 206 207 208 209 210 211
		$am = $this->getAssetManager();
		foreach ($bundle->depends as $name) {
			if (!isset($result[$name])) {
				$dependencyBundle = $am->getBundle($name);
				$result[$name] = false;
				$this->loadDependency($dependencyBundle, $result);
				$result[$name] = $dependencyBundle;
			} elseif ($result[$name] === false) {
				throw new Exception("A circular dependency is detected for bundle '$name'.");
212 213 214 215
			}
		}
	}

216
	/**
217 218 219 220
	 * 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.
Qiang Xue committed
221
	 * @throws Exception on failure.
222
	 */
Qiang Xue committed
223 224
	protected function loadTargets($targets, $bundles)
	{
225
		// build the dependency order of bundles
Qiang Xue committed
226 227 228 229 230
		$registered = array();
		foreach ($bundles as $name => $bundle) {
			$this->registerBundle($bundles, $name, $registered);
		}
		$bundleOrders = array_combine(array_keys($registered), range(0, count($bundles) - 1));
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

		// 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
Qiang Xue committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270
		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;
				}
			});
Qiang Xue committed
271
			$target['class'] = $name;
Qiang Xue committed
272 273 274 275 276
			$targets[$name] = Yii::createObject($target);
		}
		return $targets;
	}

Qiang Xue committed
277
	/**
278 279
	 * Publishes given asset bundles.
	 * @param \yii\web\AssetBundle[] $bundles asset bundles to be published.
Qiang Xue committed
280
	 */
281
	protected function publishBundles($bundles)
Qiang Xue committed
282
	{
283
		echo "\nPublishing bundles:\n";
284
		$assetManager = $this->getAssetManager();
285
		foreach ($bundles as $name => $bundle) {
286
			$bundle->publish($assetManager);
287
			echo "  '".$name."' published.\n";
Qiang Xue committed
288
		}
289
		echo "\n";
Qiang Xue committed
290 291 292
	}

	/**
293 294
	 * Builds output asset bundle.
	 * @param \yii\web\AssetBundle $target output asset bundle
295
	 * @param string $type either 'js' or 'css'.
296 297 298
	 * @param \yii\web\AssetBundle[] $bundles source asset bundles.
	 * @param integer $timestamp current timestamp.
	 * @throws Exception on failure.
Qiang Xue committed
299
	 */
Qiang Xue committed
300
	protected function buildTarget($target, $type, $bundles, $timestamp)
Qiang Xue committed
301
	{
Qiang Xue committed
302
		$outputFile = strtr($target->$type, array(
Qiang Xue committed
303 304 305
			'{ts}' => $timestamp,
		));
		$inputFiles = array();
Qiang Xue committed
306 307

		foreach ($target->depends as $name) {
Qiang Xue committed
308 309
			if (isset($bundles[$name])) {
				foreach ($bundles[$name]->$type as $file) {
310
					$inputFiles[] = $bundles[$name]->basePath . '/' . $file;
Qiang Xue committed
311 312
				}
			} else {
313
				throw new Exception("Unknown bundle: '{$name}'");
Qiang Xue committed
314 315 316
			}
		}
		if ($type === 'js') {
Qiang Xue committed
317
			$this->compressJsFiles($inputFiles, $target->basePath . '/' . $outputFile);
Qiang Xue committed
318
		} else {
Qiang Xue committed
319
			$this->compressCssFiles($inputFiles, $target->basePath . '/' . $outputFile);
Qiang Xue committed
320
		}
Qiang Xue committed
321
		$target->$type = array($outputFile);
Qiang Xue committed
322 323
	}

324
	/**
325 326 327 328
	 * 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.
329
	 */
Qiang Xue committed
330 331
	protected function adjustDependency($targets, $bundles)
	{
332 333
		echo "Creating new bundle configuration...\n";

Qiang Xue committed
334 335 336
		$map = array();
		foreach ($targets as $name => $target) {
			foreach ($target->depends as $bundle) {
337
				$map[$bundle] = $name;
Qiang Xue committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
			}
		}

		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),
			));
		}
Qiang Xue committed
364 365 366
		return $targets;
	}

367 368 369 370 371
	/**
	 * 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.
Qiang Xue committed
372
	 * @throws Exception if circular dependency is detected.
373
	 */
Qiang Xue committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
	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'.");
		}
	}

389 390 391 392
	/**
	 * Saves new asset bundles configuration.
	 * @param \yii\web\AssetBundle[] $targets list of asset bundles to be saved.
	 * @param string $bundleFile output file name.
393
	 * @throws \yii\console\Exception on failure.
394
	 */
Qiang Xue committed
395 396 397 398 399 400 401 402 403 404 405 406
	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());
407
		$bundleFileContent = <<<EOD
Qiang Xue committed
408 409
<?php
/**
410
 * This file is generated by the "yii {$this->id}" command.
411
 * DO NOT MODIFY THIS FILE DIRECTLY.
412
 * @version {$version}
Qiang Xue committed
413
 */
414
return {$array};
415 416
EOD;
		if (!file_put_contents($bundleFile, $bundleFileContent)) {
417 418 419
			throw new Exception("Unable to write output bundle configuration at '{$bundleFile}'.");
		}
		echo "Output bundle configuration created at '{$bundleFile}'.\n";
Qiang Xue committed
420 421
	}

422
	/**
423
	 * Compresses given JavaScript files and combines them into the single one.
424 425
	 * @param array $inputFiles list of source file names.
	 * @param string $outputFile output file name.
426
	 * @throws \yii\console\Exception on failure
427
	 */
Qiang Xue committed
428 429
	protected function compressJsFiles($inputFiles, $outputFile)
	{
430 431 432 433
		if (empty($inputFiles)) {
			return;
		}
		echo "  Compressing JavaScript files...\n";
434 435 436
		if (is_string($this->jsCompressor)) {
			$tmpFile = $outputFile . '.tmp';
			$this->combineJsFiles($inputFiles, $tmpFile);
437
			echo shell_exec(strtr($this->jsCompressor, array(
438 439
				'{from}' => escapeshellarg($tmpFile),
				'{to}' => escapeshellarg($outputFile),
440 441 442
			)));
			@unlink($tmpFile);
		} else {
443
			call_user_func($this->jsCompressor, $this, $inputFiles, $outputFile);
444
		}
445 446 447 448
		if (!file_exists($outputFile)) {
			throw new Exception("Unable to compress JavaScript files into '{$outputFile}'.");
		}
		echo "  JavaScript files compressed into '{$outputFile}'.\n";
Qiang Xue committed
449 450
	}

451 452 453 454
	/**
	 * 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.
455
	 * @throws \yii\console\Exception on failure
456
	 */
Qiang Xue committed
457 458
	protected function compressCssFiles($inputFiles, $outputFile)
	{
459 460 461 462
		if (empty($inputFiles)) {
			return;
		}
		echo "  Compressing CSS files...\n";
463 464 465
		if (is_string($this->cssCompressor)) {
			$tmpFile = $outputFile . '.tmp';
			$this->combineCssFiles($inputFiles, $tmpFile);
466
			echo shell_exec(strtr($this->cssCompressor, array(
467 468
				'{from}' => escapeshellarg($tmpFile),
				'{to}' => escapeshellarg($outputFile),
469
			)));
470
			@unlink($tmpFile);
471
		} else {
472
			call_user_func($this->cssCompressor, $this, $inputFiles, $outputFile);
473
		}
474 475 476 477
		if (!file_exists($outputFile)) {
			throw new Exception("Unable to compress CSS files into '{$outputFile}'.");
		}
		echo "  CSS files compressed into '{$outputFile}'.\n";
478 479
	}

480
	/**
481
	 * Combines JavaScript files into a single one.
482 483
	 * @param array $inputFiles source file names.
	 * @param string $outputFile output file name.
484
	 * @throws \yii\console\Exception on failure.
485 486
	 */
	public function combineJsFiles($inputFiles, $outputFile)
487 488
	{
		$content = '';
489
		foreach ($inputFiles as $file) {
490 491 492 493
			$content .= "/*** BEGIN FILE: $file ***/\n"
				. file_get_contents($file)
				. "/*** END FILE: $file ***/\n";
		}
494
		if (!file_put_contents($outputFile, $content)) {
495
			throw new Exception("Unable to write output JavaScript file '{$outputFile}'.");
496
		}
497 498
	}

499 500 501 502
	/**
	 * Combines CSS files into a single one.
	 * @param array $inputFiles source file names.
	 * @param string $outputFile output file name.
503
	 * @throws \yii\console\Exception on failure.
504 505
	 */
	public function combineCssFiles($inputFiles, $outputFile)
506 507
	{
		$content = '';
508
		foreach ($inputFiles as $file) {
509
			$content .= "/*** BEGIN FILE: $file ***/\n"
510
				. $this->adjustCssUrl(file_get_contents($file), dirname($file), dirname($outputFile))
511 512
				. "/*** END FILE: $file ***/\n";
		}
513 514 515
		if (!file_put_contents($outputFile, $content)) {
			throw new Exception("Unable to write output CSS file '{$outputFile}'.");
		}
516 517
	}

518 519 520 521 522 523 524 525 526
	/**
	 * Adjusts CSS content allowing URL references pointing to the original resources.
	 * @param string $cssContent source CSS content.
	 * @param string $inputFilePath input CSS file name.
	 * @param string $outputFilePath output CSS file name.
	 * @return string adjusted CSS content.
	 */
	protected function adjustCssUrl($cssContent, $inputFilePath, $outputFilePath)
	{
527 528 529 530 531 532 533 534 535
		$sharedPathParts = array();
		$inputFilePathParts = explode('/', $inputFilePath);
		$inputFilePathPartsCount = count($inputFilePathParts);
		$outputFilePathParts = explode('/', $outputFilePath);
		$outputFilePathPartsCount = count($outputFilePathParts);
		for ($i =0; $i < $inputFilePathPartsCount && $i < $outputFilePathPartsCount; $i++) {
			if ($inputFilePathParts[$i] == $outputFilePathParts[$i]) {
				$sharedPathParts[] = $inputFilePathParts[$i];
			} else {
536 537 538
				break;
			}
		}
539 540
		$sharedPath = implode('/', $sharedPathParts);

541 542 543 544 545
		$inputFileRelativePath = trim(str_replace($sharedPath, '', $inputFilePath), '/');
		$outputFileRelativePath = trim(str_replace($sharedPath, '', $outputFilePath), '/');
		$inputFileRelativePathParts = explode('/', $inputFileRelativePath);
		$outputFileRelativePathParts = explode('/', $outputFileRelativePath);

resurtm committed
546
		$callback = function ($matches) use ($inputFileRelativePathParts, $outputFileRelativePathParts) {
547 548 549
			$fullMatch = $matches[0];
			$inputUrl = $matches[1];

550 551 552 553
			if (preg_match('/https?:\/\//is', $inputUrl)) {
				return $fullMatch;
			}

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
			$outputUrlParts = array_fill(0, count($outputFileRelativePathParts), '..');
			$outputUrlParts = array_merge($outputUrlParts, $inputFileRelativePathParts);

			if (strpos($inputUrl, '/') !== false) {
				$inputUrlParts = explode('/', $inputUrl);
				foreach ($inputUrlParts as $key => $inputUrlPart) {
					if ($inputUrlPart == '..') {
						array_pop($outputUrlParts);
						unset($inputUrlParts[$key]);
					}
				}
				$outputUrlParts[] = implode('/', $inputUrlParts);
			} else {
				$outputUrlParts[] = $inputUrl;
			}
			$outputUrl = implode('/', $outputUrlParts);
570 571

			return str_replace($inputUrl, $outputUrl, $fullMatch);
572
		};
573

574
		$cssContent = preg_replace_callback('/url\(["\']?([^"]*)["\']?\)/is', $callback, $cssContent);
575 576 577 578

		return $cssContent;
	}

579 580 581
	/**
	 * Creates template of configuration file for [[actionCompress]].
	 * @param string $configFile output file name.
582
	 * @throws \yii\console\Exception on failure.
583
	 */
584 585 586 587
	public function actionTemplate($configFile)
	{
		$template = <<<EOD
<?php
588 589
/**
 * Configuration file for the "yii asset" console command.
590
 * Note that in the console environment, some path aliases like '@webroot' and '@web' may not exist.
Qiang Xue committed
591
 * Please define these missing path aliases.
592
 */
593
return array(
594
	// The list of asset bundles to compress:
Qiang Xue committed
595 596 597 598
	'bundles' => array(
		// 'yii\web\YiiAsset',
		// 'yii\web\JqueryAsset',
	),
599
	// Asset bundle for compression output:
600
	'targets' => array(
Qiang Xue committed
601
		'app\config\AllAsset' => array(
602
			'basePath' => 'path/to/web',
Qiang Xue committed
603 604 605
			'baseUrl' => '',
			'js' => 'js/all-{ts}.js',
			'css' => 'css/all-{ts}.css',
606 607
		),
	),
608
	// Asset manager configuration:
609 610
	'assetManager' => array(
		'basePath' => __DIR__,
Qiang Xue committed
611
		'baseUrl' => '',
612 613 614
	),
);
EOD;
615 616 617 618 619
		if (file_exists($configFile)) {
			if (!$this->confirm("File '{$configFile}' already exists. Do you wish to overwrite it?")) {
				return;
			}
		}
620 621
		if (!file_put_contents($configFile, $template)) {
			throw new Exception("Unable to write template file '{$configFile}'.");
622 623 624
		} else {
			echo "Configuration file template created at '{$configFile}'.\n\n";
		}
Qiang Xue committed
625
	}
Zander Baldwin committed
626
}