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

View File

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

View File

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