From 2b5984a73c586938f3e580f8eb05afc31036a04f Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Mon, 3 Mar 2014 16:19:38 +1300 Subject: [PATCH] Coupling environment with PHP session info, avoids injector troubles I'd rather have Session::get('TestSessionId') being passed in as an identifier, but the env object is passed around so much as a singleton that its easy to forget that argument, and cause hard to debug errors. We need a global "app" object which all other objects can reference somehow. --- code/TestSessionController.php | 2 +- code/TestSessionEnvironment.php | 21 ++++++++++++++++++++- code/TestSessionRequestFilter.php | 5 +---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/code/TestSessionController.php b/code/TestSessionController.php index 3384054..4068e48 100644 --- a/code/TestSessionController.php +++ b/code/TestSessionController.php @@ -30,7 +30,7 @@ class TestSessionController extends Controller { public function __construct() { parent::__construct(); - $this->environment = Injector::inst()->get('TestSessionEnvironment', Session::get('TestSessionId')); + $this->environment = Injector::inst()->get('TestSessionEnvironment'); } public function init() { diff --git a/code/TestSessionEnvironment.php b/code/TestSessionEnvironment.php index 32e6f21..7ee33ba 100644 --- a/code/TestSessionEnvironment.php +++ b/code/TestSessionEnvironment.php @@ -54,7 +54,12 @@ class TestSessionEnvironment extends Object { public function __construct($id = null) { parent::__construct(); - $this->id = $id; + if($id) { + $this->id = $id; + } else { + Session::start(); + $this->id = Session::get('TestSessionId'); + } } /** @@ -75,6 +80,20 @@ class TestSessionEnvironment extends Object { return(file_exists($this->getFilePath())); } + /** + * @param String $id + */ + public function setId($id) { + $this->id = $id; + } + + /** + * @return String + */ + public function getId() { + return $this->id; + } + /** * Creates a temp database, sets up any extra requirements, and writes the state file. The database will be * connected to as part of {@link self::applyState()}, so if you're continuing script execution after calling this diff --git a/code/TestSessionRequestFilter.php b/code/TestSessionRequestFilter.php index cc3cc21..bfdbd41 100644 --- a/code/TestSessionRequestFilter.php +++ b/code/TestSessionRequestFilter.php @@ -10,10 +10,7 @@ class TestSessionRequestFilter { protected $testSessionEnvironment; public function __construct() { - $this->testSessionEnvironment = Injector::inst()->get( - 'TestSessionEnvironment', - Session::get('TestSessionId') - ); + $this->testSessionEnvironment = Injector::inst()->get('TestSessionEnvironment'); } public function preRequest($req, $session, $model) {