FIX Allow the current controller as well as injectable HTTPRequest objects

This commit is contained in:
Robbie Averill 2017-12-12 16:35:53 +13:00
parent 097d0697c5
commit eb6c1fc6de

View File

@ -178,15 +178,17 @@ class SecurityToken implements TemplateGlobalProvider
* Returns the current session instance from the injector * Returns the current session instance from the injector
* *
* @return Session * @return Session
* @throws Exception If the HTTPRequest class hasn't been registered as a service * @throws Exception If the HTTPRequest class hasn't been registered as a service and no controllers exist
*/ */
protected function getSession() protected function getSession()
{ {
$injector = Injector::inst(); $injector = Injector::inst();
if (!$injector->has(HTTPRequest::class)) { if ($injector->has(HTTPRequest::class)) {
throw new Exception('No HTTPRequest object available yet!'); return $injector->get(HTTPRequest::class)->getSession();
} elseif (Controller::has_curr()) {
return Controller::curr()->getRequest()->getSession();
} }
return $injector->get(HTTPRequest::class)->getSession(); throw new Exception('No HTTPRequest object or controller available yet!');
} }
/** /**