Browse Source

Fixed bug in yiic.php.

Refactoring AssetConverter.
tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
304122ebe4
  1. 1
      framework/console/Application.php
  2. 26
      framework/console/controllers/ScriptController.php
  3. 27
      framework/web/AssetConverter.php
  4. 10
      framework/web/IAssetConverter.php
  5. 7
      framework/yiic.php

1
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',
);
}

26
framework/console/controllers/ScriptController.php

@ -0,0 +1,26 @@
<?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)
{
}
}

27
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 <qiang.xue@gmail.com>
* @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";

10
framework/web/IAssetConverter.php

@ -8,10 +8,20 @@
namespace yii\web;
/**
* The IAssetConverter interface must be implemented by asset converter classes.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @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);
}

7
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();

Loading…
Cancel
Save