mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
API CHANGE #2922: RequestHandler:: now inherit
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@64958 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
cd699e3d89
commit
132957b5c2
@ -77,7 +77,14 @@ class RequestHandler extends ViewableData {
|
||||
function handleRequest($request) {
|
||||
$this->request = $request;
|
||||
|
||||
foreach($this->stat('url_handlers') as $rule => $action) {
|
||||
// $handlerClass is used to step up the class hierarchy to implement url_handlers inheritance
|
||||
$handlerClass = $this->class;
|
||||
// We stop after RequestHandler; in other words, at ViewableData
|
||||
while($handlerClass != 'ViewableData') {
|
||||
// Todo: ajshort's stat rewriting could be useful here.
|
||||
$urlHandlers = eval("return $handlerClass::\$url_handlers;");
|
||||
|
||||
if($urlHandlers) foreach($urlHandlers as $rule => $action) {
|
||||
if(isset($_REQUEST['debug_request'])) Debug::message("Testing '$rule' with '" . $request->remaining() . "' on $this->class");
|
||||
if($params = $request->match($rule, true)) {
|
||||
// FIXME: This unnecessary coupling was added to fix a bug in Image_Uploader.
|
||||
@ -127,10 +134,13 @@ class RequestHandler extends ViewableData {
|
||||
return $this->httpError(400, "I can't handle sub-URLs of a $this->class object.");
|
||||
}
|
||||
|
||||
break;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
$handlerClass = get_parent_class($handlerClass);
|
||||
}
|
||||
|
||||
// If nothing matches, return this object
|
||||
return $this;
|
||||
}
|
||||
|
@ -72,6 +72,16 @@ class RequestHandlingTest extends SapphireTest {
|
||||
$response = Director::test("testParentBase/testChildBase/method/1/2");
|
||||
$this->assertEquals("This is a method on the controller: 1, 2", $response->getBody());
|
||||
}
|
||||
|
||||
function testInheritedUrlHandlers() {
|
||||
/* $url_handlers can be defined on any class, and */
|
||||
$response = Director::test("testGoodBase1/TestForm/fields/SubclassedField/something");
|
||||
$this->assertEquals("customSomething", $response->getBody());
|
||||
|
||||
/* However, if the subclass' url_handlers don't match, then the parent class' url_handlers will be used */
|
||||
$response = Director::test("testGoodBase1/TestForm/fields/SubclassedField");
|
||||
$this->assertEquals("SubclassedField requested", $response->getBody());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -127,7 +137,8 @@ class RequestHandlingTest_Controller extends Controller {
|
||||
|
||||
function TestForm() {
|
||||
return new RequestHandlingTest_Form($this, "TestForm", new FieldSet(
|
||||
new RequestHandlingTest_FormField("MyField")
|
||||
new RequestHandlingTest_FormField("MyField"),
|
||||
new RequestHandlingTest_SubclassedFormField("SubclassedField")
|
||||
), new FieldSet(
|
||||
new FormAction("myAction")
|
||||
));
|
||||
@ -195,3 +206,19 @@ class RequestHandlingTest_FormField extends FormField {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Form field for the test
|
||||
*/
|
||||
class RequestHandlingTest_SubclassedFormField extends RequestHandlingTest_FormField {
|
||||
// We have some url_handlers defined that override RequestHandlingTest_FormField handlers.
|
||||
// We will confirm that the url_handlers inherit.
|
||||
static $url_handlers = array(
|
||||
'something' => 'customSomething',
|
||||
);
|
||||
|
||||
function customSomething() {
|
||||
return "customSomething";
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user