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.
		
		
		
		
			
				
					63 lines
				
				1.9 KiB
			
		
		
			
		
	
	
					63 lines
				
				1.9 KiB
			| 
											13 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;
 | ||
|  | 
 | ||
|  | /**
 | ||
| 
											13 years ago
										 |  * AssetConverter supports conversion of several popular script formats into JS or CSS scripts.
 | ||
|  |  *
 | ||
| 
											13 years ago
										 |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
|  | class AssetConverter extends Component implements IAssetConverter
 | ||
|  | {
 | ||
| 
											13 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.
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public $commands = array(
 | ||
| 
											13 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}'),
 | ||
| 
											13 years ago
										 | 	);
 | ||
|  | 
 | ||
| 
											13 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.
 | ||
|  | 	 * @param string $baseUrl the URL corresponding to $basePath
 | ||
|  | 	 * @return string the URL to the converted asset file.
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function convert($asset, $basePath, $baseUrl)
 | ||
|  | 	{
 | ||
|  | 		$pos = strrpos($asset, '.');
 | ||
|  | 		if ($pos !== false) {
 | ||
|  | 			$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();
 | ||
| 
											13 years ago
										 | 					$command = strtr($command, array(
 | ||
|  | 						'{from}' => "$basePath/$asset",
 | ||
|  | 						'{to}' => "$basePath/$result",
 | ||
|  | 					));
 | ||
| 
											13 years ago
										 | 					exec($command, $output);
 | ||
|  | 					Yii::info("Converted $asset into $result: " . implode("\n", $output), __METHOD__);
 | ||
|  | 				}
 | ||
| 
											13 years ago
										 | 				return "$baseUrl/$result";
 | ||
| 
											13 years ago
										 | 			}
 | ||
|  | 		}
 | ||
|  | 		return "$baseUrl/$asset";
 | ||
|  | 	}
 | ||
| 
											13 years ago
										 | }
 |