From 82c978f86a79693a01e0fbff79c584f80a6e334b Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Sun, 16 Jun 2013 17:04:28 +0200 Subject: [PATCH] draft idea of action based responses --- framework/yii/base/Action.php | 21 ++++++++++++++++++++- framework/yii/base/Application.php | 1 + framework/yii/base/Controller.php | 1 + framework/yii/base/InlineAction.php | 4 +++- framework/yii/console/Application.php | 14 +++++--------- framework/yii/web/Application.php | 16 ++++------------ 6 files changed, 34 insertions(+), 23 deletions(-) diff --git a/framework/yii/base/Action.php b/framework/yii/base/Action.php index 8778cab..9e0d073 100644 --- a/framework/yii/base/Action.php +++ b/framework/yii/base/Action.php @@ -7,6 +7,8 @@ namespace yii\base; +use Yii; + /** * Action is the base class for all controller action classes. * @@ -40,6 +42,21 @@ class Action extends Component */ public $controller; + private $_response; + + public function getResponse() + { + if ($this->_response === null) { + $this->_response = Yii::$app->createResponse(); + } + return $this->_response; + } + + public function setResponse($response) + { + $this->_response = $response; + } + /** * Constructor. * @param string $id the ID of this action @@ -75,6 +92,8 @@ class Action extends Component throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.'); } $args = $this->controller->bindActionParams($this, $params); - return call_user_func_array(array($this, 'run'), $args); + $response = $this->getResponse(); + $response->result = call_user_func_array(array($this, 'run'), $args); + return $response; } } diff --git a/framework/yii/base/Application.php b/framework/yii/base/Application.php index 9eeb53b..01e8112 100644 --- a/framework/yii/base/Application.php +++ b/framework/yii/base/Application.php @@ -155,6 +155,7 @@ abstract class Application extends Module */ abstract public function handleRequest($request); + public abstract function createResponse(); private $_runtimePath; diff --git a/framework/yii/base/Controller.php b/framework/yii/base/Controller.php index 5b8debb..49af5fe 100644 --- a/framework/yii/base/Controller.php +++ b/framework/yii/base/Controller.php @@ -111,6 +111,7 @@ class Controller extends Component $oldAction = $this->action; $this->action = $action; $result = null; + // TODO beforeAction may also create a response somehow. if ($this->module->beforeAction($action)) { if ($this->beforeAction($action)) { $result = $action->runWithParams($params); diff --git a/framework/yii/base/InlineAction.php b/framework/yii/base/InlineAction.php index 75728e0..e683105 100644 --- a/framework/yii/base/InlineAction.php +++ b/framework/yii/base/InlineAction.php @@ -44,6 +44,8 @@ class InlineAction extends Action public function runWithParams($params) { $args = $this->controller->bindActionParams($this, $params); - return call_user_func_array(array($this->controller, $this->actionMethod), $args); + $response = $this->getResponse(); + $response->result = call_user_func_array(array($this->controller, $this->actionMethod), $args); + return $response; } } diff --git a/framework/yii/console/Application.php b/framework/yii/console/Application.php index c5e22ab..2baef94 100644 --- a/framework/yii/console/Application.php +++ b/framework/yii/console/Application.php @@ -93,22 +93,18 @@ class Application extends \yii\base\Application { list ($route, $params) = $request->resolve(); $result = $this->runAction($route, $params); - if ($result instanceof Response) { - return $result; - } else { - $response = $this->getResponse(); - $response->exitStatus = (int)$result; - return $response; - } + $response = $this->getResponse(); + $response->exitStatus = (int)$result; + return $response; } /** * Returns the response component. * @return Response the response component */ - public function getResponse() + public function createResponse() { - return $this->getComponent('response'); + return new Response(); } /** diff --git a/framework/yii/web/Application.php b/framework/yii/web/Application.php index 6536db7..3994ec2 100644 --- a/framework/yii/web/Application.php +++ b/framework/yii/web/Application.php @@ -65,16 +65,8 @@ class Application extends \yii\base\Application $params = array_splice($this->catchAll, 1); } try { - $result = $this->runAction($route, $params); - if ($result instanceof Response) { - return $result; - } else { - $response = $this->getResponse(); - if ($result !== null) { - $response->setContent($result); - } - return $response; - } + $response = $this->runAction($route, $params); + return $response; } catch (InvalidRouteException $e) { throw new HttpException(404, $e->getMessage(), $e->getCode(), $e); } @@ -119,9 +111,9 @@ class Application extends \yii\base\Application * Returns the response component. * @return Response the response component */ - public function getResponse() + public function createResponse() { - return $this->getComponent('response'); + return new Response(); } /**