Permalink
Browse files

ENHANCEMENT Added NullHTTPRequest as a placeholder for new RequestHan…

…dler/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
  • Loading branch information...
1 parent 6e640d1 commit f056abc818202381352b6423da672822917efb01 @chillu chillu committed Dec 16, 2010
@@ -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 @@ public function httpError($errorCode, $errorMessage = null) {
/**
* 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
}
@@ -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());
+ }
+
+}

0 comments on commit f056abc

Please sign in to comment.