Browse Source

MVC cleanup

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
e09a791c92
  1. 10
      framework/base/Controller.php
  2. 3
      framework/base/ErrorHandler.php
  3. 2
      framework/base/InlineAction.php
  4. 62
      framework/base/Module.php
  5. 26
      framework/console/Application.php
  6. 4
      framework/console/controllers/CreateController.php
  7. 31
      framework/console/controllers/HelpController.php
  8. 2
      framework/util/ConsoleHelper.php
  9. 10
      framework/yiic.php

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

3
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,

2
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);
}
}

62
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;
}
}

26
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;
}
}

4
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
*/

31
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 <command-name> [...options...]\n\n";
echo "The following commands are available:\n";
echo "\nUsage: yiic <command-name> [...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 <command-name>\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";
}

2
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 <mail@cebe.cc>
* @author Alexander Makarov <sam@rmcreative.ru>

10
framework/yiic.php

@ -1,5 +1,4 @@
<?php
define('YII_DEBUG', true);
/**
* Yii console bootstrap file.
*
@ -8,16 +7,17 @@ define('YII_DEBUG', true);
* @license http://www.yiiframework.com/license/
*/
defined('YII_DEBUG') or define('YII_DEBUG', true);
// fcgi doesn't have STDIN defined by default
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
require(__DIR__ . '/yii.php');
$config = array(
'controllerPath' => '@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();

Loading…
Cancel
Save