New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement TestSwarmContext (issue #127) #131
Changes from all commits
b9de29d
a9e49fa
1aaa420
60f7e23
8e263b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* Wrapper class containing various request-specific objects. | ||
* Each of these objects is created only once for the context. | ||
* The creation happends on-demand and is put in a private cache. | ||
* | ||
* @author Timo Tijhof, 2012 | ||
* @since 0.3.0 | ||
* @package TestSwarm | ||
*/ | ||
class TestSwarmContext { | ||
private $browserInfo, $conf, $db, $request; | ||
|
||
/** | ||
* The context is self-initializing. The only thing it | ||
* needs to be passed is an array with all setting keys from testswarm.ini | ||
* (including ones commented out in the sample file, it has to contain them all) | ||
* Population of default values of optional settings happens in init.php | ||
* @param $config array | ||
*/ | ||
public function __construct( Array $config ) { | ||
$conf = new stdClass; | ||
foreach ( $config as $key => $val ) { | ||
$conf->$key = is_array( $val ) ? (object)$val : $val; | ||
} | ||
$this->conf = $conf; | ||
} | ||
|
||
public function getBrowserInfo() { | ||
if ( $this->browserInfo === null ) { | ||
$ua = isset( $_SERVER["HTTP_USER_AGENT"] ) ? $_SERVER["HTTP_USER_AGENT"] : ""; | ||
$this->browserInfo = BrowserInfo::newFromContext( $this, $ua ); | ||
} | ||
return $this->browserInfo; | ||
} | ||
|
||
/** | ||
* Get the configuration object | ||
* @return stdClass | ||
*/ | ||
public function getConf() { | ||
return $this->conf; | ||
} | ||
|
||
/** | ||
* Get the Database object | ||
* @return Database | ||
*/ | ||
public function getDB() { | ||
if ( $this->db === null ) { | ||
$this->db = Database::newFromContext( $this ); | ||
} | ||
return $this->db; | ||
} | ||
|
||
/** | ||
* Get the WebRequest object | ||
* @return WebRequest | ||
*/ | ||
public function getRequest() { | ||
if ( $this->request === null ) { | ||
$this->request = new WebRequest( $this ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also use a static factory method to be consistent... "newFromContext" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
return $this->request; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this makes sense? Checking for an array and casting it to an object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
conf
is a 2-level deep object (an object with objects). The variable given is a fresh ini parse, which will be an array of arrays. Casting the main variable to an object will only cast the root level, so I'm looping instead. Although right now all settings are in a subgroup, there may be root settings in the future, hence the check for an array first.If it's a group, cast the array to an object. Otherwise keep it (a string, number or boolean) as is. See also
./config/testswarm-sample.ini
andinit.php