Commit e1593847 by Qiang Xue

Refactored console commands.

parent 0067e8c9
......@@ -8,6 +8,7 @@
namespace yii\base;
use Yii;
use yii\helpers\Console;
use yii\web\HttpException;
/**
......@@ -510,6 +511,9 @@ abstract class Application extends Module
{
if ($exception instanceof Exception && ($exception instanceof UserException || !YII_DEBUG)) {
$message = $exception->getName() . ': ' . $exception->getMessage();
if (Yii::$app->controller instanceof \yii\console\Controller) {
$message = Yii::$app->controller->ansiFormat($message, Console::FG_RED);
}
} else {
$message = YII_DEBUG ? (string)$exception : 'Error: ' . $exception->getMessage();
}
......
......@@ -89,24 +89,22 @@ class Controller extends \yii\base\Controller
*/
public function bindActionParams($action, $params)
{
$args = array();
if (!empty($params)) {
$options = $this->globalOptions();
foreach ($params as $name => $value) {
if (in_array($name, $options, true)) {
$this->$name = $value;
unset($params[$name]);
} elseif (is_int($name)) {
$args[] = $value;
} else {
throw new Exception(Yii::t('yii', 'Unknown option: --{name}', array(
'{name}' => $name,
)));
}
}
}
$args = isset($params[Request::ANONYMOUS_PARAMS]) ? $params[Request::ANONYMOUS_PARAMS] : array();
unset($params[Request::ANONYMOUS_PARAMS]);
if (!empty($params)) {
throw new Exception(Yii::t('yii', 'Unknown options: {params}', array(
'{params}' => implode(', ', array_keys($params)),
)));
}
if ($action instanceof InlineAction) {
$method = new \ReflectionMethod($this, $action->actionMethod);
} else {
......
......@@ -13,8 +13,6 @@ namespace yii\console;
*/
class Request extends \yii\base\Request
{
const ANONYMOUS_PARAMS = '-args';
private $_params;
/**
......@@ -57,13 +55,13 @@ class Request extends \yii\base\Request
$route = '';
}
$params = array(self::ANONYMOUS_PARAMS => array());
$params = array();
foreach ($rawParams as $param) {
if (preg_match('/^--(\w+)(=(.*))?$/', $param, $matches)) {
$name = $matches[1];
$params[$name] = isset($matches[3]) ? $matches[3] : true;
} else {
$params[self::ANONYMOUS_PARAMS][] = $param;
$params[] = $param;
}
}
......
......@@ -71,7 +71,7 @@ class MessageController extends Controller
public function actionExtract($configFile)
{
if (!is_file($configFile)) {
throw new Exception("the configuration file {$configFile} does not exist.");
throw new Exception("The configuration file does not exist: $configFile");
}
$config = array_merge(array(
......
......@@ -79,10 +79,7 @@ class AssetControllerTest extends TestCase
$controller = $this->createAssetController();
ob_start();
ob_implicit_flush(false);
$params = array(
\yii\console\Request::ANONYMOUS_PARAMS => $args
);
$controller->run($actionId, $params);
$controller->run($actionId, $args);
return ob_get_clean();
}
......
......@@ -15,6 +15,7 @@ class MessageControllerTest extends TestCase
public function setUp()
{
$this->mockApplication();
$this->sourcePath = Yii::getAlias('@yiiunit/runtime/test_source');
$this->createDir($this->sourcePath);
if (!file_exists($this->sourcePath)) {
......@@ -100,10 +101,7 @@ class MessageControllerTest extends TestCase
$controller = $this->createMessageController();
ob_start();
ob_implicit_flush(false);
$params = array(
\yii\console\Request::ANONYMOUS_PARAMS => $args
);
$controller->run($actionId, $params);
$controller->run($actionId, $args);
return ob_get_clean();
}
......
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