FIX SSViewer should resolve templates with or without underscores

* Capture fully qualified class name, without "Controller"
This commit is contained in:
Robbie Averill 2016-12-30 12:27:20 +13:00
parent c620063608
commit 6fb49224b9
3 changed files with 45 additions and 48 deletions

View File

@ -253,9 +253,9 @@ class SSViewer implements Flushable
$templates[] = $template; $templates[] = $template;
$templates[] = ['type' => 'Includes', $template]; $templates[] = ['type' => 'Includes', $template];
// If the class is "Page_Controller", look for Page.ss // If the class is "PageController" (PSR-2 compatibility) or "Page_Controller" (legacy), look for Page.ss
if (stripos($class, '_controller') !== false) { if (preg_match('/^(?<name>.+[^\\])_?Controller$/i', $class, $matches)) {
$templates[] = str_ireplace('_controller', '', $class) . $suffix; $templates[] = $matches['name'] . $suffix;
} }
if ($baseClass && $class == $baseClass) { if ($baseClass && $class == $baseClass) {

View File

@ -23,7 +23,7 @@ use SilverStripe\View\Requirements_Backend;
use SilverStripe\View\SSViewer; use SilverStripe\View\SSViewer;
use SilverStripe\View\Requirements; use SilverStripe\View\Requirements;
use SilverStripe\View\Tests\SSViewerTest\SSViewerTestModel; use SilverStripe\View\Tests\SSViewerTest\SSViewerTestModel;
use SilverStripe\View\Tests\SSViewerTest\SSViewerTestModel_Controller; use SilverStripe\View\Tests\SSViewerTest\SSViewerTestModelController;
use SilverStripe\View\ViewableData; use SilverStripe\View\ViewableData;
use SilverStripe\View\SSViewer_FromString; use SilverStripe\View\SSViewer_FromString;
use SilverStripe\View\SSTemplateParser; use SilverStripe\View\SSTemplateParser;
@ -1526,17 +1526,15 @@ after'
public function testLayout() public function testLayout()
{ {
$self = $this;
$this->useTestTheme( $this->useTestTheme(
__DIR__.'/SSViewerTest', __DIR__.'/SSViewerTest',
'layouttest', 'layouttest',
function () use ($self) { function () {
$template = new SSViewer(array('Page')); $template = new SSViewer(array('Page'));
$self->assertEquals("Foo\n\n", $template->process(new ArrayData(array()))); $this->assertEquals("Foo\n\n", $template->process(new ArrayData(array())));
$template = new SSViewer(array('Shortcodes', 'Page')); $template = new SSViewer(array('Shortcodes', 'Page'));
$self->assertEquals("[file_link]\n\n", $template->process(new ArrayData(array()))); $this->assertEquals("[file_link]\n\n", $template->process(new ArrayData(array())));
} }
); );
} }
@ -1546,23 +1544,22 @@ after'
*/ */
public function testGetTemplatesByClass() public function testGetTemplatesByClass()
{ {
$self = $this;
$this->useTestTheme( $this->useTestTheme(
__DIR__ . '/SSViewerTest', __DIR__ . '/SSViewerTest',
'layouttest', 'layouttest',
function () use ($self) { function () {
// Test passing a string // Test passing a string
$templates = SSViewer::get_templates_by_class( $templates = SSViewer::get_templates_by_class(
SSViewerTestModel_Controller::class, SSViewerTestModelController::class,
'', '',
Controller::class Controller::class
); );
$self->assertEquals( $this->assertEquals(
[ [
SSViewerTestModel_Controller::class, SSViewerTestModelController::class,
[ [
'type' => 'Includes', 'type' => 'Includes',
SSViewerTestModel_Controller::class, SSViewerTestModelController::class,
], ],
SSViewerTestModel::class, SSViewerTestModel::class,
Controller::class, Controller::class,
@ -1576,16 +1573,16 @@ after'
// Test to ensure we're stopping at the base class. // Test to ensure we're stopping at the base class.
$templates = SSViewer::get_templates_by_class( $templates = SSViewer::get_templates_by_class(
SSViewerTestModel_Controller::class, SSViewerTestModelController::class,
'', '',
SSViewerTestModel_Controller::class SSViewerTestModelController::class
); );
$self->assertEquals( $this->assertEquals(
[ [
SSViewerTestModel_Controller::class, SSViewerTestModelController::class,
[ [
'type' => 'Includes', 'type' => 'Includes',
SSViewerTestModel_Controller::class, SSViewerTestModelController::class,
], ],
SSViewerTestModel::class, SSViewerTestModel::class,
], ],
@ -1595,27 +1592,27 @@ after'
// Make sure we can search templates by suffix. // Make sure we can search templates by suffix.
$templates = SSViewer::get_templates_by_class( $templates = SSViewer::get_templates_by_class(
SSViewerTestModel::class, SSViewerTestModel::class,
'_Controller', 'Controller',
DataObject::class DataObject::class
); );
$self->assertEquals( $this->assertEquals(
[ [
SSViewerTestModel_Controller::class, SSViewerTestModelController::class,
[ [
'type' => 'Includes', 'type' => 'Includes',
SSViewerTestModel_Controller::class, SSViewerTestModelController::class,
], ],
DataObject::class.'_Controller', DataObject::class . 'Controller',
[ [
'type' => 'Includes', 'type' => 'Includes',
DataObject::class.'_Controller', DataObject::class . 'Controller',
], ],
], ],
$templates $templates
); );
// Let's throw something random in there. // Let's throw something random in there.
$self->setExpectedException('InvalidArgumentException'); $this->setExpectedException('InvalidArgumentException');
SSViewer::get_templates_by_class(array()); SSViewer::get_templates_by_class(array());
} }
); );

View File

@ -5,7 +5,7 @@ namespace SilverStripe\View\Tests\SSViewerTest;
use SilverStripe\Dev\TestOnly; use SilverStripe\Dev\TestOnly;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
class SSViewerTestModel_Controller extends Controller implements TestOnly class SSViewerTestModelController extends Controller implements TestOnly
{ {
} }