From 304122ebe4e4ffaba958ec6b27012724cb3b1232 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 19 Apr 2013 20:44:59 -0400 Subject: [PATCH] Fixed bug in yiic.php. Refactoring AssetConverter. --- framework/console/Application.php | 1 + framework/console/controllers/ScriptController.php | 26 +++++++++++++++++++++ framework/web/AssetConverter.php | 27 ++++++++++++++++++---- framework/web/IAssetConverter.php | 10 ++++++++ framework/yiic.php | 7 +++--- 5 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 framework/console/controllers/ScriptController.php diff --git a/framework/console/Application.php b/framework/console/Application.php index 574495b..e185cc5 100644 --- a/framework/console/Application.php +++ b/framework/console/Application.php @@ -129,6 +129,7 @@ class Application extends \yii\base\Application 'migrate' => 'yii\console\controllers\MigrateController', 'app' => 'yii\console\controllers\AppController', 'cache' => 'yii\console\controllers\CacheController', + 'script' => 'yii\console\controllers\ScriptController', ); } diff --git a/framework/console/controllers/ScriptController.php b/framework/console/controllers/ScriptController.php new file mode 100644 index 0000000..7ca498b --- /dev/null +++ b/framework/console/controllers/ScriptController.php @@ -0,0 +1,26 @@ + + * @since 2.0 + */ +class ScriptController extends Controller +{ + public $defaultAction = 'combo'; + + public function actionCombo($configFile) + { + + } +} \ No newline at end of file diff --git a/framework/web/AssetConverter.php b/framework/web/AssetConverter.php index 26cc59b..8340be5 100644 --- a/framework/web/AssetConverter.php +++ b/framework/web/AssetConverter.php @@ -11,18 +11,32 @@ use Yii; use yii\base\Component; /** + * AssetConverter supports conversion of several popular script formats into JS or CSS scripts. + * * @author Qiang Xue * @since 2.0 */ 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( - 'less' => array('css', 'lessc %s %s'), - 'scss' => array('css', 'sass %s %s'), - 'sass' => array('css', 'sass %s %s'), - 'styl' => array('js', 'stylus < %s > %s'), + 'less' => array('css', 'lessc {from} {to}'), + 'scss' => array('css', 'sass {from} {to}'), + 'sass' => array('css', 'sass {from} {to}'), + '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) { $pos = strrpos($asset, '.'); @@ -33,7 +47,10 @@ class AssetConverter extends Component implements IAssetConverter $result = substr($asset, 0, $pos + 1) . $ext; if (@filemtime("$basePath/$result") < filemtime("$basePath/$asset")) { $output = array(); - $command = sprintf($command, "$basePath/$asset", "$basePath/$result"); + $command = strtr($command, array( + '{from}' => "$basePath/$asset", + '{to}' => "$basePath/$result", + )); exec($command, $output); Yii::info("Converted $asset into $result: " . implode("\n", $output), __METHOD__); return "$baseUrl/$result"; diff --git a/framework/web/IAssetConverter.php b/framework/web/IAssetConverter.php index 994cb2f..4334d3e 100644 --- a/framework/web/IAssetConverter.php +++ b/framework/web/IAssetConverter.php @@ -8,10 +8,20 @@ namespace yii\web; /** + * The IAssetConverter interface must be implemented by asset converter classes. + * * @author Qiang Xue * @since 2.0 */ 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); } \ No newline at end of file diff --git a/framework/yiic.php b/framework/yiic.php index 0db69bb..3872e2f 100644 --- a/framework/yiic.php +++ b/framework/yiic.php @@ -14,10 +14,9 @@ defined('STDIN') or define('STDIN', fopen('php://stdin', 'r')); require(__DIR__ . '/yii.php'); -$id = 'yiic'; -$basePath = __DIR__ . '/console'; - -$application = new yii\console\Application($id, $basePath, array( +$application = new yii\console\Application(array( + 'id' => 'yiic', + 'basePath' => __DIR__ . '/console', 'controllerPath' => '@yii/console/controllers', )); $application->run();