From 313c34c8231e007bdcd9503e6120ccbec281ea26 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Thu, 26 Sep 2024 16:24:21 +1200 Subject: [PATCH] ENH Improve typing to support refactored template layer --- src/AssetControlExtension.php | 2 +- src/File.php | 6 ++---- src/Shortcodes/FileShortcodeProvider.php | 9 +++++---- tests/php/ImageManipulationTest.php | 7 ++++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/AssetControlExtension.php b/src/AssetControlExtension.php index 32e77890..ac1a7790 100644 --- a/src/AssetControlExtension.php +++ b/src/AssetControlExtension.php @@ -247,7 +247,7 @@ protected function findAssets(DataObject $record) } // Omit variant and merge with set - $next = $record->dbObject($field)->getValue(); + $next = $record->dbObject($field)?->getValue(); unset($next['Variant']); if ($next) { $files[] = $next; diff --git a/src/File.php b/src/File.php index b2a19c88..04e8da45 100644 --- a/src/File.php +++ b/src/File.php @@ -1322,16 +1322,14 @@ public function forTemplate(): string /** * Return a html5 tag of the appropriate for this file (normally img or a) - * - * @return string */ - public function getTag() + public function getTag(): string { $template = $this->File->getFrontendTemplate(); if (empty($template)) { return ''; } - return (string)$this->renderWith($template); + return $this->renderWith($template); } /** diff --git a/src/Shortcodes/FileShortcodeProvider.php b/src/Shortcodes/FileShortcodeProvider.php index bc7d165c..2a9bcd27 100644 --- a/src/Shortcodes/FileShortcodeProvider.php +++ b/src/Shortcodes/FileShortcodeProvider.php @@ -18,8 +18,9 @@ use SilverStripe\View\HTML; use SilverStripe\View\Parsers\ShortcodeHandler; use SilverStripe\View\Parsers\ShortcodeParser; +use SilverStripe\View\SSTemplateEngine; use SilverStripe\View\SSViewer; -use SilverStripe\View\SSViewer_FromString; +use SilverStripe\View\ViewLayerData; /** * Provides shortcodes for File dataobject @@ -237,9 +238,9 @@ protected static function find_error_record($errorCode) */ public static function getCacheKey($params, $content = null) { - $key = SSViewer::config()->get('global_key'); - $viewer = new SSViewer_FromString($key); - $globalKey = md5($viewer->process(ArrayData::create([])) ?? ''); + $key = SSTemplateEngine::config()->get('global_key'); + $engine = SSTemplateEngine::create(); + $globalKey = md5($engine->renderString($key, ViewLayerData::create([]))); $argsKey = md5(serialize($params)) . '#' . md5(serialize($content)); return "{$globalKey}#{$argsKey}"; diff --git a/tests/php/ImageManipulationTest.php b/tests/php/ImageManipulationTest.php index 5e815f0a..22111d45 100644 --- a/tests/php/ImageManipulationTest.php +++ b/tests/php/ImageManipulationTest.php @@ -22,8 +22,9 @@ use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\SapphireTest; -use SilverStripe\View\SSViewer; use PHPUnit\Framework\Attributes\DataProvider; +use SilverStripe\View\SSTemplateEngine; +use SilverStripe\View\ViewLayerData; /** * ImageTest is abstract and should be overridden with manipulator-specific subtests @@ -447,10 +448,10 @@ public function testRender(string $template, string $expected) { /** @var Image $origin */ $image = $this->objFromFixture(Image::class, 'imageWithTitle'); - + $engine = new SSTemplateEngine(); $this->assertEquals( $expected, - trim($image->renderWith(SSViewer::fromString($template)) ?? '') + trim($engine->renderString($template, ViewLayerData::create($image))) ); }