Skip to content

Commit

Permalink
Add fallback if image cannot be found (#173)
Browse files Browse the repository at this point in the history
* TASK: Enable hot dev server

* TASK: Add fallback if image cannot be found
  • Loading branch information
sabbelasichon authored Dec 16, 2022
1 parent 32111b8 commit 6ca5497
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Classes/Integration/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
33 changes: 22 additions & 11 deletions Classes/ViewHelpers/SvgViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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}']");

Expand Down

0 comments on commit 6ca5497

Please sign in to comment.