Commit 30e6d28b by Qiang Xue

Finished asset converter.

parent 767a78f8
...@@ -39,6 +39,7 @@ class ViewContent extends Component ...@@ -39,6 +39,7 @@ class ViewContent extends Component
public function reset() public function reset()
{ {
$this->assetBundles = null;
$this->title = null; $this->title = null;
$this->metaTags = null; $this->metaTags = null;
$this->linkTags = null; $this->linkTags = null;
......
...@@ -122,11 +122,13 @@ class AssetBundle extends Object ...@@ -122,11 +122,13 @@ class AssetBundle extends Object
list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions); list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
} }
$converter = $am->getConverter();
foreach ($this->js as $js => $options) { foreach ($this->js as $js => $options) {
$js = is_string($options) ? $options : $js; $js = is_string($options) ? $options : $js;
if (strpos($js, '/') !== 0 && strpos($js, '://') === false) { if (strpos($js, '/') !== 0 && strpos($js, '://') === false) {
if (isset($this->basePath, $this->baseUrl)) { if (isset($this->basePath, $this->baseUrl)) {
$js = $am->processAsset(ltrim($js, '/'), $this->basePath, $this->baseUrl); $js = $converter->convert(ltrim($js, '/'), $this->basePath, $this->baseUrl);
} else { } else {
throw new InvalidConfigException('Both of the "baseUrl" and "basePath" properties must be set.'); throw new InvalidConfigException('Both of the "baseUrl" and "basePath" properties must be set.');
} }
...@@ -137,7 +139,7 @@ class AssetBundle extends Object ...@@ -137,7 +139,7 @@ class AssetBundle extends Object
$css = is_string($options) ? $options : $css; $css = is_string($options) ? $options : $css;
if (strpos($css, '//') !== 0 && strpos($css, '://') === false) { if (strpos($css, '//') !== 0 && strpos($css, '://') === false) {
if (isset($this->basePath, $this->baseUrl)) { if (isset($this->basePath, $this->baseUrl)) {
$css = $am->processAsset(ltrim($css, '/'), $this->basePath, $this->baseUrl); $css = $converter->convert(ltrim($css, '/'), $this->basePath, $this->baseUrl);
} else { } else {
throw new InvalidConfigException('Both of the "baseUrl" and "basePath" properties must be set.'); throw new InvalidConfigException('Both of the "baseUrl" and "basePath" properties must be set.');
} }
......
<?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;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class AssetConverter extends Component implements IAssetConverter
{
public $commands = array(
'less' => array('css', 'lessc %s %s'),
'scss' => array('css', 'sass %s %s'),
'sass' => array('css', 'sass %s %s'),
'styl' => array('js', 'stylus < %s > %s'),
);
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();
$command = sprintf($command, "$basePath/$asset", "$basePath/$result");
exec($command, $output);
Yii::info("Converted $asset into $result: " . implode("\n", $output), __METHOD__);
return "$baseUrl/$result";
}
}
}
return "$baseUrl/$asset";
}
}
\ No newline at end of file
...@@ -26,13 +26,6 @@ class AssetManager extends Component ...@@ -26,13 +26,6 @@ class AssetManager extends Component
*/ */
public $bundles; public $bundles;
/** /**
* @var array list of asset processors. An asset processor will convert a special type of asset files
* (e.g. LESS, Sass, TypeScript) into JS or CSS files. The array keys are the file extension names
* (e.g. "less", "sass", "ts"), and the array values are the corresponding configuration arrays
* for creating the processor objects.
*/
public $processors;
/**
* @return string the root directory storing the published asset files. * @return string the root directory storing the published asset files.
*/ */
public $basePath = '@wwwroot/assets'; public $basePath = '@wwwroot/assets';
...@@ -139,26 +132,26 @@ class AssetManager extends Component ...@@ -139,26 +132,26 @@ class AssetManager extends Component
return $this->bundles[$name]; return $this->bundles[$name];
} }
private $_converter;
/** /**
* Processes the given asset file and returns a URL to the processed one. * @return IAssetConverter
* This method can be overwritten to support various types of asset files, such as LESS, Sass, TypeScript.
* @param string $asset the asset file path to be processed. The file path is relative
* to $basePath, and it may contain forward slashes to indicate sub-directories (e.g. "js/main.js").
* @param string $basePath the directory that contains the asset file.
* @param string $baseUrl the corresponding URL of $basePath.
* @return string the processed asset file path.
*/ */
public function processAsset($asset, $basePath, $baseUrl) public function getConverter()
{ {
$ext = pathinfo($asset, PATHINFO_EXTENSION); if ($this->_converter === null) {
if (isset($this->processors[$ext])) { $this->_converter = Yii::createObject(array(
if (is_array($this->processors[$ext])) { 'class' => 'yii\\web\\AssetConverter',
$this->processors[$ext] = Yii::createObject($this->processors[$ext]); ));
} elseif (is_array($this->_converter) || is_string($this->_converter)) {
$this->_converter = Yii::createObject($this->_converter);
} }
return $this->processors[$ext]->process($asset, $basePath, $baseUrl); return $this->_converter;
} else {
return $baseUrl . '/' . $asset;
} }
public function setConverter($value)
{
$this->_converter = $value;
} }
/** /**
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
interface IAssetConverter
{
public function convert($asset, $basePath, $baseUrl);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment