mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
6e640d1dd6
commit
f056abc818
16
core/control/NullHTTPRequest.php
Normal file
16
core/control/NullHTTPRequest.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
@ -20,6 +20,11 @@ class RequestHandlingTest extends SapphireTest {
|
||||
// );
|
||||
// }
|
||||
|
||||
function testConstructedWithNullRequest() {
|
||||
$r = new RequestHandler();
|
||||
$this->assertType('NullHTTPRequest', $r->getRequest());
|
||||
}
|
||||
|
||||
function testRequestHandlerChainingAllParams() {
|
||||
// TODO
|
||||
}
|
||||
|
22
tests/control/NullHTTPRequestTest.php
Normal file
22
tests/control/NullHTTPRequestTest.php
Normal 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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user