AssetConverter.php 3.26 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\web;

use Yii;
use yii\base\Component;
12
use yii\base\Exception;
Qiang Xue committed
13 14

/**
Qiang Xue committed
15 16
 * AssetConverter supports conversion of several popular script formats into JS or CSS scripts.
 *
17 18
 * It is used by [[AssetManager]] to convert files after they have been published.
 *
Qiang Xue committed
19 20 21
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
22
class AssetConverter extends Component implements AssetConverterInterface
Qiang Xue committed
23
{
Qiang Xue committed
24 25 26 27 28
	/**
	 * @var array the commands that are used to perform the asset conversion.
	 * The keys are the asset file extension names, and the values are the corresponding
	 * target script types (either "css" or "js") and the commands used for the conversion.
	 */
Alexander Makarov committed
29
	public $commands = [
30
		'less' => ['css', 'lessc {from} {to} --no-color'],
Alexander Makarov committed
31 32 33
		'scss' => ['css', 'sass {from} {to}'],
		'sass' => ['css', 'sass {from} {to}'],
		'styl' => ['js', 'stylus < {from} > {to}'],
34 35
		'coffee' => ['js', 'coffee -p {from} > {to}'],
		'ts' => ['js', 'tsc --out {to} {from}'],
Alexander Makarov committed
36
	];
Qiang Xue committed
37

Qiang Xue committed
38 39 40 41
	/**
	 * Converts a given asset file into a CSS or JS file.
	 * @param string $asset the asset file path, relative to $basePath
	 * @param string $basePath the directory the $asset is relative to.
42
	 * @return string the converted asset file path, relative to $basePath.
Qiang Xue committed
43
	 */
44
	public function convert($asset, $basePath)
Qiang Xue committed
45 46
	{
		$pos = strrpos($asset, '.');
47
		if ($pos !== false) {
Qiang Xue committed
48 49 50 51 52
			$ext = substr($asset, $pos + 1);
			if (isset($this->commands[$ext])) {
				list ($ext, $command) = $this->commands[$ext];
				$result = substr($asset, 0, $pos + 1) . $ext;
				if (@filemtime("$basePath/$result") < filemtime("$basePath/$asset")) {
53
					$this->runCommand($command, $basePath, $asset, $result);
Qiang Xue committed
54
				}
55
				return $result;
Qiang Xue committed
56 57
			}
		}
58
		return $asset;
Qiang Xue committed
59
	}
60 61 62 63 64 65 66 67

	/**
	 * Runs a command to convert asset files.
	 * @param string $command the command to run
	 * @param string $basePath asset base path and command working directory
	 * @param string $asset the name of the asset file
	 * @param string $result the name of the file to be generated by the converter command
	 * @return bool true on success, false on failure. Failures will be logged.
68 69
	 * @throws \yii\base\Exception when the command fails and YII_DEBUG is true.
	 * In production mode the error will be logged.
70 71 72 73 74 75 76
	 */
	protected function runCommand($command, $basePath, $asset, $result)
	{
		$command = strtr($command, [
			'{from}' => escapeshellarg("$basePath/$asset"),
			'{to}' => escapeshellarg("$basePath/$result"),
		]);
Qiang Xue committed
77 78 79 80 81
		$descriptor = [
			1 => ['pipe', 'w'],
			2 => ['pipe', 'w'],
		];
		$pipes = [];
82 83 84 85 86 87 88 89
		$proc = proc_open($command, $descriptor, $pipes, $basePath);
		$stdout = stream_get_contents($pipes[1]);
		$stderr = stream_get_contents($pipes[2]);
		foreach($pipes as $pipe) {
			fclose($pipe);
		}
		$status = proc_close($proc);

90
		if ($status === 0) {
91
			Yii::trace("Converted $asset into $result:\nSTDOUT:\n$stdout\nSTDERR:\n$stderr", __METHOD__);
92 93 94 95
		} elseif (YII_DEBUG) {
			throw new Exception("AssetConverter command '$command' failed with exit code $status:\nSTDOUT:\n$stdout\nSTDERR:\n$stderr");
		} else {
			Yii::error("AssetConverter command '$command' failed with exit code $status:\nSTDOUT:\n$stdout\nSTDERR:\n$stderr");
96 97 98
		}
		return $status === 0;
	}
Zander Baldwin committed
99
}