Refactored Controller.
This commit is contained in:
parent
dd9f43b7dd
commit
d1c37fc293
|
@ -7,6 +7,7 @@ use b8\Http\Response;
|
||||||
use b8\Http\Request;
|
use b8\Http\Request;
|
||||||
use b8\Http\Router;
|
use b8\Http\Router;
|
||||||
use PHPCensor\Config;
|
use PHPCensor\Config;
|
||||||
|
use PHPCensor\Controller;
|
||||||
|
|
||||||
class Application
|
class Application
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,128 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace b8;
|
|
||||||
|
|
||||||
use b8\Http\Request;
|
|
||||||
use b8\Http\Response;
|
|
||||||
use PHPCensor\View;
|
|
||||||
use PHPCensor\Config;
|
|
||||||
|
|
||||||
abstract class Controller
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @var Request
|
|
||||||
*/
|
|
||||||
protected $request;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Response
|
|
||||||
*/
|
|
||||||
protected $response;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Config
|
|
||||||
*/
|
|
||||||
protected $config;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var View
|
|
||||||
*/
|
|
||||||
protected $controllerView;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var View
|
|
||||||
*/
|
|
||||||
protected $view;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Config $config
|
|
||||||
* @param Request $request
|
|
||||||
* @param Response $response
|
|
||||||
*/
|
|
||||||
public function __construct(Config $config, Request $request, Response $response)
|
|
||||||
{
|
|
||||||
$this->config = $config;
|
|
||||||
$this->request = $request;
|
|
||||||
$this->response = $response;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
*
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
public function hasAction($name)
|
|
||||||
{
|
|
||||||
if (method_exists($this, $name)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (method_exists($this, '__call')) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles an action on this controller and returns a Response object.
|
|
||||||
*
|
|
||||||
* @param string $action
|
|
||||||
* @param array $actionParams
|
|
||||||
*
|
|
||||||
* @return Response
|
|
||||||
*/
|
|
||||||
public function handleAction($action, $actionParams)
|
|
||||||
{
|
|
||||||
return call_user_func_array([$this, $action], $actionParams);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialise the controller.
|
|
||||||
*/
|
|
||||||
abstract public function init();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a hash of incoming request parameters ($_GET, $_POST)
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getParams()
|
|
||||||
{
|
|
||||||
return $this->request->getParams();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a specific incoming request parameter.
|
|
||||||
*
|
|
||||||
* @param string $key
|
|
||||||
* @param mixed $default Default return value (if key does not exist)
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getParam($key, $default = null)
|
|
||||||
{
|
|
||||||
return $this->request->getParam($key, $default);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Change the value of an incoming request parameter.
|
|
||||||
*
|
|
||||||
* @param string $key
|
|
||||||
* @param mixed $value
|
|
||||||
*/
|
|
||||||
public function setParam($key, $value)
|
|
||||||
{
|
|
||||||
$this->request->setParam($key, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove an incoming request parameter.
|
|
||||||
*
|
|
||||||
* @param string $key
|
|
||||||
*/
|
|
||||||
public function unsetParam($key)
|
|
||||||
{
|
|
||||||
$this->request->unsetParam($key);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -9,11 +9,26 @@ use PHPCensor\Store\Factory;
|
||||||
use PHPCensor\Model\User;
|
use PHPCensor\Model\User;
|
||||||
use PHPCensor\Store\UserStore;
|
use PHPCensor\Store\UserStore;
|
||||||
|
|
||||||
class Controller extends \b8\Controller
|
class Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var View
|
* @var Request
|
||||||
*/
|
*/
|
||||||
|
protected $request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Response
|
||||||
|
*/
|
||||||
|
protected $response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Config
|
||||||
|
*/
|
||||||
|
protected $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var View
|
||||||
|
*/
|
||||||
protected $controllerView;
|
protected $controllerView;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -46,13 +61,17 @@ class Controller extends \b8\Controller
|
||||||
*/
|
*/
|
||||||
public function __construct(Config $config, Request $request, Response $response)
|
public function __construct(Config $config, Request $request, Response $response)
|
||||||
{
|
{
|
||||||
parent::__construct($config, $request, $response);
|
$this->config = $config;
|
||||||
|
$this->request = $request;
|
||||||
|
$this->response = $response;
|
||||||
|
|
||||||
$class = explode('\\', get_class($this));
|
$class = explode('\\', get_class($this));
|
||||||
$this->className = substr(array_pop($class), 0, -10);
|
$this->className = substr(array_pop($class), 0, -10);
|
||||||
$this->setControllerView();
|
$this->setControllerView();
|
||||||
|
|
||||||
unset($_SESSION['php-censor-user']);
|
if (!empty($_SESSION['php-censor-user'])) {
|
||||||
|
unset($_SESSION['php-censor-user']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -90,7 +109,7 @@ class Controller extends \b8\Controller
|
||||||
public function handleAction($action, $actionParams)
|
public function handleAction($action, $actionParams)
|
||||||
{
|
{
|
||||||
$this->setView($action);
|
$this->setView($action);
|
||||||
$response = parent::handleAction($action, $actionParams);
|
$response = call_user_func_array([$this, $action], $actionParams);
|
||||||
|
|
||||||
if ($response instanceof Response) {
|
if ($response instanceof Response) {
|
||||||
return $response;
|
return $response;
|
||||||
|
@ -148,4 +167,66 @@ class Controller extends \b8\Controller
|
||||||
|
|
||||||
return $userStore->getById($_SESSION['php-censor-user-id']);
|
return $userStore->getById($_SESSION['php-censor-user-id']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function hasAction($name)
|
||||||
|
{
|
||||||
|
if (method_exists($this, $name)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (method_exists($this, '__call')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a hash of incoming request parameters ($_GET, $_POST)
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getParams()
|
||||||
|
{
|
||||||
|
return $this->request->getParams();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a specific incoming request parameter.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $default Default return value (if key does not exist)
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getParam($key, $default = null)
|
||||||
|
{
|
||||||
|
return $this->request->getParam($key, $default);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change the value of an incoming request parameter.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
public function setParam($key, $value)
|
||||||
|
{
|
||||||
|
$this->request->setParam($key, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove an incoming request parameter.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
*/
|
||||||
|
public function unsetParam($key)
|
||||||
|
{
|
||||||
|
$this->request->unsetParam($key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,10 +11,12 @@ use PHPCensor\Model\Project;
|
||||||
use PHPCensor\Service\BuildService;
|
use PHPCensor\Service\BuildService;
|
||||||
use PHPCensor\Store\BuildStore;
|
use PHPCensor\Store\BuildStore;
|
||||||
use PHPCensor\Store\ProjectStore;
|
use PHPCensor\Store\ProjectStore;
|
||||||
use b8\Controller;
|
use PHPCensor\Controller;
|
||||||
use PHPCensor\Config;
|
use PHPCensor\Config;
|
||||||
use b8\Exception\HttpException\NotFoundException;
|
use b8\Exception\HttpException\NotFoundException;
|
||||||
use PHPCensor\Store\Factory;
|
use PHPCensor\Store\Factory;
|
||||||
|
use b8\Http\Request;
|
||||||
|
use b8\Http\Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Webhook Controller - Processes webhook pings from BitBucket, Github, Gitlab, Gogs, etc.
|
* Webhook Controller - Processes webhook pings from BitBucket, Github, Gitlab, Gogs, etc.
|
||||||
|
@ -42,6 +44,18 @@ class WebhookController extends Controller
|
||||||
*/
|
*/
|
||||||
protected $buildService;
|
protected $buildService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Config $config
|
||||||
|
* @param Request $request
|
||||||
|
* @param Response $response
|
||||||
|
*/
|
||||||
|
public function __construct(Config $config, Request $request, Response $response)
|
||||||
|
{
|
||||||
|
$this->config = $config;
|
||||||
|
$this->request = $request;
|
||||||
|
$this->response = $response;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialise the controller, set up stores and services.
|
* Initialise the controller, set up stores and services.
|
||||||
*/
|
*/
|
||||||
|
@ -63,7 +77,7 @@ class WebhookController extends Controller
|
||||||
{
|
{
|
||||||
$response = new b8\Http\Response\JsonResponse();
|
$response = new b8\Http\Response\JsonResponse();
|
||||||
try {
|
try {
|
||||||
$data = parent::handleAction($action, $actionParams);
|
$data = call_user_func_array([$this, $action], $actionParams);
|
||||||
if (isset($data['responseCode'])) {
|
if (isset($data['responseCode'])) {
|
||||||
$response->setResponseCode($data['responseCode']);
|
$response->setResponseCode($data['responseCode']);
|
||||||
unset($data['responseCode']);
|
unset($data['responseCode']);
|
||||||
|
|
Loading…
Reference in a new issue