From 3bf9e1ab254e9353bc4a2223aaed65feb00053fc Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Wed, 16 Apr 2014 12:37:32 +0100 Subject: [PATCH] Inlining the validateSession functionality in Application, fixes #312 --- PHPCI/Application.php | 46 +++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/PHPCI/Application.php b/PHPCI/Application.php index 3d889967..5c9647d2 100644 --- a/PHPCI/Application.php +++ b/PHPCI/Application.php @@ -26,11 +26,27 @@ class Application extends b8\Application $route = '/:controller/:action'; $opts = array('controller' => 'Home', 'action' => 'index'); - $this->router->clearRoutes(); - $this->router->register($route, $opts, function (&$route, Response &$response) use (&$request) { + // Inlined as a closure to fix "using $this when not in object context" on 5.3 + $validateSession = function () { + if (!empty($_SESSION['user_id'])) { + $user = b8\Store\Factory::getStore('User')->getByPrimaryKey($_SESSION['user_id']); + + if ($user) { + $_SESSION['user'] = $user; + return true; + } + + unset($_SESSION['user_id']); + } + + return false; + }; + + // Handler for the route we're about to register, checks for a valid session where necessary: + $routeHandler = function (&$route, Response &$response) use (&$request, $validateSession) { $skipValidation = in_array($route['controller'], array('session', 'webhook', 'build-status')); - if (!$skipValidation && !$this->validateSession()) { + if (!$skipValidation && !$validateSession()) { if ($request->isAjax()) { $response->setResponseCode(401); $response->setContent(''); @@ -43,7 +59,10 @@ class Application extends b8\Application } return true; - }); + }; + + $this->router->clearRoutes(); + $this->router->register($route, $opts, $routeHandler); } /** * Handle an incoming web request. @@ -60,23 +79,4 @@ class Application extends b8\Application return $this->response; } - - /** - * Validate whether or not the remote user has a valid session: - */ - protected function validateSession() - { - if (!empty($_SESSION['user_id'])) { - $user = b8\Store\Factory::getStore('User')->getByPrimaryKey($_SESSION['user_id']); - - if ($user) { - $_SESSION['user'] = $user; - return true; - } - - unset($_SESSION['user_id']); - } - - return false; - } }