Commit 304122eb by Qiang Xue

Fixed bug in yiic.php.

Refactoring AssetConverter.
parent c7cf1026
...@@ -129,6 +129,7 @@ class Application extends \yii\base\Application ...@@ -129,6 +129,7 @@ class Application extends \yii\base\Application
'migrate' => 'yii\console\controllers\MigrateController', 'migrate' => 'yii\console\controllers\MigrateController',
'app' => 'yii\console\controllers\AppController', 'app' => 'yii\console\controllers\AppController',
'cache' => 'yii\console\controllers\CacheController', 'cache' => 'yii\console\controllers\CacheController',
'script' => 'yii\console\controllers\ScriptController',
); );
} }
......
<?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;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ScriptController extends Controller
{
public $defaultAction = 'combo';
public function actionCombo($configFile)
{
}
}
\ No newline at end of file
...@@ -11,18 +11,32 @@ use Yii; ...@@ -11,18 +11,32 @@ use Yii;
use yii\base\Component; use yii\base\Component;
/** /**
* AssetConverter supports conversion of several popular script formats into JS or CSS scripts.
*
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class AssetConverter extends Component implements IAssetConverter class AssetConverter extends Component implements IAssetConverter
{ {
/**
* @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.
*/
public $commands = array( public $commands = array(
'less' => array('css', 'lessc %s %s'), 'less' => array('css', 'lessc {from} {to}'),
'scss' => array('css', 'sass %s %s'), 'scss' => array('css', 'sass {from} {to}'),
'sass' => array('css', 'sass %s %s'), 'sass' => array('css', 'sass {from} {to}'),
'styl' => array('js', 'stylus < %s > %s'), 'styl' => array('js', 'stylus < {from} > {to}'),
); );
/**
* 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.
*/
public function convert($asset, $basePath, $baseUrl) public function convert($asset, $basePath, $baseUrl)
{ {
$pos = strrpos($asset, '.'); $pos = strrpos($asset, '.');
...@@ -33,7 +47,10 @@ class AssetConverter extends Component implements IAssetConverter ...@@ -33,7 +47,10 @@ class AssetConverter extends Component implements IAssetConverter
$result = substr($asset, 0, $pos + 1) . $ext; $result = substr($asset, 0, $pos + 1) . $ext;
if (@filemtime("$basePath/$result") < filemtime("$basePath/$asset")) { if (@filemtime("$basePath/$result") < filemtime("$basePath/$asset")) {
$output = array(); $output = array();
$command = sprintf($command, "$basePath/$asset", "$basePath/$result"); $command = strtr($command, array(
'{from}' => "$basePath/$asset",
'{to}' => "$basePath/$result",
));
exec($command, $output); exec($command, $output);
Yii::info("Converted $asset into $result: " . implode("\n", $output), __METHOD__); Yii::info("Converted $asset into $result: " . implode("\n", $output), __METHOD__);
return "$baseUrl/$result"; return "$baseUrl/$result";
......
...@@ -8,10 +8,20 @@ ...@@ -8,10 +8,20 @@
namespace yii\web; namespace yii\web;
/** /**
* The IAssetConverter interface must be implemented by asset converter classes.
*
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
interface IAssetConverter interface IAssetConverter
{ {
/**
* 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. If the given asset does not
* need conversion, "$baseUrl/$asset" should be returned.
*/
public function convert($asset, $basePath, $baseUrl); public function convert($asset, $basePath, $baseUrl);
} }
\ No newline at end of file
...@@ -14,10 +14,9 @@ defined('STDIN') or define('STDIN', fopen('php://stdin', 'r')); ...@@ -14,10 +14,9 @@ defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
require(__DIR__ . '/yii.php'); require(__DIR__ . '/yii.php');
$id = 'yiic'; $application = new yii\console\Application(array(
$basePath = __DIR__ . '/console'; 'id' => 'yiic',
'basePath' => __DIR__ . '/console',
$application = new yii\console\Application($id, $basePath, array(
'controllerPath' => '@yii/console/controllers', 'controllerPath' => '@yii/console/controllers',
)); ));
$application->run(); $application->run();
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