Commit e09a791c by Qiang Xue

MVC cleanup

parent 4402073d
...@@ -100,10 +100,6 @@ class Controller extends Component ...@@ -100,10 +100,6 @@ class Controller extends Component
*/ */
public function runAction($id, $params = array()) public function runAction($id, $params = array())
{ {
if ($id === '') {
$id = $this->defaultAction;
}
$action = $this->createAction($id); $action = $this->createAction($id);
if ($action !== null) { if ($action !== null) {
$oldAction = $this->action; $oldAction = $this->action;
...@@ -143,7 +139,7 @@ class Controller extends Component ...@@ -143,7 +139,7 @@ class Controller extends Component
} elseif ($pos > 0) { } elseif ($pos > 0) {
return $this->module->runAction($route, $params); return $this->module->runAction($route, $params);
} else { } else {
return \Yii::$application->runAction($route, $params); return \Yii::$application->runAction(ltrim($route, '/'), $params);
} }
} }
...@@ -174,6 +170,10 @@ class Controller extends Component ...@@ -174,6 +170,10 @@ class Controller extends Component
*/ */
public function createAction($id) public function createAction($id)
{ {
if ($id === '') {
$id = $this->defaultAction;
}
$actionMap = $this->actions(); $actionMap = $this->actions();
if (isset($actionMap[$id])) { if (isset($actionMap[$id])) {
return Yii::createObject($actionMap[$id], $id, $this); return Yii::createObject($actionMap[$id], $id, $this);
......
...@@ -320,8 +320,7 @@ class ErrorHandler extends Component ...@@ -320,8 +320,7 @@ class ErrorHandler extends Component
*/ */
public function renderAsHtml($exception) public function renderAsHtml($exception)
{ {
$view = new View; $view = new View($this);
$view->owner = $this;
$name = !YII_DEBUG || $exception instanceof HttpException ? $this->errorView : $this->exceptionView; $name = !YII_DEBUG || $exception instanceof HttpException ? $this->errorView : $this->exceptionView;
echo $view->render($name, array( echo $view->render($name, array(
'exception' => $exception, 'exception' => $exception,
......
...@@ -47,6 +47,6 @@ class InlineAction extends Action ...@@ -47,6 +47,6 @@ class InlineAction extends Action
{ {
$method = new \ReflectionMethod($this->controller, $this->actionMethod); $method = new \ReflectionMethod($this->controller, $this->actionMethod);
$args = $this->bindActionParams($method, $params); $args = $this->bindActionParams($method, $params);
return (int)$method->invokeArgs($this, $args); return (int)$method->invokeArgs($this->controller, $args);
} }
} }
...@@ -559,53 +559,52 @@ abstract class Module extends Component ...@@ -559,53 +559,52 @@ abstract class Module extends Component
*/ */
public function runAction($route, $params = array()) public function runAction($route, $params = array())
{ {
$route = trim($route, '/'); $result = $this->createController($route);
if ($route === '') { if (is_array($result)) {
$route = trim($this->defaultRoute, '/'); /** @var $controller Controller */
} list($controller, $actionID) = $result;
if (($pos = strpos($route, '/')) !== false) {
$id = substr($route, 0, $pos);
$route2 = substr($route, $pos + 1);
} else {
$id = $route;
$route2 = '';
}
$module = $this->getModule($id);
if ($module !== null) {
return $module->runAction($route2, $params);
}
$controller = $this->createController($id);
if ($controller !== null) {
$oldController = Yii::$application->controller; $oldController = Yii::$application->controller;
Yii::$application->controller = $controller; Yii::$application->controller = $controller;
$status = $controller->runAction($actionID, $params);
$status = $controller->runAction($route2, $params);
Yii::$application->controller = $oldController; Yii::$application->controller = $oldController;
return $status; return $status;
} else { } else {
throw new InvalidRouteException('Unable to resolve the request: ' . $this->getUniqueId() . '/' . $route); throw new InvalidRouteException('Unable to resolve the request: ' . trim($this->getUniqueId() . '/' . $route, '/'));
} }
} }
/** /**
* Creates a controller instance based on the controller ID. * Creates a controller instance based on the controller ID.
* *
* The controller is created within the given module. The method first attempts to * The controller is created within this module. The method first attempts to
* create the controller based on the [[controllerMap]] of the module. If not available, * create the controller based on the [[controllerMap]] of the module. If not available,
* it will look for the controller class under the [[controllerPath]] and create an * it will look for the controller class under the [[controllerPath]] and create an
* instance of it. * instance of it.
* *
* @param string $id the controller ID * @param string $route the route consisting of module, controller and action IDs.
* @return Controller the newly created controller instance * @return array|boolean if the controller is created successfully, it will be returned together
* with the remainder of the route which represents the action ID. Otherwise false will be returned.
*/ */
public function createController($id) public function createController($route)
{ {
if ($route === '') {
$route = $this->defaultRoute;
}
if (($pos = strpos($route, '/')) !== false) {
$id = substr($route, 0, $pos);
$route = substr($route, $pos + 1);
} else {
$id = $route;
$route = '';
}
$module = $this->getModule($id);
if ($module !== null) {
return $module->createController($route);
}
if (isset($this->controllerMap[$id])) { if (isset($this->controllerMap[$id])) {
return Yii::createObject($this->controllerMap[$id], $id, $this); $controller = Yii::createObject($this->controllerMap[$id], $id, $this);
} elseif (preg_match('/^[a-z0-9\\-_]+$/', $id)) { } elseif (preg_match('/^[a-z0-9\\-_]+$/', $id)) {
$className = StringHelper::id2camel($id) . 'Controller'; $className = StringHelper::id2camel($id) . 'Controller';
$classFile = $this->controllerPath . DIRECTORY_SEPARATOR . $className . '.php'; $classFile = $this->controllerPath . DIRECTORY_SEPARATOR . $className . '.php';
...@@ -615,10 +614,11 @@ abstract class Module extends Component ...@@ -615,10 +614,11 @@ abstract class Module extends Component
require($classFile); require($classFile);
} }
if (class_exists($className, false) && is_subclass_of($className, '\yii\base\Controller')) { if (class_exists($className, false) && is_subclass_of($className, '\yii\base\Controller')) {
return new $className($id, $this); $controller = new $className($id, $this);
} }
} }
} }
return null;
return isset($controller) ? array($controller, $route) : false;
} }
} }
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
namespace yii\console; namespace yii\console;
use yii\base\Exception; use yii\base\Exception;
use yii\util\ReflectionHelper; use yii\base\InvalidRouteException;
/** /**
* Application represents a console application. * Application represents a console application.
...@@ -94,7 +94,29 @@ class Application extends \yii\base\Application ...@@ -94,7 +94,29 @@ class Application extends \yii\base\Application
if ($request->getIsConsoleRequest()) { if ($request->getIsConsoleRequest()) {
return $this->runAction($request->route, $request->params); return $this->runAction($request->route, $request->params);
} else { } else {
die('This script must be run from the command line.'); echo "Error: this script must be run from the command line.";
return 1;
}
}
/**
* Runs a controller action specified by a route.
* This method parses the specified route and creates the corresponding child module(s), controller and action
* instances. It then calls [[Controller::runAction()]] to run the action with the given parameters.
* If the route is empty, the method will use [[defaultRoute]].
* @param string $route the route that specifies the action.
* @param array $params the parameters to be passed to the action
* @return integer the status code returned by the action execution. 0 means normal, and other values mean abnormal.
* @throws InvalidRouteException if the requested route cannot be resolved into an action successfully
*/
public function runAction($route, $params = array())
{
try {
return parent::runAction($route, $params);
} catch (InvalidRouteException $e) {
echo "\nError: unknown command \"$route\".\n";
return 1;
} }
} }
......
...@@ -165,8 +165,8 @@ class CreateController extends Controller ...@@ -165,8 +165,8 @@ class CreateController extends Controller
} }
/** /**
* @param string $path1 abosolute path * @param string $path1 absolute path
* @param string $path2 abosolute path * @param string $path2 absolute path
* *
* @return string relative path * @return string relative path
*/ */
......
...@@ -12,6 +12,7 @@ namespace yii\console\controllers; ...@@ -12,6 +12,7 @@ namespace yii\console\controllers;
use yii\base\Application; use yii\base\Application;
use yii\base\InlineAction; use yii\base\InlineAction;
use yii\console\Controller; use yii\console\Controller;
use yii\util\StringHelper;
/** /**
* This command provides help information about console commands. * This command provides help information about console commands.
...@@ -54,16 +55,16 @@ class HelpController extends Controller ...@@ -54,16 +55,16 @@ class HelpController extends Controller
} else { } else {
$result = \Yii::$application->createController($args[0]); $result = \Yii::$application->createController($args[0]);
if ($result === false) { if ($result === false) {
echo "Unknown command: " . $args[0] . "\n"; echo "\nError: no help for unknown command \"{$args[0]}\".\n";
return 1; return 1;
} }
list($controller, $action) = $result; list($controller, $actionID) = $result;
if ($action === '') { if ($actionID === '') {
$status = $this->getControllerHelp($controller); $status = $this->getControllerHelp($controller);
} else { } else {
$status = $this->getActionHelp($controller, $action); $status = $this->getActionHelp($controller, $actionID);
} }
} }
return $status; return $status;
...@@ -87,13 +88,13 @@ class HelpController extends Controller ...@@ -87,13 +88,13 @@ class HelpController extends Controller
*/ */
public function getActions($controller) public function getActions($controller)
{ {
$actions = array_keys($controller->actionMap); $actions = array_keys($controller->actions());
$class = new \ReflectionClass($controller); $class = new \ReflectionClass($controller);
foreach ($class->getMethods() as $method) { foreach ($class->getMethods() as $method) {
/** @var $method \ReflectionMethod */ /** @var $method \ReflectionMethod */
$name = $method->getName(); $name = $method->getName();
if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0) { if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0 && $name !== 'actions') {
$actions[] = lcfirst(substr($name, 6)); $actions[] = StringHelper::camel2id(substr($name, 6));
} }
} }
sort($actions); sort($actions);
...@@ -107,11 +108,7 @@ class HelpController extends Controller ...@@ -107,11 +108,7 @@ class HelpController extends Controller
*/ */
protected function getModuleCommands($module) protected function getModuleCommands($module)
{ {
if ($module instanceof Application) { $prefix = $module instanceof Application ? '' : $module->getUniqueID() . '/';
$prefix = '';
} else {
$prefix = $module->getUniqueId() . '/';
}
$commands = array(); $commands = array();
foreach (array_keys($module->controllerMap) as $id) { foreach (array_keys($module->controllerMap) as $id) {
...@@ -145,12 +142,12 @@ class HelpController extends Controller ...@@ -145,12 +142,12 @@ class HelpController extends Controller
{ {
$commands = $this->getCommands(); $commands = $this->getCommands();
if ($commands !== array()) { if ($commands !== array()) {
echo "\n Usage: yiic <command-name> [...options...]\n\n"; echo "\nUsage: yiic <command-name> [...options...]\n\n";
echo "The following commands are available:\n"; echo "The following commands are available:\n\n";
foreach ($commands as $command) { foreach ($commands as $command) {
echo " - $command\n"; echo " * $command\n";
} }
echo "\nTo see individual command help, enter:\n"; echo "\nTo see the help of each command, enter:\n";
echo "\n yiic help <command-name>\n"; echo "\n yiic help <command-name>\n";
} else { } else {
echo "\nNo commands are found.\n"; echo "\nNo commands are found.\n";
...@@ -195,7 +192,7 @@ class HelpController extends Controller ...@@ -195,7 +192,7 @@ class HelpController extends Controller
$prefix = $controller->getUniqueId(); $prefix = $controller->getUniqueId();
foreach ($actions as $action) { foreach ($actions as $action) {
if ($controller->defaultAction === $action) { if ($controller->defaultAction === $action) {
echo " * $prefix/$action (default)\n"; echo " * $prefix (default)\n";
} else { } else {
echo " * $prefix/$action\n"; echo " * $prefix/$action\n";
} }
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
namespace yii\util; namespace yii\util;
/** /**
* ConsoleHelper provides additional unility functions for console applications. * ConsoleHelper provides additional utility functions for console applications.
* *
* @author Carsten Brandt <mail@cebe.cc> * @author Carsten Brandt <mail@cebe.cc>
* @author Alexander Makarov <sam@rmcreative.ru> * @author Alexander Makarov <sam@rmcreative.ru>
......
<?php <?php
define('YII_DEBUG', true);
/** /**
* Yii console bootstrap file. * Yii console bootstrap file.
* *
...@@ -8,16 +7,17 @@ define('YII_DEBUG', true); ...@@ -8,16 +7,17 @@ define('YII_DEBUG', true);
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
defined('YII_DEBUG') or define('YII_DEBUG', true);
// fcgi doesn't have STDIN defined by default // fcgi doesn't have STDIN defined by default
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r')); defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
require(__DIR__ . '/yii.php'); require(__DIR__ . '/yii.php');
$config = array(
'controllerPath' => '@yii/console/controllers',
);
$id = 'yiic'; $id = 'yiic';
$basePath = __DIR__ . '/console'; $basePath = __DIR__ . '/console';
$application = new yii\console\Application($id, $basePath, $config); $application = new yii\console\Application($id, $basePath, array(
'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