Simplifying SiteTree::getControllerName (#1629)

This commit is contained in:
Daniel Hensby 2016-09-23 05:00:13 +01:00 committed by Damian Mooyman
parent ffe85db33f
commit 012867f485
1 changed files with 17 additions and 10 deletions

View File

@ -2713,20 +2713,27 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
}
/**
* Find the controller name by our convention of {$ModelClass}_Controller
*
* @return string
*/
public function getControllerName() {
if ($this->class === SiteTree::class) {
$controller = ContentController::class;
} else {
$ancestry = ClassInfo::ancestry($this->class);
while ($class = array_pop($ancestry)) {
if (class_exists($class . "_Controller")) {
break;
}
}
//default controller for SiteTree objects
$controller = ContentController::class;
$controller = ($class !== null) ? "{$class}_Controller" : ContentController::class;
//go through the ancestry for this class looking for
$ancestry = ClassInfo::ancestry($this->class);
// loop over the array going from the deepest descendant (ie: the current class) to SiteTree
while ($class = array_pop($ancestry)) {
//we don't need to go any deeper than the SiteTree class
if ($class == SiteTree::class) {
break;
}
//if we have a class of "{$ClassName}_Controller" then we found our controller
if (class_exists($candidate = sprintf('%s_Controller', $class))) {
$controller = $candidate;
break;
}
}
return $controller;