Merge pull request #2115 from andrewandante/pulls/4/add_controller_name_config_var

add controller_name config var to SiteTree for easier override
This commit is contained in:
Ingo Schommer 2018-07-23 14:09:47 +12:00 committed by GitHub
commit 262236c3e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -192,6 +192,15 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
*/ */
private static $hide_ancestor = null; private static $hide_ancestor = null;
/**
* You can define the class of the controller that maps to your SiteTree object here if
* you don't want to rely on the magic of appending Controller to the Classname
*
* @config
* @var string
*/
private static $controller_name = null;
private static $db = array( private static $db = array(
"URLSegment" => "Varchar(255)", "URLSegment" => "Varchar(255)",
"Title" => "Varchar(255)", "Title" => "Varchar(255)",
@ -2779,11 +2788,16 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
/** /**
* Find the controller name by our convention of {$ModelClass}Controller * Find the controller name by our convention of {$ModelClass}Controller
* Can be overriden by config variable
* *
* @return string * @return string
*/ */
public function getControllerName() public function getControllerName()
{ {
if ($controller = Config::inst()->get(static::class, 'controller_name')) {
return $controller;
}
//default controller for SiteTree objects //default controller for SiteTree objects
$controller = ContentController::class; $controller = ContentController::class;

View File

@ -1506,6 +1506,16 @@ class SiteTreeTest extends SapphireTest
$this->assertSame('PageController', $class->getControllerName()); $this->assertSame('PageController', $class->getControllerName());
} }
/**
* Test that the controller name for a SiteTree instance can be gathered when set directly via config var
*/
public function testGetControllerNameFromConfig()
{
Config::inst()->update(Page::class, 'controller_name', 'This\\Is\\A\\New\\Controller');
$class = new Page;
$this->assertSame('This\\Is\\A\\New\\Controller', $class->getControllerName());
}
/** /**
* Test that underscored class names (legacy) are still supported (deprecation notice is issued though). * Test that underscored class names (legacy) are still supported (deprecation notice is issued though).
*/ */