From 67386df9efe47f481dfde4ddac81ca998dc5d2e8 Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Wed, 22 May 2013 16:36:55 +0100 Subject: [PATCH] Refactoring to support updated b8framework. --- PHPCI/Application.php | 55 ++++++++++--------- PHPCI/Controller.php | 54 ++++++++++++++++++ PHPCI/Controller/BitbucketController.php | 2 +- PHPCI/Controller/BuildController.php | 9 +-- PHPCI/Controller/BuildStatusController.php | 2 +- PHPCI/Controller/GithubController.php | 2 +- PHPCI/Controller/IndexController.php | 9 ++- PHPCI/Controller/ProjectController.php | 31 ++++++----- PHPCI/Controller/SessionController.php | 15 ++--- PHPCI/Controller/UserController.php | 17 +++--- PHPCI/Helper/User.php | 2 +- PHPCI/Model/Base/BuildBase.php | 6 +- PHPCI/View/{Build.phtml => Build/view.phtml} | 0 PHPCI/View/{Index.phtml => Index/index.phtml} | 0 .../{Project.phtml => Project/view.phtml} | 0 .../View/{Login.phtml => Session/login.phtml} | 0 PHPCI/View/{User.phtml => User/index.phtml} | 0 PHPCI/View/{Layout.phtml => layout.phtml} | 0 bootstrap.php | 10 ++-- index.php | 5 +- 20 files changed, 140 insertions(+), 79 deletions(-) create mode 100644 PHPCI/Controller.php rename PHPCI/View/{Build.phtml => Build/view.phtml} (100%) rename PHPCI/View/{Index.phtml => Index/index.phtml} (100%) rename PHPCI/View/{Project.phtml => Project/view.phtml} (100%) rename PHPCI/View/{Login.phtml => Session/login.phtml} (100%) rename PHPCI/View/{User.phtml => User/index.phtml} (100%) rename PHPCI/View/{Layout.phtml => layout.phtml} (100%) diff --git a/PHPCI/Application.php b/PHPCI/Application.php index a2d2f940..5e873571 100644 --- a/PHPCI/Application.php +++ b/PHPCI/Application.php @@ -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; } } diff --git a/PHPCI/Controller.php b/PHPCI/Controller.php new file mode 100644 index 00000000..b90e1c4e --- /dev/null +++ b/PHPCI/Controller.php @@ -0,0 +1,54 @@ +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; + } +} \ No newline at end of file diff --git a/PHPCI/Controller/BitbucketController.php b/PHPCI/Controller/BitbucketController.php index b0011336..dff0e8e6 100644 --- a/PHPCI/Controller/BitbucketController.php +++ b/PHPCI/Controller/BitbucketController.php @@ -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() { diff --git a/PHPCI/Controller/BuildController.php b/PHPCI/Controller/BuildController.php index 77c4762a..21de59c9 100644 --- a/PHPCI/Controller/BuildController.php +++ b/PHPCI/Controller/BuildController.php @@ -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); } /** diff --git a/PHPCI/Controller/BuildStatusController.php b/PHPCI/Controller/BuildStatusController.php index e898405a..2a0f5c36 100644 --- a/PHPCI/Controller/BuildStatusController.php +++ b/PHPCI/Controller/BuildStatusController.php @@ -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() { diff --git a/PHPCI/Controller/GithubController.php b/PHPCI/Controller/GithubController.php index a2a92e68..90bc37fe 100644 --- a/PHPCI/Controller/GithubController.php +++ b/PHPCI/Controller/GithubController.php @@ -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() { diff --git a/PHPCI/Controller/IndexController.php b/PHPCI/Controller/IndexController.php index 2633ab87..6f9d9c52 100644 --- a/PHPCI/Controller/IndexController.php +++ b/PHPCI/Controller/IndexController.php @@ -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(); } /** diff --git a/PHPCI/Controller/ProjectController.php b/PHPCI/Controller/ProjectController.php index 89a4f819..70f95ab1 100644 --- a/PHPCI/Controller/ProjectController.php +++ b/PHPCI/Controller/ProjectController.php @@ -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') { diff --git a/PHPCI/Controller/SessionController.php b/PHPCI/Controller/SessionController.php index a05c708d..214f9921 100644 --- a/PHPCI/Controller/SessionController.php +++ b/PHPCI/Controller/SessionController.php @@ -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(); } /** diff --git a/PHPCI/Controller/UserController.php b/PHPCI/Controller/UserController.php index 2160bd9b..402c8792 100644 --- a/PHPCI/Controller/UserController.php +++ b/PHPCI/Controller/UserController.php @@ -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.'); } diff --git a/PHPCI/Helper/User.php b/PHPCI/Helper/User.php index e421520c..64872992 100644 --- a/PHPCI/Helper/User.php +++ b/PHPCI/Helper/User.php @@ -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); } } diff --git a/PHPCI/Model/Base/BuildBase.php b/PHPCI/Model/Base/BuildBase.php index a2b0c092..e051aac4 100644 --- a/PHPCI/Model/Base/BuildBase.php +++ b/PHPCI/Model/Base/BuildBase.php @@ -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; diff --git a/PHPCI/View/Build.phtml b/PHPCI/View/Build/view.phtml similarity index 100% rename from PHPCI/View/Build.phtml rename to PHPCI/View/Build/view.phtml diff --git a/PHPCI/View/Index.phtml b/PHPCI/View/Index/index.phtml similarity index 100% rename from PHPCI/View/Index.phtml rename to PHPCI/View/Index/index.phtml diff --git a/PHPCI/View/Project.phtml b/PHPCI/View/Project/view.phtml similarity index 100% rename from PHPCI/View/Project.phtml rename to PHPCI/View/Project/view.phtml diff --git a/PHPCI/View/Login.phtml b/PHPCI/View/Session/login.phtml similarity index 100% rename from PHPCI/View/Login.phtml rename to PHPCI/View/Session/login.phtml diff --git a/PHPCI/View/User.phtml b/PHPCI/View/User/index.phtml similarity index 100% rename from PHPCI/View/User.phtml rename to PHPCI/View/User/index.phtml diff --git a/PHPCI/View/Layout.phtml b/PHPCI/View/layout.phtml similarity index 100% rename from PHPCI/View/Layout.phtml rename to PHPCI/View/layout.phtml diff --git a/bootstrap.php b/bootstrap.php index 3ece003e..4704260f 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -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/'); \ No newline at end of file diff --git a/index.php b/index.php index 622e04c2..4f42e24f 100644 --- a/index.php +++ b/index.php @@ -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();