diff --git a/admin/code/LeftAndMain.php b/admin/code/LeftAndMain.php index bc5919855..b729d3dba 100644 --- a/admin/code/LeftAndMain.php +++ b/admin/code/LeftAndMain.php @@ -698,17 +698,13 @@ class LeftAndMain extends Controller implements PermissionProvider { } /** - * Return a list of appropriate templates for this class, with the given suffix + * Return a list of appropriate templates for this class, with the given suffix using + * {@link SSViewer::get_templates_by_class()} + * + * @return array */ public function getTemplatesWithSuffix($suffix) { - $templates = array(); - $classes = array_reverse(ClassInfo::ancestry($this->class)); - foreach($classes as $class) { - $template = $class . $suffix; - if(SSViewer::hasTemplate($template)) $templates[] = $template; - if($class == 'LeftAndMain') break; - } - return $templates; + return SSViewer::get_templates_by_class(get_class($this), $suffix, 'LeftAndMain'); } public function Content() { diff --git a/tests/view/SSViewerTest.php b/tests/view/SSViewerTest.php index 77ca9599a..01ff00524 100644 --- a/tests/view/SSViewerTest.php +++ b/tests/view/SSViewerTest.php @@ -1018,6 +1018,38 @@ after') }); } + /** + * @covers SSViewer::get_templates_by_class() + */ + public function testGetTemplatesByClass() { + $self = $this; + $this->useTestTheme('layouttest', function() use ($self) { + // Test passing a string + $templates = SSViewer::get_templates_by_class('SSViewerTest_Controller', '', 'Controller'); + $self->assertCount(2, $templates); + + // Test to ensure we're stopping at the base class. + $templates = SSViewer::get_templates_by_class('SSViewerTest_Controller', '', 'SSViewerTest_Controller'); + $self->assertCount(1, $templates); + + // Make sure we can filter our templates by suffix. + $templates = SSViewer::get_templates_by_class('SSViewerTest', '_Controller'); + $self->assertCount(1, $templates); + + // Test passing a valid object + $templates = SSViewer::get_templates_by_class("SSViewerTest_Controller", '', 'Controller'); + + // Test that templates are returned in the correct order + $self->assertEquals('SSViewerTest_Controller', array_shift($templates)); + $self->assertEquals('Controller', array_shift($templates)); + + // Let's throw something random in there. + $self->setExpectedException('InvalidArgumentException'); + $templates = SSViewer::get_templates_by_class(array()); + $this->assertCount(0, $templates); + }); + } + /** * @covers SSViewer::get_themes() */ diff --git a/tests/view/themes/layouttest/Controller.ss b/tests/view/themes/layouttest/Controller.ss new file mode 100644 index 000000000..0704454f4 --- /dev/null +++ b/tests/view/themes/layouttest/Controller.ss @@ -0,0 +1 @@ +Controller \ No newline at end of file diff --git a/tests/view/themes/layouttest/SSViewerTest_Controller.ss b/tests/view/themes/layouttest/SSViewerTest_Controller.ss new file mode 100644 index 000000000..4e4311de3 --- /dev/null +++ b/tests/view/themes/layouttest/SSViewerTest_Controller.ss @@ -0,0 +1 @@ +SSViewerTest \ No newline at end of file diff --git a/view/SSViewer.php b/view/SSViewer.php index b94e86382..44ae67aa2 100644 --- a/view/SSViewer.php +++ b/view/SSViewer.php @@ -687,6 +687,32 @@ class SSViewer { Deprecation::notice('3.2', 'Use the "SSViewer.theme" and "SSViewer.theme_enabled" config settings instead'); return Config::inst()->get('SSViewer', 'theme_enabled') ? Config::inst()->get('SSViewer', 'theme') : null; } + + /** + * Traverses the given the given class context looking for templates with the relevant name. + * + * @param $className string - valid class name + * @param $suffix string + * @param $baseClass string + * + * @return array + */ + public static function get_templates_by_class($className, $suffix = '', $baseClass = null) { + // Figure out the class name from the supplied context. + if(!is_string($className) || !class_exists($className)) { + throw new InvalidArgumentException('SSViewer::get_templates_by_class() expects a valid class name as ' . + 'its first parameter.'); + return array(); + } + $templates = array(); + $classes = array_reverse(ClassInfo::ancestry($className)); + foreach($classes as $class) { + $template = $class . $suffix; + if(SSViewer::hasTemplate($template)) $templates[] = $template; + if($baseClass && $class == $baseClass) break; + } + return $templates; + } /** * @param string|array $templateList If passed as a string with .ss extension, used as the "main" template.