Refactoring to support updated b8framework.

This commit is contained in:
Dan Cryer 2013-05-22 16:36:55 +01:00
parent aff5b1886e
commit 67386df9ef
20 changed files with 140 additions and 79 deletions

View file

@ -10,7 +10,8 @@
namespace PHPCI;
use b8;
use b8\Registry;
use b8\Http\Response\RedirectResponse;
use b8\View;
/**
* PHPCI Front Controller
@ -18,35 +19,32 @@ use b8\Registry;
*/
class Application extends b8\Application
{
public function __construct()
{
if (isset($_SERVER['REDIRECT_PATH_INFO'])) {
$_SERVER['REQUEST_URI'] = $_SERVER['REDIRECT_PATH_INFO'];
}
return parent::__construct();
}
/**
* Handle an incoming web request.
*/
public function handleRequest()
{
$controllerName = \b8\Registry::getInstance()->get('ControllerName');
// Registry legacy:
$registry = new b8\Registry($this->config, $this->request);
$this->initRequest();
// Validate the user's session unless it is a login/logout action or a web hook:
$sessionAction = ($controllerName == 'Session' && in_array($this->action, array('login', 'logout')));
$externalAction = in_array($controllerName, array('Bitbucket', 'Github', 'BuildStatus'));
if (!$externalAction && !$sessionAction) {
$this->validateSession();
$sessionAction = ($this->controllerName == 'Session' && in_array($this->action, array('login', 'logout')));
$externalAction = in_array($this->controllerName, array('Bitbucket', 'Github', 'BuildStatus'));
$skipValidation = ($externalAction || $sessionAction);
if($skipValidation || $this->validateSession()) {
parent::handleRequest();
}
// Render content into layout and return:
$view = new b8\View('Layout');
$view->content = parent::handleRequest();
return $view->render();
if (View::exists('layout') && $this->response->hasLayout()) {
$view = new View('layout');
$view->content = $this->response->getContent();
$this->response->setContent($view->render());
}
return $this->response;
}
/**
@ -58,14 +56,21 @@ class Application extends b8\Application
$user = b8\Store\Factory::getStore('User')->getByPrimaryKey($_SESSION['user_id']);
if ($user) {
Registry::getInstance()->set('user', $user);
return;
$_SESSION['user'] = $user;
return true;
}
unset($_SESSION['user_id']);
}
header('Location: /session/login');
die;
if ($this->request->isAjax()) {
$this->response->setResponseCode(401);
$this->response->setBody('');
} else {
$this->response = new RedirectResponse($this->response);
$this->response->setHeader('Location', '/session/login');
}
return false;
}
}

54
PHPCI/Controller.php Normal file
View file

@ -0,0 +1,54 @@
<?php
namespace PHPCI;
use b8\Config;
use b8\Http\Request;
use b8\Http\Response;
use b8\View;
class Controller extends \b8\Controller
{
public function init() {}
public function __construct(Config $config, Request $request, Response $response)
{
parent::__construct($config, $request, $response);
$class = explode('\\', get_class($this));
$this->className = substr(array_pop($class), 0, -10);
$this->setControllerView();
}
protected function setControllerView()
{
if (View::exists($this->className)) {
$this->controllerView = new View($this->className);
} else {
$this->controllerView = new View\UserView('{@content}');
}
}
protected function setView($action)
{
if (View::exists($this->className . '/' . $action)) {
$this->view = new View($this->className . '/' . $action);
}
}
public function handleAction($action, $actionParams)
{
$this->setView($action);
$response = parent::handleAction($action, $actionParams);
if (is_string($response)) {
$this->controllerView->content = $response;
} elseif (isset($this->view)) {
$this->controllerView->content = $this->view->render();
}
$this->response->setContent($this->controllerView->render());
return $this->response;
}
}

View file

@ -19,7 +19,7 @@ use PHPCI\Model\Build;
* @package PHPCI
* @subpackage Web
*/
class BitbucketController extends b8\Controller
class BitbucketController extends \PHPCI\Controller
{
public function init()
{

View file

@ -19,7 +19,7 @@ use PHPCI\Model\Build;
* @package PHPCI
* @subpackage Web
*/
class BuildController extends b8\Controller
class BuildController extends \PHPCI\Controller
{
public function init()
{
@ -32,11 +32,8 @@ class BuildController extends b8\Controller
public function view($buildId)
{
$build = $this->_buildStore->getById($buildId);
$view = new b8\View('Build');
$view->build = $build;
$view->data = $this->getBuildData($buildId);
return $view->render();
$this->view->build = $build;
$this->view->data = $this->getBuildData($buildId);
}
/**

View file

@ -20,7 +20,7 @@ use PHPCI\Model\Build;
* @package PHPCI
* @subpackage Web
*/
class BuildStatusController extends b8\Controller
class BuildStatusController extends \PHPCI\Controller
{
public function init()
{

View file

@ -19,7 +19,7 @@ use PHPCI\Model\Build;
* @package PHPCI
* @subpackage Web
*/
class GithubController extends b8\Controller
class GithubController extends \PHPCI\Controller
{
public function init()
{

View file

@ -17,7 +17,7 @@ use b8;
* @package PHPCI
* @subpackage Web
*/
class IndexController extends b8\Controller
class IndexController extends \PHPCI\Controller
{
public function init()
{
@ -31,11 +31,10 @@ class IndexController extends b8\Controller
public function index()
{
$projects = $this->_projectStore->getWhere(array(), 50, 0, array(), array('title' => 'ASC'));
$view = new b8\View('Index');
$view->builds = $this->getLatestBuildsHtml();
$view->projects = $projects['items'];
$this->view->builds = $this->getLatestBuildsHtml();
$this->view->projects = $projects['items'];
return $view->render();
return $this->view->render();
}
/**

View file

@ -9,9 +9,11 @@
namespace PHPCI\Controller;
use b8;
use PHPCI\Model\Build;
use PHPCI\Model\Project;
use b8;
use b8\Controller;
use b8\Store;
use b8\Form;
use b8\Registry;
@ -21,12 +23,12 @@ use b8\Registry;
* @package PHPCI
* @subpackage Web
*/
class ProjectController extends b8\Controller
class ProjectController extends \PHPCI\Controller
{
public function init()
{
$this->_buildStore = b8\Store\Factory::getStore('Build');
$this->_projectStore = b8\Store\Factory::getStore('Project');
$this->_buildStore = Store\Factory::getStore('Build');
$this->_projectStore = Store\Factory::getStore('Project');
}
/**
@ -38,13 +40,12 @@ class ProjectController extends b8\Controller
$page = $this->getParam('p', 1);
$builds = $this->getLatestBuildsHtml($projectId, (($page - 1) * 10));
$view = new b8\View('Project');
$view->builds = $builds[0];
$view->total = $builds[1];
$view->project = $project;
$view->page = $page;
$this->view->builds = $builds[0];
$this->view->total = $builds[1];
$this->view->project = $project;
$this->view->page = $page;
return $view->render();
return $this->view->render();
}
/**
@ -69,7 +70,7 @@ class ProjectController extends b8\Controller
*/
public function delete($projectId)
{
if (!Registry::getInstance()->get('user')->getIsAdmin()) {
if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
}
@ -107,11 +108,11 @@ class ProjectController extends b8\Controller
*/
public function add()
{
if (!Registry::getInstance()->get('user')->getIsAdmin()) {
if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
}
$method = Registry::getInstance()->get('requestMethod');
$method = $this->request->getMethod();
$this->handleGithubResponse();
if ($method == 'POST') {
@ -199,11 +200,11 @@ class ProjectController extends b8\Controller
*/
public function edit($projectId)
{
if (!Registry::getInstance()->get('user')->getIsAdmin()) {
if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
}
$method = Registry::getInstance()->get('requestMethod');
$method = $this->request->getMethod();
$project = $this->_projectStore->getById($projectId);
if ($method == 'POST') {

View file

@ -17,10 +17,11 @@ use b8;
* @package PHPCI
* @subpackage Web
*/
class SessionController extends b8\Controller
class SessionController extends \PHPCI\Controller
{
public function init()
{
$this->response->disableLayout();
$this->_userStore = b8\Store\Factory::getStore('User');
}
@ -28,10 +29,10 @@ class SessionController extends b8\Controller
* Handles user login (form and processing)
*/
public function login()
{
if (b8\Registry::getInstance()->get('requestMethod') == 'POST') {
{
if ($this->request->getMethod() == 'POST') {
$user = $this->_userStore->getByEmail($this->getParam('email'));
if ($user && password_verify($this->getParam('password', ''), $user->getHash())) {
$_SESSION['user_id'] = $user->getId();
header('Location: ' . PHPCI_URL);
@ -60,9 +61,9 @@ class SessionController extends b8\Controller
$pwd->setClass('btn-success');
$form->addField($pwd);
$view = new b8\View('Login');
$view->form = $form->render();
die($view->render());
$this->view->form = $form->render();
return $this->view->render();
}
/**

View file

@ -20,7 +20,7 @@ use b8\Form;
* @package PHPCI
* @subpackage Web
*/
class UserController extends b8\Controller
class UserController extends \PHPCI\Controller
{
public function init()
{
@ -33,10 +33,9 @@ class UserController extends b8\Controller
public function index()
{
$users = $this->_userStore->getWhere(array(), 1000, 0, array(), array('email' => 'ASC'));
$view = new b8\View('User');
$view->users = $users;
$this->view->users = $users;
return $view->render();
return $this->view->render();
}
/**
@ -44,11 +43,11 @@ class UserController extends b8\Controller
*/
public function add()
{
if (!Registry::getInstance()->get('user')->getIsAdmin()) {
if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
}
$method = Registry::getInstance()->get('requestMethod');
$method = $this->request->getMethod();
if ($method == 'POST') {
$values = $this->getParams();
@ -85,11 +84,11 @@ class UserController extends b8\Controller
*/
public function edit($userId)
{
if (!Registry::getInstance()->get('user')->getIsAdmin()) {
if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
}
$method = Registry::getInstance()->get('requestMethod');
$method = $this->request->getMethod();
$user = $this->_userStore->getById($userId);
if ($method == 'POST') {
@ -172,7 +171,7 @@ class UserController extends b8\Controller
*/
public function delete($userId)
{
if (!Registry::getInstance()->get('user')->getIsAdmin()) {
if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
}

View file

@ -19,7 +19,7 @@ class User
{
public function __call($method, $params = array())
{
$user = \b8\Registry::getInstance()->get('user');
$user = $_SESSION['user'];
return call_user_func_array(array($user, $method), $params);
}
}

View file

@ -101,7 +101,7 @@ class BuildBase extends Model
'length' => '4',
),
'log' => array(
'type' => 'text',
'type' => 'longtext',
'length' => '',
'nullable' => true,
),
@ -500,11 +500,11 @@ class BuildBase extends Model
}
$cacheKey = 'Cache.Project.' . $key;
$rtn = $this->registry->get($cacheKey, null);
$rtn = $this->cache->get($cacheKey, null);
if (empty($rtn)) {
$rtn = \b8\Store\Factory::getStore('Project')->getById($key);
$this->registry->set($cacheKey, $rtn);
$this->cache->set($cacheKey, $rtn);
}
return $rtn;

View file

@ -36,16 +36,18 @@ if (!defined('APPLICATION_PATH')) {
require_once(APPLICATION_PATH . 'vendor/autoload.php');
// Load configuration if present:
$config = new b8\Config();
if (file_exists(APPLICATION_PATH . 'config.php')) {
require(APPLICATION_PATH . 'config.php');
// Define our PHPCI_URL, if not already defined:
if (!defined('PHPCI_URL')) {
define('PHPCI_URL', $registry->get('install_url', '') . '/');
define('PHPCI_URL', $config->get('install_url', '') . '/');
}
}
// Set up the registry:
b8\Registry::getInstance()->set('app_namespace', 'PHPCI');
b8\Registry::getInstance()->set('DefaultController', 'Index');
b8\Registry::getInstance()->set('ViewPath', dirname(__FILE__) . '/PHPCI/View/');
$config->set('app_namespace', 'PHPCI');
$config->set('default_controller', 'Index');
$config->set('view_path', dirname(__FILE__) . '/PHPCI/View/');

View file

@ -9,7 +9,10 @@
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 'on');
require_once('bootstrap.php');
$fc = new PHPCI\Application();
$fc = new PHPCI\Application(b8\Config::getInstance());
print $fc->handleRequest();