ENHANCEMENT Added NullHTTPRequest as a placeholder for new RequestHandler/Controller instances that haven't handled a URL yet. This means RequestHandler->request can be safely called from singleton controllers

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@115116 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-12-16 04:06:13 +00:00
parent 6e640d1dd6
commit f056abc818
4 changed files with 52 additions and 2 deletions

View File

@ -0,0 +1,16 @@
<?php
/**
* Implements the "Null Object" pattern for a missing http request.
* Set on controllers on construction time, typically overwritten
* by {@link Controller->handleRequest()} and {@link Controller->handleAction()} later on.
*
* @param sapphire
* @subpackage control
*/
class NullHTTPRequest extends SS_HTTPRequest {
function __construct() {
parent::__construct(null, null);
}
}

View File

@ -73,9 +73,13 @@ class RequestHandler extends ViewableData {
* </code>
*/
static $allowed_actions = null;
public function __construct() {
$this->brokenOnConstruct = false;
// Check necessary to avoid class conflicts before manifest is rebuilt
if(class_exists('NullHTTPRequest')) $this->request = new NullHTTPRequest();
parent::__construct();
}
@ -317,8 +321,11 @@ class RequestHandler extends ViewableData {
/**
* Returns the SS_HTTPRequest object that this controller is using.
* Returns a placeholder {@link NullHTTPRequest} object unless
* {@link handleAction()} or {@link handleRequest()} have been called,
* which adds a reference to an actual {@link SS_HTTPRequest} object.
*
* @return SS_HTTPRequest
* @return SS_HTTPRequest|NullHTTPRequest
*/
function getRequest() {
return $this->request;

View File

@ -20,6 +20,11 @@ class RequestHandlingTest extends SapphireTest {
// );
// }
function testConstructedWithNullRequest() {
$r = new RequestHandler();
$this->assertType('NullHTTPRequest', $r->getRequest());
}
function testRequestHandlerChainingAllParams() {
// TODO
}

View File

@ -0,0 +1,22 @@
<?php
/**
* @package sapphire
* @subpackage tests
*/
class NullHTTPRequestTest extends SapphireTest {
function testAllHttpVerbsAreFalse() {
$r = new NullHTTPRequest();
$this->assertFalse($r->isGET());
$this->assertFalse($r->isPOST());
$this->assertFalse($r->isPUT());
$this->assertFalse($r->isDELETE());
$this->assertFalse($r->isHEAD());
}
function testGetURL() {
$r = new NullHTTPRequest();
$this->assertEquals('', $r->getURL());
}
}