From ff8f899fbe19ad4301d4bfab47d6b1a0b3e73549 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 2 Nov 2019 18:21:22 +1100 Subject: [PATCH] Move response common functions to ResponseMaker trait --- modules/backend/classes/Controller.php | 48 +++-------------- modules/cms/classes/Controller.php | 41 +++----------- modules/system/traits/ResponseMaker.php | 72 +++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 74 deletions(-) create mode 100644 modules/system/traits/ResponseMaker.php diff --git a/modules/backend/classes/Controller.php b/modules/backend/classes/Controller.php index 1f727432e..05d4eb563 100644 --- a/modules/backend/classes/Controller.php +++ b/modules/backend/classes/Controller.php @@ -36,6 +36,7 @@ class Controller extends Extendable use \System\Traits\AssetMaker; use \System\Traits\ConfigMaker; use \System\Traits\EventEmitter; + use \System\Traits\ResponseMaker; use \System\Traits\SecurityController; use \Backend\Traits\ErrorMaker; use \Backend\Traits\WidgetMaker; @@ -108,16 +109,6 @@ class Controller extends Extendable */ protected $guarded = []; - /** - * @var int Response status code - */ - protected $statusCode = 200; - - /** - * @var mixed Override the standard controller response. - */ - protected $responseOverride = null; - /** * Constructor. */ @@ -177,6 +168,7 @@ class Controller extends Extendable /* * Check security token. + * @see \System\Traits\SecurityController */ if (!$this->verifyCsrfToken()) { return Response::make(Lang::get('system::lang.page.invalid_token.label'), 403); @@ -184,6 +176,7 @@ class Controller extends Extendable /* * Check forced HTTPS protocol. + * @see \System\Traits\SecurityController */ if (!$this->verifyForceSecure()) { return Redirect::secure(Request::path()); @@ -251,15 +244,11 @@ class Controller extends Extendable */ $result = $this->execPageAction($action, $params); - if ($this->responseOverride !== null) { - $result = $this->responseOverride; - } - - if (!is_string($result)) { - return $result; - } - - return Response::make($result, $this->statusCode); + /* + * Prepare and return response + * @see \System\Traits\ResponseMaker + */ + return $this->makeResponse($result); } /** @@ -649,27 +638,6 @@ class Controller extends Extendable return $id; } - /** - * Sets the status code for the current web response. - * @param int $code Status code - */ - public function setStatusCode($code) - { - $this->statusCode = (int) $code; - return $this; - } - - /** - * Sets the response for the current page request cycle, this value takes priority - * over the standard response prepared by the controller. - * @param mixed $response Response object or string - */ - public function setResponse($response) - { - $this->responseOverride = $response; - return $this; - } - // // Hints // diff --git a/modules/cms/classes/Controller.php b/modules/cms/classes/Controller.php index 7910e0b89..53293d70e 100644 --- a/modules/cms/classes/Controller.php +++ b/modules/cms/classes/Controller.php @@ -39,6 +39,7 @@ class Controller { use \System\Traits\AssetMaker; use \System\Traits\EventEmitter; + use \System\Traits\ResponseMaker; use \System\Traits\SecurityController; /** @@ -91,11 +92,6 @@ class Controller */ public $vars = []; - /** - * @var int Response status code - */ - protected $statusCode = 200; - /** * @var self Cache of self */ @@ -174,6 +170,7 @@ class Controller /* * Check security token. + * @see \System\Traits\SecurityController */ if (!$this->verifyCsrfToken()) { return Response::make(Lang::get('system::lang.page.invalid_token.label'), 403); @@ -284,11 +281,11 @@ class Controller return $event; } - if (!is_string($result)) { - return $result; - } - - return Response::make($result, $this->statusCode); + /* + * Prepare and return response + * @see \System\Traits\ResponseMaker + */ + return $this->makeResponse($result); } /** @@ -1251,34 +1248,10 @@ class Controller return $result; } - // - // Setters - // - - /** - * Sets the status code for the current web response. - * @param int $code Status code - * @return self - */ - public function setStatusCode($code) - { - $this->statusCode = (int) $code; - return $this; - } - // // Getters // - /** - * Returns the status code for the current web response. - * @return int Status code - */ - public function getStatusCode() - { - return $this->statusCode; - } - /** * Returns an existing instance of the controller. * If the controller doesn't exists, returns null. diff --git a/modules/system/traits/ResponseMaker.php b/modules/system/traits/ResponseMaker.php new file mode 100644 index 000000000..9aa4bed4d --- /dev/null +++ b/modules/system/traits/ResponseMaker.php @@ -0,0 +1,72 @@ +statusCode = (int) $code; + return $this; + } + + /** + * Returns the status code for the current web response. + * @return int Status code + */ + public function getStatusCode() + { + return $this->statusCode; + } + + /** + * Sets the response for the current page request cycle, this value takes priority + * over the standard response prepared by the controller. + * @param mixed $response Response object or string + */ + public function setResponse($response) + { + $this->responseOverride = $response; + return $this; + } + + /** + * Prepares a response that considers overrides and custom responses. + * @param mixed $contents + * @return mixed + */ + public function makeResponse($contents) + { + if ($this->responseOverride !== null) { + $contents = $this->responseOverride; + } + + if (!is_string($contents)) { + return $contents; + } + + return Response::make($contents, $this->statusCode); + } +}