Files
cms-core/Classes/Imaging/IconProvider/AbstractSvgIconProvider.php
T

108 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Core\Imaging\IconProvider;
use TYPO3\CMS\Core\Imaging\Exception\InvalidSvgException;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconProviderInterface;
use TYPO3\CMS\Core\Imaging\Svg\SvgDocumentFactory;
use TYPO3\CMS\Core\Imaging\Svg\SvgDocumentService;
use TYPO3\CMS\Core\SystemResource\Exception\SystemResourceDoesNotExistException;
use TYPO3\CMS\Core\SystemResource\Exception\SystemResourceException;
use TYPO3\CMS\Core\SystemResource\SystemResourceFactory;
use TYPO3\CMS\Core\SystemResource\Type\SystemResourceInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
/**
* Abstract class for all SVG-based icon providers
*
* @internal
*/
abstract class AbstractSvgIconProvider implements IconProviderInterface
{
public const MARKUP_IDENTIFIER_INLINE = 'inline';
protected SvgDocumentFactory $svgDocumentFactory;
protected SvgDocumentService $svgDocumentService;
abstract protected function generateMarkup(Icon $icon, array $options): string;
abstract protected function generateInlineMarkup(array $options): string;
// inject* setters keep the constructor of inheriting providers clean.
public function injectSvgDocumentFactory(SvgDocumentFactory $svgDocumentFactory): void
{
$this->svgDocumentFactory = $svgDocumentFactory;
}
public function injectSvgDocumentService(SvgDocumentService $svgDocumentService): void
{
$this->svgDocumentService = $svgDocumentService;
}
public function prepareIconMarkup(Icon $icon, array $options = []): void
{
$icon->setMarkup($this->generateMarkup($icon, $options));
$icon->setAlternativeMarkup(self::MARKUP_IDENTIFIER_INLINE, $this->generateInlineMarkup($options));
}
/**
* Calculate public path of SVG file
*/
protected function getPublicPath(string $source): string
{
return (string)PathUtility::getSystemResourceUri($source);
}
protected function getInlineSvg(string $source): string
{
$svgContent = $this->getInlineSvgContents($source);
if ($svgContent === null) {
return '';
}
try {
return $this->svgDocumentService->toInlineMarkup(
$this->svgDocumentFactory->fromStringAndSanitize($svgContent)
);
} catch (InvalidSvgException) {
return '';
}
}
protected function getInlineSvgContents(string $source): ?string
{
try {
$resourceFactory = GeneralUtility::makeInstance(SystemResourceFactory::class);
$resource = $resourceFactory->createResource($source);
if ($resource instanceof SystemResourceInterface) {
return $resource->getContents();
}
} catch (SystemResourceDoesNotExistException) {
return null;
} catch (SystemResourceException) {
}
if (!PathUtility::isAbsolutePath($source)) {
$source = GeneralUtility::getFileAbsFileName($source);
}
if (!file_exists($source)) {
return null;
}
return file_get_contents($source) ?: null;
}
}