From 6ca549791c83965954cb9fee4cf7fbbc2e6a0100 Mon Sep 17 00:00:00 2001 From: Sebastian Schreiber Date: Fri, 16 Dec 2022 09:09:16 +0100 Subject: [PATCH] Add fallback if image cannot be found (#173) * TASK: Enable hot dev server * TASK: Add fallback if image cannot be found --- Classes/Integration/Filesystem.php | 2 +- Classes/ViewHelpers/SvgViewHelper.php | 33 ++++++++++++++++++--------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Classes/Integration/Filesystem.php b/Classes/Integration/Filesystem.php index a89880c..3eaacd6 100644 --- a/Classes/Integration/Filesystem.php +++ b/Classes/Integration/Filesystem.php @@ -20,7 +20,7 @@ final class Filesystem implements FilesystemInterface { public function get(string $pathToFile): string { - $data = @file_get_contents($pathToFile); + $data = GeneralUtility::getUrl($pathToFile); if (false === $data) { throw new UnexpectedValueException(sprintf('Data could not be read from file %s', $pathToFile)); diff --git a/Classes/ViewHelpers/SvgViewHelper.php b/Classes/ViewHelpers/SvgViewHelper.php index 575b7aa..37a322b 100644 --- a/Classes/ViewHelpers/SvgViewHelper.php +++ b/Classes/ViewHelpers/SvgViewHelper.php @@ -15,7 +15,9 @@ use DOMElement; use DOMNodeList; use DOMXPath; +use Ssch\Typo3Encore\Integration\FilesystemInterface; use Ssch\Typo3Encore\Integration\IdGeneratorInterface; +use TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException; use TYPO3\CMS\Extbase\Service\ImageService; use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper; use UnexpectedValueException; @@ -30,18 +32,21 @@ class SvgViewHelper extends AbstractTagBasedViewHelper */ protected $tagName = 'svg'; - private ImageService $imageService; - private IdGeneratorInterface $idGenerator; - public function injectImageService(ImageService $imageService): void - { - $this->imageService = $imageService; - } + private FilesystemInterface $filesystem; - public function injectIdGenerator(IdGeneratorInterface $idGenerator): void - { + private ImageService $imageService; + + public function __construct( + FilesystemInterface $filesystem, + IdGeneratorInterface $idGenerator, + ImageService $imageService + ) { + parent::__construct(); + $this->filesystem = $filesystem; $this->idGenerator = $idGenerator; + $this->imageService = $imageService; } public function initializeArguments(): void @@ -72,8 +77,14 @@ public function initializeArguments(): void public function render(): string { - $image = $this->imageService->getImage($this->arguments['src'], null, false); - $imageUri = $this->imageService->getImageUri($image, (bool) $this->arguments['absolute']); + try { + $image = $this->imageService->getImage($this->arguments['src'], null, false); + $imageUri = $this->imageService->getImageUri($image, (bool) $this->arguments['absolute']); + $imageContents = $image->getContents(); + } catch (FolderDoesNotExistException $folderDoesNotExistException) { + $imageUri = $this->arguments['src']; + $imageContents = $this->filesystem->get($imageUri); + } $content = []; $uniqueId = 'unique'; @@ -110,7 +121,7 @@ public function render(): string $name = (string) $this->arguments['name']; if ($this->arguments['inline']) { $doc = new DOMDocument(); - $doc->loadXML($image->getContents()); + $doc->loadXML($imageContents); $xpath = new DOMXPath($doc); $iconNodeList = $xpath->query("//*[@id='{$name}']");