From e09a791c921e2b0f2a11db9941e06c335190d10a Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 31 Jan 2013 15:05:49 -0500 Subject: [PATCH] MVC cleanup --- framework/base/Controller.php | 10 ++-- framework/base/ErrorHandler.php | 3 +- framework/base/InlineAction.php | 2 +- framework/base/Module.php | 62 +++++++++++----------- framework/console/Application.php | 26 ++++++++- framework/console/controllers/CreateController.php | 4 +- framework/console/controllers/HelpController.php | 31 +++++------ framework/util/ConsoleHelper.php | 2 +- framework/yiic.php | 10 ++-- 9 files changed, 84 insertions(+), 66 deletions(-) diff --git a/framework/base/Controller.php b/framework/base/Controller.php index 9be3d6e..804b339 100644 --- a/framework/base/Controller.php +++ b/framework/base/Controller.php @@ -100,10 +100,6 @@ class Controller extends Component */ public function runAction($id, $params = array()) { - if ($id === '') { - $id = $this->defaultAction; - } - $action = $this->createAction($id); if ($action !== null) { $oldAction = $this->action; @@ -143,7 +139,7 @@ class Controller extends Component } elseif ($pos > 0) { return $this->module->runAction($route, $params); } else { - return \Yii::$application->runAction($route, $params); + return \Yii::$application->runAction(ltrim($route, '/'), $params); } } @@ -174,6 +170,10 @@ class Controller extends Component */ public function createAction($id) { + if ($id === '') { + $id = $this->defaultAction; + } + $actionMap = $this->actions(); if (isset($actionMap[$id])) { return Yii::createObject($actionMap[$id], $id, $this); diff --git a/framework/base/ErrorHandler.php b/framework/base/ErrorHandler.php index a095509..5b48fbf 100644 --- a/framework/base/ErrorHandler.php +++ b/framework/base/ErrorHandler.php @@ -320,8 +320,7 @@ class ErrorHandler extends Component */ public function renderAsHtml($exception) { - $view = new View; - $view->owner = $this; + $view = new View($this); $name = !YII_DEBUG || $exception instanceof HttpException ? $this->errorView : $this->exceptionView; echo $view->render($name, array( 'exception' => $exception, diff --git a/framework/base/InlineAction.php b/framework/base/InlineAction.php index 00ecb8f..4cd5413 100644 --- a/framework/base/InlineAction.php +++ b/framework/base/InlineAction.php @@ -47,6 +47,6 @@ class InlineAction extends Action { $method = new \ReflectionMethod($this->controller, $this->actionMethod); $args = $this->bindActionParams($method, $params); - return (int)$method->invokeArgs($this, $args); + return (int)$method->invokeArgs($this->controller, $args); } } diff --git a/framework/base/Module.php b/framework/base/Module.php index 966cc64..dcb468c 100644 --- a/framework/base/Module.php +++ b/framework/base/Module.php @@ -559,53 +559,52 @@ abstract class Module extends Component */ public function runAction($route, $params = array()) { - $route = trim($route, '/'); - if ($route === '') { - $route = trim($this->defaultRoute, '/'); - } - 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) { + $result = $this->createController($route); + if (is_array($result)) { + /** @var $controller Controller */ + list($controller, $actionID) = $result; $oldController = Yii::$application->controller; Yii::$application->controller = $controller; - - $status = $controller->runAction($route2, $params); - + $status = $controller->runAction($actionID, $params); Yii::$application->controller = $oldController; - return $status; } 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. * - * 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, * it will look for the controller class under the [[controllerPath]] and create an * instance of it. * - * @param string $id the controller ID - * @return Controller the newly created controller instance + * @param string $route the route consisting of module, controller and action IDs. + * @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])) { - return Yii::createObject($this->controllerMap[$id], $id, $this); + $controller = Yii::createObject($this->controllerMap[$id], $id, $this); } elseif (preg_match('/^[a-z0-9\\-_]+$/', $id)) { $className = StringHelper::id2camel($id) . 'Controller'; $classFile = $this->controllerPath . DIRECTORY_SEPARATOR . $className . '.php'; @@ -615,10 +614,11 @@ abstract class Module extends Component require($classFile); } 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; } } diff --git a/framework/console/Application.php b/framework/console/Application.php index 1b3192e..6ad40cc 100644 --- a/framework/console/Application.php +++ b/framework/console/Application.php @@ -10,7 +10,7 @@ namespace yii\console; use yii\base\Exception; -use yii\util\ReflectionHelper; +use yii\base\InvalidRouteException; /** * Application represents a console application. @@ -94,7 +94,29 @@ class Application extends \yii\base\Application if ($request->getIsConsoleRequest()) { return $this->runAction($request->route, $request->params); } 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; } } diff --git a/framework/console/controllers/CreateController.php b/framework/console/controllers/CreateController.php index a513e40..7bd7fd0 100644 --- a/framework/console/controllers/CreateController.php +++ b/framework/console/controllers/CreateController.php @@ -165,8 +165,8 @@ class CreateController extends Controller } /** - * @param string $path1 abosolute path - * @param string $path2 abosolute path + * @param string $path1 absolute path + * @param string $path2 absolute path * * @return string relative path */ diff --git a/framework/console/controllers/HelpController.php b/framework/console/controllers/HelpController.php index 5617c7b..c663b2b 100644 --- a/framework/console/controllers/HelpController.php +++ b/framework/console/controllers/HelpController.php @@ -12,6 +12,7 @@ namespace yii\console\controllers; use yii\base\Application; use yii\base\InlineAction; use yii\console\Controller; +use yii\util\StringHelper; /** * This command provides help information about console commands. @@ -54,16 +55,16 @@ class HelpController extends Controller } else { $result = \Yii::$application->createController($args[0]); if ($result === false) { - echo "Unknown command: " . $args[0] . "\n"; + echo "\nError: no help for unknown command \"{$args[0]}\".\n"; return 1; } - list($controller, $action) = $result; + list($controller, $actionID) = $result; - if ($action === '') { + if ($actionID === '') { $status = $this->getControllerHelp($controller); } else { - $status = $this->getActionHelp($controller, $action); + $status = $this->getActionHelp($controller, $actionID); } } return $status; @@ -87,13 +88,13 @@ class HelpController extends Controller */ public function getActions($controller) { - $actions = array_keys($controller->actionMap); + $actions = array_keys($controller->actions()); $class = new \ReflectionClass($controller); foreach ($class->getMethods() as $method) { /** @var $method \ReflectionMethod */ $name = $method->getName(); - if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0) { - $actions[] = lcfirst(substr($name, 6)); + if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0 && $name !== 'actions') { + $actions[] = StringHelper::camel2id(substr($name, 6)); } } sort($actions); @@ -107,11 +108,7 @@ class HelpController extends Controller */ protected function getModuleCommands($module) { - if ($module instanceof Application) { - $prefix = ''; - } else { - $prefix = $module->getUniqueId() . '/'; - } + $prefix = $module instanceof Application ? '' : $module->getUniqueID() . '/'; $commands = array(); foreach (array_keys($module->controllerMap) as $id) { @@ -145,12 +142,12 @@ class HelpController extends Controller { $commands = $this->getCommands(); if ($commands !== array()) { - echo "\n Usage: yiic [...options...]\n\n"; - echo "The following commands are available:\n"; + echo "\nUsage: yiic [...options...]\n\n"; + echo "The following commands are available:\n\n"; 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 \n"; } else { echo "\nNo commands are found.\n"; @@ -195,7 +192,7 @@ class HelpController extends Controller $prefix = $controller->getUniqueId(); foreach ($actions as $action) { if ($controller->defaultAction === $action) { - echo " * $prefix/$action (default)\n"; + echo " * $prefix (default)\n"; } else { echo " * $prefix/$action\n"; } diff --git a/framework/util/ConsoleHelper.php b/framework/util/ConsoleHelper.php index 9333b3c..f07ead0 100644 --- a/framework/util/ConsoleHelper.php +++ b/framework/util/ConsoleHelper.php @@ -10,7 +10,7 @@ namespace yii\util; /** - * ConsoleHelper provides additional unility functions for console applications. + * ConsoleHelper provides additional utility functions for console applications. * * @author Carsten Brandt * @author Alexander Makarov diff --git a/framework/yiic.php b/framework/yiic.php index 0f05183..55b9e60 100644 --- a/framework/yiic.php +++ b/framework/yiic.php @@ -1,5 +1,4 @@ '@yii/console/controllers', -); $id = 'yiic'; $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();