Move response common functions to ResponseMaker trait
This commit is contained in:
parent
f269901d72
commit
ff8f899fbe
|
|
@ -36,6 +36,7 @@ class Controller extends Extendable
|
||||||
use \System\Traits\AssetMaker;
|
use \System\Traits\AssetMaker;
|
||||||
use \System\Traits\ConfigMaker;
|
use \System\Traits\ConfigMaker;
|
||||||
use \System\Traits\EventEmitter;
|
use \System\Traits\EventEmitter;
|
||||||
|
use \System\Traits\ResponseMaker;
|
||||||
use \System\Traits\SecurityController;
|
use \System\Traits\SecurityController;
|
||||||
use \Backend\Traits\ErrorMaker;
|
use \Backend\Traits\ErrorMaker;
|
||||||
use \Backend\Traits\WidgetMaker;
|
use \Backend\Traits\WidgetMaker;
|
||||||
|
|
@ -108,16 +109,6 @@ class Controller extends Extendable
|
||||||
*/
|
*/
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int Response status code
|
|
||||||
*/
|
|
||||||
protected $statusCode = 200;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var mixed Override the standard controller response.
|
|
||||||
*/
|
|
||||||
protected $responseOverride = null;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
|
|
@ -177,6 +168,7 @@ class Controller extends Extendable
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check security token.
|
* Check security token.
|
||||||
|
* @see \System\Traits\SecurityController
|
||||||
*/
|
*/
|
||||||
if (!$this->verifyCsrfToken()) {
|
if (!$this->verifyCsrfToken()) {
|
||||||
return Response::make(Lang::get('system::lang.page.invalid_token.label'), 403);
|
return Response::make(Lang::get('system::lang.page.invalid_token.label'), 403);
|
||||||
|
|
@ -184,6 +176,7 @@ class Controller extends Extendable
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check forced HTTPS protocol.
|
* Check forced HTTPS protocol.
|
||||||
|
* @see \System\Traits\SecurityController
|
||||||
*/
|
*/
|
||||||
if (!$this->verifyForceSecure()) {
|
if (!$this->verifyForceSecure()) {
|
||||||
return Redirect::secure(Request::path());
|
return Redirect::secure(Request::path());
|
||||||
|
|
@ -251,15 +244,11 @@ class Controller extends Extendable
|
||||||
*/
|
*/
|
||||||
$result = $this->execPageAction($action, $params);
|
$result = $this->execPageAction($action, $params);
|
||||||
|
|
||||||
if ($this->responseOverride !== null) {
|
/*
|
||||||
$result = $this->responseOverride;
|
* Prepare and return response
|
||||||
}
|
* @see \System\Traits\ResponseMaker
|
||||||
|
*/
|
||||||
if (!is_string($result)) {
|
return $this->makeResponse($result);
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Response::make($result, $this->statusCode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -649,27 +638,6 @@ class Controller extends Extendable
|
||||||
return $id;
|
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
|
// Hints
|
||||||
//
|
//
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ class Controller
|
||||||
{
|
{
|
||||||
use \System\Traits\AssetMaker;
|
use \System\Traits\AssetMaker;
|
||||||
use \System\Traits\EventEmitter;
|
use \System\Traits\EventEmitter;
|
||||||
|
use \System\Traits\ResponseMaker;
|
||||||
use \System\Traits\SecurityController;
|
use \System\Traits\SecurityController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -91,11 +92,6 @@ class Controller
|
||||||
*/
|
*/
|
||||||
public $vars = [];
|
public $vars = [];
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int Response status code
|
|
||||||
*/
|
|
||||||
protected $statusCode = 200;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var self Cache of self
|
* @var self Cache of self
|
||||||
*/
|
*/
|
||||||
|
|
@ -174,6 +170,7 @@ class Controller
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check security token.
|
* Check security token.
|
||||||
|
* @see \System\Traits\SecurityController
|
||||||
*/
|
*/
|
||||||
if (!$this->verifyCsrfToken()) {
|
if (!$this->verifyCsrfToken()) {
|
||||||
return Response::make(Lang::get('system::lang.page.invalid_token.label'), 403);
|
return Response::make(Lang::get('system::lang.page.invalid_token.label'), 403);
|
||||||
|
|
@ -284,11 +281,11 @@ class Controller
|
||||||
return $event;
|
return $event;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_string($result)) {
|
/*
|
||||||
return $result;
|
* Prepare and return response
|
||||||
}
|
* @see \System\Traits\ResponseMaker
|
||||||
|
*/
|
||||||
return Response::make($result, $this->statusCode);
|
return $this->makeResponse($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1251,34 +1248,10 @@ class Controller
|
||||||
return $result;
|
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
|
// 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.
|
* Returns an existing instance of the controller.
|
||||||
* If the controller doesn't exists, returns null.
|
* If the controller doesn't exists, returns null.
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,72 @@
|
||||||
|
<?php namespace System\Traits;
|
||||||
|
|
||||||
|
use Response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Response Maker Trait
|
||||||
|
* Stores attributes the can be used to prepare a response from the server.
|
||||||
|
*
|
||||||
|
* @package october\system
|
||||||
|
* @author Alexey Bobkov, Samuel Georges
|
||||||
|
*/
|
||||||
|
trait ResponseMaker
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var int Response status code
|
||||||
|
*/
|
||||||
|
protected $statusCode = 200;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var mixed Override the standard controller response.
|
||||||
|
*/
|
||||||
|
protected $responseOverride = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue