TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* 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\Icon;
|
||||
use TYPO3\CMS\Core\Imaging\IconProviderInterface;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Core\Utility\PathUtility;
|
||||
|
||||
/**
|
||||
* Class provides icons that are classic <img> tags using bitmaps as source
|
||||
*/
|
||||
class BitmapIconProvider implements IconProviderInterface
|
||||
{
|
||||
public const MARKUP_IDENTIFIER_INLINE = 'inline';
|
||||
|
||||
public function prepareIconMarkup(Icon $icon, array $options = [])
|
||||
{
|
||||
$icon->setMarkup($this->generateMarkup($icon, $options));
|
||||
$icon->setAlternativeMarkup(self::MARKUP_IDENTIFIER_INLINE, $this->generateInlineMarkup($icon, $options));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function generateMarkup(Icon $icon, array $options)
|
||||
{
|
||||
if (empty($options['source'])) {
|
||||
throw new \InvalidArgumentException('[' . $icon->getIdentifier() . '] The option "source" is required and must not be empty', 1440754980);
|
||||
}
|
||||
|
||||
$source = $options['source'];
|
||||
|
||||
return '<img src="' . htmlspecialchars($this->getPublicPath($source)) . '" width="' . $icon->getDimension()->getWidth() . '" height="' . $icon->getDimension()->getHeight() . '" alt="" />';
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate public path of image file
|
||||
*/
|
||||
protected function getPublicPath(string $source): string
|
||||
{
|
||||
return (string)PathUtility::getSystemResourceUri($source);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function generateInlineMarkup(Icon $icon, array $options)
|
||||
{
|
||||
if (empty($options['source'])) {
|
||||
throw new \InvalidArgumentException('The option "source" is required and must not be empty', 1471460676);
|
||||
}
|
||||
|
||||
$source = $options['source'];
|
||||
|
||||
$filePath = PathUtility::isAbsolutePath($source) ? $source : GeneralUtility::getFileAbsFileName($source);
|
||||
|
||||
if (!file_exists($filePath)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
'<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 %1$d %2$d" width="%1$d" height="%2$d"><image width="%1$d" height="%1$d" xlink:href="%3$s"/></svg>',
|
||||
$icon->getDimension()->getWidth(),
|
||||
$icon->getDimension()->getHeight(),
|
||||
$this->getPublicPath($source)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* 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\Icon;
|
||||
|
||||
/**
|
||||
* Class provides icons that are classic <img> tags using vectors as source
|
||||
*/
|
||||
class SvgIconProvider extends AbstractSvgIconProvider
|
||||
{
|
||||
/**
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function generateMarkup(Icon $icon, array $options): string
|
||||
{
|
||||
if (empty($options['source'])) {
|
||||
throw new \InvalidArgumentException('[' . $icon->getIdentifier() . '] The option "source" is required and must not be empty', 1460976566);
|
||||
}
|
||||
|
||||
$source = $options['source'];
|
||||
return '<img src="' . htmlspecialchars($this->getPublicPath($source)) . '" width="' . $icon->getDimension()->getWidth() . '" height="' . $icon->getDimension()->getHeight() . '" alt="" />';
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function generateInlineMarkup(array $options): string
|
||||
{
|
||||
if (empty($options['source'])) {
|
||||
throw new \InvalidArgumentException('The option "source" is required and must not be empty', 1460976610);
|
||||
}
|
||||
|
||||
$source = $options['source'];
|
||||
|
||||
return $this->getInlineSvg($source);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?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\Icon;
|
||||
|
||||
/**
|
||||
* SvgSpriteIconProvider provides sprite icons and are rendered via <svg> tag
|
||||
*/
|
||||
class SvgSpriteIconProvider extends AbstractSvgIconProvider
|
||||
{
|
||||
/**
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function generateMarkup(Icon $icon, array $options): string
|
||||
{
|
||||
if (empty($options['sprite'])) {
|
||||
throw new \InvalidArgumentException('[' . $icon->getIdentifier() . '] The option "sprite" is required and must not be empty', 1603439142);
|
||||
}
|
||||
|
||||
$source = $options['sprite'];
|
||||
return $this->generateSpriteUseMarkup($source);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function generateInlineMarkup(array $options): string
|
||||
{
|
||||
if (!empty($options['source'])) {
|
||||
$source = $options['source'];
|
||||
return $this->getInlineSvg($source);
|
||||
}
|
||||
|
||||
if (empty($options['sprite'])) {
|
||||
throw new \InvalidArgumentException('The option "sprite" is required and must not be empty if not source SVG file is provided', 1603439146);
|
||||
}
|
||||
|
||||
$source = $options['sprite'];
|
||||
return $this->generateSpriteUseMarkup($source);
|
||||
}
|
||||
|
||||
private function generateSpriteUseMarkup(string $sprite): string
|
||||
{
|
||||
return '<svg class="icon-color"><use xlink:href="' . htmlspecialchars($this->getPublicPath($sprite)) . '" /></svg>';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user