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.
62 lines
1.8 KiB
62 lines
1.8 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\web;
|
||
|
|
||
|
use Yii;
|
||
|
use yii\base\Component;
|
||
|
|
||
|
/**
|
||
12 years ago
|
* AssetConverter supports conversion of several popular script formats into JS or CSS scripts.
|
||
|
*
|
||
12 years ago
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class AssetConverter extends Component implements IAssetConverter
|
||
|
{
|
||
12 years ago
|
/**
|
||
|
* @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.
|
||
|
*/
|
||
12 years ago
|
public $commands = array(
|
||
12 years ago
|
'less' => array('css', 'lessc {from} {to}'),
|
||
|
'scss' => array('css', 'sass {from} {to}'),
|
||
|
'sass' => array('css', 'sass {from} {to}'),
|
||
|
'styl' => array('js', 'stylus < {from} > {to}'),
|
||
12 years ago
|
);
|
||
|
|
||
12 years ago
|
/**
|
||
|
* 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.
|
||
12 years ago
|
* @return string the converted asset file path, relative to $basePath.
|
||
12 years ago
|
*/
|
||
12 years ago
|
public function convert($asset, $basePath)
|
||
12 years ago
|
{
|
||
|
$pos = strrpos($asset, '.');
|
||
12 years ago
|
if ($pos === false) {
|
||
12 years ago
|
$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")) {
|
||
|
$output = array();
|
||
12 years ago
|
$command = strtr($command, array(
|
||
|
'{from}' => "$basePath/$asset",
|
||
|
'{to}' => "$basePath/$result",
|
||
|
));
|
||
12 years ago
|
exec($command, $output);
|
||
|
Yii::info("Converted $asset into $result: " . implode("\n", $output), __METHOD__);
|
||
|
}
|
||
12 years ago
|
return $result;
|
||
12 years ago
|
}
|
||
|
}
|
||
12 years ago
|
return $asset;
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|