Commit e1593847 by Qiang Xue

Refactored console commands.

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